Search code examples
javascriptgraphqlrelayjsgraphql-jsrelay

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. I´ve checked several posts about thunks and lazy loading and here is my current code:

History/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:

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;

Company/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;

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

Solution

  • 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.

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