我必须解决 GraphQL 的循环定义,但由于某种原因我无法解决。我已经检查了几篇关于 thunk 和延迟加载的帖子,这是我当前的代码:
历史/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)
});
User/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)
});
Company/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
最佳答案
我想你现在已经解决了,但是对于任何路过的人来说:
在 history/types.js
中,fields
对象必须内联到 HistoryType
的 fields
函数中,因此它在运行时创建。
const HistoryType = new GraphQLObjectType({
...
fields: () => ({ ... }), // not a var, object is inlined
...
});
关于javascript - 使用 thunk 的 GraphQL 循环引用 - 错误 : field type must be Output Type but got: undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009069/