共享中继节点接口

Share Relay nodeInterface

我想与多个 GraphQL 类型共享一个节点接口。当前收到此错误:

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

砰砰声:

// interfaces: [nodeInterface]
interfaces: () => [nodeInterface]

details:

type GraphQLObjectTypeConfig = {
    name: string;
    interfaces?: GraphQLInterfacesThunk | Array<GraphQLInterfaceType>;
    fields: GraphQLFieldConfigMapThunk | GraphQLFieldConfigMap;
    isTypeOf?: (value: any, info?: GraphQLResolveInfo) => boolean;
    description?: ?string
}

type GraphQLInterfacesThunk = () => Array<GraphQLInterfaceType>;