我想与多个GraphQL类型共享一个nodeInterface.目前收到此错误:
Error: User may only implement Interface types, it cannot implement: undefined.
我已经声明了这样的界面:
// file: node-interface.js let {nodeInterface, nodeField} = nodeDefinitions( (globalId) => { let {type, id} = fromGlobalId(globalId); if (type === 'UserModel') {; return UserModel.findOne(id).exec(); } return null; }, (obj) => { if (obj instanceof UserModel) { return UserType; } return null; } ); export { nodeInterface }; export { nodeField }; and attempting to use it in my UserType like this // file: user.js import { nodeInterface } from ‘./node-interface'; let UserType = new GraphQLObjectType({ name: 'User', description: 'A user', fields: () => ({ id: globalIdField('User'), username: { type: GraphQLString, }, }), interfaces: [nodeInterface] });
我错过了什么?我需要能够将多个GraphQL类型的声明分解为相应的文件并实现nodeInterface ...
有必要为任何物质的模式创建类型注册表.见这里的例子:
输入注册表
然后像这样创建nodeInterface和nodeField:
// ./src/schema/node.js import { nodeDefinitions } from 'graphql-relay'; import { idFetcher, typeResolver } from './registry'; export const { nodeInterface, nodeField } = nodeDefinitions( idFetcher, typeResolver );
结帐此问题了解更多信息:抽象类型解析