Lorenz
Lorenz

Reputation: 67

GraphQL: Error when defining a GraphQLList with a custom type in a GraphQLObjectType

I want to learn GraphQL and I am coding an example that represents a Book-Shop where users can be the author of multiple books.

My problem is that I can't define a list of the type "BookType" in UserType (GraphQLObjectType). The strange thing is that it throws no error when a BookType has an author.

user.js (UserType):

const {
    GraphQLList,
    GraphQLID, 
    GraphQLString, 
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLInt,
     } = require('graphql');

const BookType = require('./book');
const BookSchema = require('../db/models/book');

console.log(BookType);

module.exports = new GraphQLObjectType({
    name: 'User',
    fields: () => ({
        id: { type: new GraphQLNonNull(GraphQLID) },
        name: { type: new GraphQLNonNull(GraphQLString) },
        books: { 
            type: new GraphQLList(BookType),
            resolve(parent, args){
                return BookSchema.find({_id: parent.id});
            }
         }
    })
});

book.js (BookType):

const {
    GraphQLID, 
    GraphQLString, 
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLInt } = require('graphql');

const UserType = require('./user');
const UserSchema = require('../db/models/user');

console.log(UserType);


module.exports = new GraphQLObjectType({
    name: 'Book',
    fields: () => ({
        id: { type: new GraphQLNonNull(GraphQLID) },
        author: { 
            type: UserType,
            resolve(parent, args){
                return UserSchema.findOne({_id: parent.author});
            }
         },
        title: { type: new GraphQLNonNull(GraphQLString) },
        sites: { type: new GraphQLNonNull(GraphQLInt) }
    })
});

As you can see I log BookType and UserType. Strangely I get an empty object when I log BookType.

Output:

console.log(BookType) --> {}
console.log(UserType) --> User

Here is the error:

Error: Expected [object Object] to be a GraphQL type.
    at invariant (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/jsutils/invariant.js:19:11)
    at assertType (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:88:43)
    at new GraphQLList (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:258:19)
    at fields (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/src/types/user.js:21:19)
    at resolveThunk (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:370:40)
    at defineFieldMap (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:532:18)
    at GraphQLObjectType.getFields (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/definition.js:506:44)
    at typeMapReducer (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:232:38)
    at /media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:239:20
    at Array.forEach (<anonymous>)
    at typeMapReducer (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:232:51)
    at /media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:239:20
    at Array.forEach (<anonymous>)
    at typeMapReducer (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:232:51)
    at Array.reduce (<anonymous>)
    at new GraphQLSchema (/media/lorenz/DATA/Privat/Programmieren/JavaScript/NodeJS/bookShop/node_modules/graphql/type/schema.js:122:28)

Upvotes: 1

Views: 936

Answers (1)

Daniel
Daniel

Reputation: 15489

you are repeating a lot of code here. I would begin my creating one file, call it schema.js.

Work on developing your RootQuery, here is an example that could help:

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    user: {
      type: UserType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
        return axios
          .get(`http://localhost:3000/users/${args.id}`)
          .then(res => res.data);
      }
    },
    company: {
      type: CompanyType,
      args: { id: { type: GraphQLString } },
      resolve(parentValue, args) {
        return axios
          .get(`http://localhost:3000/companies/${args.id}`)
          .then(res => res.data);
      }
    }
  }
});

export that and then for UserType and others create functions out of them. Here is an example of that:

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    id: { type: GraphQLString },
    firstName: { type: GraphQLString },
    age: { type: GraphQLInt },
    company: {
      type: CompanyType,
      resolve(parentValue, args) {
        return axios
          .get(`http://localhost:3000/companies/${parentValue.companyId}`)
          .then(res => res.data);
      }
    }
  })
});

So instead of exporting GraphQLObjectType for each one, you can just export one time

module.exports = new GraphQLSchema({ query: RootQuery });

Upvotes: 0

Related Questions