简体   繁体   中英

GraphQL Cross References throws error: field type must be Output Type but got: [object Object]

I am struggleing at the moment with my GraphQL API and unfortunately it is not working because I get always the error message:

Error: Contact.other field type must be Output Type but got: [object Object].

I still read some articles and posts, like GraphQL with express error : Query.example field type must be Output Type but got: [object Object] , but it does not work anyway because the answers does not solve the error reason in my case. I hope you can help me or just give me a hint to solve this problem. I attached the main parts of my code below:

ProfileType.js:

const graphql = require('graphql');
const ContactType = require('./ContactType');
const ObjectType = graphql.GraphQLObjectType;
const List = graphql.GraphQLListType;
const ID = graphql.GraphQLID;
const NonNull = graphql.GraphQLNonNull;

const ProfileType = new ObjectType({
  name: 'Profile',
  fields: function () {
    return {
      id: {type: new NonNull(ID)},
      contacts: {type: new List(ContactType)},
    };
  },
});

module.exports = ProfileType;

ContactType.js:

const graphql = require('graphql');
const ProfileType = require('./ProfileType');
const ObjectType = graphql.GraphQLObjectType;
const EnumType = graphql.GraphQLEnumType;

const ContactType = new ObjectType({
  name: 'Contact',
  fields: function () {
    return {
      other: {
        type: ProfileType
      },
      status: {
        type: new EnumType({
          values: {
            REQUESTED: {value: 0},
            COMMITTED: {value: 1}
          },
          name: 'ContactStatus'
        })
      }
    };
  },
});

module.exports = ContactType;

(Posted on behalf of the OP).

Solved it by moving the required ObjectType to the fields function:

const ContactType = new ObjectType({
  name: 'Contact',
  fields: function () {
    const ProfileType = require('./ProfileType');
    // ...
  }
});

Otherwise, the ObjectType has problems with the circularity. The same have to be done of course with the ProfileType.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM