Share Relay nodeInterface

时间:2015-12-10 01:13:17

标签: relayjs graphql graphql-js

I want to share a single nodeInterface with multiple GraphQL types. Currently getting this error:

Error: User may only implement Interface types, it cannot implement: undefined.

I have declared the interface like this:

// 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]
});

What am I missing? I need to be able to break up the declaration of multiple GraphQL types into corresponding files and implement the nodeInterface...

2 个答案:

答案 0 :(得分:6)

必须为任何物质的模式创建类型注册表。见这里的例子:

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

答案 1 :(得分:1)

咚:

// 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>;