GraphQL循环引用使用thunk - 错误:字段类型必须是输出类型,但得到:undefined

问题描述:

我必须解决GraphQL的循环定义,但由于某种原因,我无法解决。从来就核对了帖子有关的thunk和延迟加载,这里是我当前的代码:GraphQL循环引用使用thunk - 错误:字段类型必须是输出类型,但得到:undefined

历史/ types.js

const fields = { 
    id: { 
     type: new GraphQLNonNull(GraphQLID), 
     resolve: (obj) => dbIdToNodeId(obj._id, "History") 
    }, 
    timestamp: { 
     type: GraphQLLong 
    }, 
    objectId: { 
     type: GraphQLString 
    }, 
    user: { 
     type: require('../User/connections').default, 
     args: connectionArgs, 
     resolve: (source, args) => { 
      return UserModel.findOne({ id: source.id }).exec(); 
     } 
    }, 
    action: { 
     type: new GraphQLNonNull(GraphQLString) 
    } 
}; 

export const HistoryType = new GraphQLObjectType({ 
    name: 'History', 
    description: 'History', 
    interfaces:() => [NodeInterface], 
    isTypeOf: (value) => value instanceof HistoryModel, 
    fields:() => (fields) 
}); 

历史/ connections.js:

import { HistoryType } from './types'; 
const { connectionType: HistoryConnectionType } = connectionDefinitions({ nodeType: HistoryType }); 

export default HistoryConnectionType; 

User/types.js

const fields = { 
    id: { 
     type: new GraphQLNonNull(GraphQLID), 
     resolve: (obj) => dbIdToNodeId(obj._id, "User") 
    }, 
    email: { 
     type: GraphQLString 
    }, 
    history: { 
     type: require('../History/connections').default, 
     args: connectionArgs, 
     resolve: (source, args, context) => { 
      return HistoryModel.find({ deleted: false, objectId: source.id}).sort('timestamp').exec(); 
     } 
    } 
}; 

export const UserType = new GraphQLObjectType({ 
    name: 'User', 
    description: 'User', 
    interfaces:() => [NodeInterface], 
    isTypeOf: (value) => value instanceof UserModel, 
    fields:() => (fields) 
}); 

用户/ connections.js

import { UserType } from './types'; 

const { connectionType: UserConnectionType } = connectionDefinitions({ nodeType: UserType }); 

export default UserConnectionType; 

公司/ types.js

const fields = { 
    id: { 
     type: new GraphQLNonNull(GraphQLID), 
     resolve: (obj) => dbIdToNodeId(obj._id, "Company") 
    }, 
    name: { 
     type: GraphQLString 
    }, 
    users: { 
     type: require('../User/connections').default, 
     args: connectionArgs, 
     resolve: (source, args) => { 
      const { id } = source; 
      return UserModel.find({ companyId: id }).then((rows) => connectionFromArray(rows,args)); 
     } 
    }, 
    history: { 
     type: require('../History/connections').default, 
     args: connectionArgs, 
     resolve(source, args, context) { 
      return loaders.getHistory(source.id); 
     } 
    } 
}; 

export const CompanyType = new GraphQLObjectType({ 
    name: 'Company', 
    description: 'Company', 
    interfaces:() => [NodeInterface], 
    isTypeOf: (value) => value instanceof CompanyModel, 
    fields:() => (fields) 
}); 

公司/ connections.js

import { CompanyType } from './types'; 

const { connectionType: CompanyConnectionType } = connectionDefinitions({ nodeType: CompanyType }); 

export default CompanyConnectionType; 

我不能让它起作用。当运行我得到以下输出:

History.user field type must be Output Type but got: undefined 

帮助赞赏解决这一问题。

+0

这可能是有益的https://stackoverflow.com/questions/42498900/graphql-error-undefined-adding-a-relationship/42500391#42500391 –

我猜你现在解决了,但任何人都匆匆而过:
history/types.js中,fields对象必须在HistoryTypefields函数内联,所以它能够在运行时创建的。

const HistoryType = new GraphQLObjectType({ 
    ... 
    fields:() => ({ ... }), // not a var, object is inlined 
    ... 
});