Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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');
like image 946
Elvira Avatar asked Jan 22 '18 06:01

Elvira


2 Answers

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

fields: () => ({

  //Enter Your Code

})
like image 186
Yashika Sorathia Avatar answered Oct 20 '22 11:10

Yashika Sorathia


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

like image 28
Elvira Avatar answered Oct 20 '22 12:10

Elvira