GraphQL] The type of [...] must be Output Type but got: undefined
Asked Answered
S

2

5

I'm having troubles with referencing to other GraphQLObjectTypes inside a GraphQLObjectType. I keep getting the following error:

"message": "The type of getBooks.stores must be Output Type but got: undefined."

I thought that using a resolve inside books.stores would help fixing the issue, but it doesn't. Can someone help me out?

Code

var express = require('express');
var graphqlHTTP = require('express-graphql');
var graphql = require('graphql');

var { buildSchema, GraphQLSchema, GraphQLObjectType,GraphQLString,GraphQLList } = require('graphql');

var books = new GraphQLObjectType({
  name:'getBooks',
  fields: {
    isbn: { type: GraphQLString},
    stores: {
      type: stores,
      resolve: function(){
        var store = [];
        store.storeName = "this will contain the name of a shop";
        return store;
      }
    }
  }
});

var stores = new GraphQLObjectType({
  name:'storeList',
  fields: {
    storeName: { type: GraphQLString}
  }
});


var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    books: {
      type: books,
      // `args` describes the arguments that the `user` query accepts
      args: {
        id: { type: new GraphQLList(GraphQLString) }// graphql.GraphQLStringj
      },
      resolve: function (_, {id}) {
        var data = [];
        data.isbn = '32131231';
        return data;
      }
    }
  }
});

var schema = new GraphQLSchema({query: queryType});

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');
Surfacetosurface answered 22/1, 2018 at 6:17 Comment(0)
S
1

It couldn't find stores because it was defined after books. So I placed the stores code before books.

Surfacetosurface answered 22/1, 2018 at 6:32 Comment(0)
I
5

Or you can just make the field a function type so that it can wrap type relations For example

fields: () => ({

  //Enter Your Code

})
Impi answered 11/6, 2019 at 7:58 Comment(1)
Adding on, the arrow function allows it so you don't have to worry about the order of the store's type. Also, this way it will allow you to modulize your schemas, so in case your app gets large, it'll be cleaner to look for specific schemas in specific folder/files.Pb
S
1

It couldn't find stores because it was defined after books. So I placed the stores code before books.

Surfacetosurface answered 22/1, 2018 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.