2015-12-10 38 views
2

我想与多个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 ...

+0

不知道,但你可能需要把类型为注册模块打破nodeinterface <->用户循环依赖。 –

回答

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

+4

初学者可以详细说明一下吗?谢谢。 –

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>; 
+3

你可以也应该详细说明你的答案,解释它做了什么,它为什么起作用,这样的事情。 –