简体   繁体   English

使用thunk的GraphQL循环引用-错误:字段类型必须为Output Type,但得到:undefined

[英]GraphQL cyclical reference using thunks - Error: field type must be Output Type but got: undefined

I have to solve a cyclic definition for GraphQL, but for some reason I´m not being able to solve. 我必须解决GraphQL的循环定义,但是由于某些原因,我无法解决。 I´ve checked several posts about thunks and lazy loading and here is my current code: 我检查了几篇关于thunk和延迟加载的文章,这是我当前的代码:

History/types.js 历史/ 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)
});

History/connections.js: 历史/ connections.js:

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

export default HistoryConnectionType;

User/types.js 用户/ 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 用户/ connections.js

import { UserType } from './types';

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

export default UserConnectionType;

Company/types.js 公司/ 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 公司/ connections.js

import { CompanyType } from './types';

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

export default CompanyConnectionType;

I can´t make it work. 我无法使它工作。 When running I´m getting the following output: 运行时,我得到以下输出:

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

I guess you resolved it by now, but for anyone passing by: 我想您现在已经解决了,但是对于路过的人来说:
In history/types.js , the fields object must be inlined in the fields function of HistoryType , so it gets created at runtime. history/types.js ,必须在HistoryTypefields函数中内联fields对象,以便在运行时创建它。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphQL:Query.type 字段类型必须是输出类型但得到:未定义 - GraphQL: Query.type field type must be Output Type but got: undefined GraphQL交叉引用引发错误:字段类型必须为输出类型,但得到:[对象对象] - GraphQL Cross References throws error: field type must be Output Type but got: [object Object] GraphQL 错误字段类型必须是输入类型,但得到: - GraphQL Error field type must be Input Type but got: GraphQL Args错误:参数类型必须是输入类型但得到:函数GraphQLObjectType(config){ - GraphQL Args error: argument type must be Input Type but got: function GraphQLObjectType(config) { GraphQL 错误:预期未定义为 GraphQL 类型 - GraphQL Error: Expected undefined to be a GraphQL type Realm DB React 本机架构 - 类型必须为“字符串”类型,出现(未定义)错误 - Realm DB React native Schema - type must be of type 'string', got (undefined) Error Graphql 说 Validation error of type undefined - Graphql saying Validation error of type undefined 尝试使用gh-pages部署我的React应用程序,但收到此错误消息:“file”参数必须是string类型。收到的类型未定义 - Trying to deploy my React app with gh-pages but got this error message : The “file” argument must be of type string. Received type undefined graphql 错误:内省必须为arguments提供输入类型,但收到:[AccommodationPictureMeta] - graphql Error: Introspection must provide input type for arguments, but received: [AccommodationPictureMeta] 使用 GraphQL 字段类型的对象 - Working with GraphQL Field Type of Objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM