2015-12-10 73 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

你可以也應該詳細說明你的答案,解釋它做了什麼,它爲什麼起作用,這樣的事情。 –