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

时间:2017-09-01 23:23:46

标签: javascript graphql relayjs graphql-js relay

我必须解决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;

用户/ 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

1 个答案:

答案 0 :(得分:1)

我猜你现在已经解决了,但对于路过的人来说: 在SELECT customer_id , order_date , order_rank , COALESCE( DATE(order_date) - DATE(LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)) , 0) FROM <table_name> 中,history/types.js对象必须在fields的{​​{1}}函数中内联,因此它会在运行时创建。

fields