我想与多个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 ...
最佳答案
必须为任何物质的架构创建类型注册表。在这里查看示例:
Type Registry
然后像这样创建nodeInterface和nodeField:
// ./src/schema/node.js
import { nodeDefinitions } from 'graphql-relay';
import { idFetcher, typeResolver } from './registry';
export const { nodeInterface, nodeField } = nodeDefinitions(
idFetcher, typeResolver
);
请查看此问题以获取更多信息:Abstract type resolution