Error: Can only create List of a GraphQLType but got: function GraphQLObjectType(config) , in GraphQL?
Asked Answered
O

1

0

I am working on a demo project on GraphQL. But stuck here. I want to send the whole object as result. The object which I want to send is:

 exports.fakeDatabase = {
        1: {
            id: 1,
            name: 'Abhay',
            description:'This is Abhay\'s Database'
        },
        2: {
            id: 2,
            name:'Bankimchandra',
            description: 'This is Bankimchandra\'s Database'
        },
         3: {
            id: 3,
            name:'chandu',
            description: 'This is chandu\'s Database'
        }
    };

But when I am sending a request to access it I got Error:

Error: Can only create List of a GraphQLType but got: function GraphQLObjectType(config) {
    _classCallCheck(this, GraphQLObjectType);

    (0, _assertValidName.assertValidName)(config.name, config.isIntrospection);
    this.name = config.name;
    this.description = config.description;
    if (config.isTypeOf) {
      (0, _invariant2.default)(typeof config.isTypeOf === 'function', this.name + ' must provide "isTypeOf" as a function.');
    }
    this.isTypeOf = config.isTypeOf;
    this._typeConfig = config;
  }.

My code is-

schema.js:

const graphql = require('graphql');
var schema = {};
schema.getAllUser = new graphql.GraphQLObjectType({
    name: 'getAllUser',
    fields: {
        data:{type:new graphql.GraphQLList(graphql.GraphQLObjectType)}  // What should I do here to send the whole `fakeObject`
   }
})
module.exports = schema;

Query.js:

const graphql = require('graphql');
const userType = require('../schemas/schemaUserType');
const fakeDatabase = require('../assets/database');
const config = require('../config/config');


var schema = {};
module.exports = schema;
const queryType = new graphql.GraphQLObjectType({
    name: 'Query',
    fields: {
 getAllUser: {
            type: userType.getAllUser,
            args: {
            }, resolve: function () {
                return fakeDatabase.fakeDatabase;
            }
        }
    }
});
schema.queryTypq1 = new graphql.GraphQLSchema({ query: queryType });

server.js:

var app = require('express')();
var graphHTTP = require('express-graphql');
const schema = require('./queries/queryType');

app.use('/graphql', graphHTTP({
  schema: schema.queryTypq1,
  graphiql: true
}));

app.listen(4000, () => { console.log('Server is running on port: 4000'); });

Please make me understand what should i do to send the object fakeDatabase

Ostensorium answered 11/4, 2017 at 6:6 Comment(0)
D
3
  1. First fakeDatabase should be an array [] as the field data type is of GraphQLList in schema.getAllUser

  2. Second you have to create a GraphQLObjectType with the fields id, name and description


may be something like this...

exports.fakeDatabase = [
    {
        id: 1,
        name: 'Abhay',
        description: 'This is Abhay\'s Database'
    },
    {
        id: 2,
        name: 'Bankimchandra',
        description: 'This is Bankimchandra\'s Database'
    },
    {
        id: 3,
        name: 'chandu',
        description: 'This is chandu\'s Database'
    }
]

and the GraphQLObjectType to represent the data

const fakeDatabaseType = new GraphQLObjectType({
    name: 'fakeDatabase',
    fields: {
        id: { type: GraphQLID },
        name: { type: GraphQLString },
        description: { type: GraphQLString },
    },
});


const graphql = require('graphql');
var schema = {};
schema.getAllUser = new graphql.GraphQLObjectType({
    name: 'getAllUser',
    fields: {
        data: {
            type: new graphql.GraphQLList(fakeDatabaseType),
            resolve: function (obj) { /* obj is the parent object 
containing the data passed from root query resolver */
                return obj;
            },
        }
    }
})
module.exports = schema;

const graphql = require('graphql');
const userType = require('../schemas/schemaUserType');
const fakeDatabase = require('../assets/database');
const config = require('../config/config');


var schema = {};
module.exports = schema;
const queryType = new graphql.GraphQLObjectType({
    name: 'Query',
    fields: {
        getAllUser: {
            type: schema.getAllUser,
            args: {
            }, resolve: function () {
                return fakeDatabase; // passing the fake array
            }
        }
    }
});
schema.queryTypq1 = new graphql.GraphQLSchema({ query: queryType });

Hope this Helps!!

Dope answered 11/4, 2017 at 6:59 Comment(12)
Thank You. Now error has been removed but result is coming null in data- { "data": { "getAllUser": { "data": null } } }Ostensorium
as per the array elements set in mine answer, you have to send the fakeDatabase array [] from the resolver of data field in getalluser ObjectTypeDope
yes, you are right, I am sending the fakeBatabase . Before sending it I have console it then it printing fine as it is. But the fakeBatabase is not coming in response/result.Ostensorium
This is my queryType:- getAllUser: { type: userType.getAllUser, args: { }, resolve: function (source, args) { return data.fakeDatabase. } }Ostensorium
I am sending correct data , i am sure because before sending it I have console and everything are fine before sending.Ostensorium
please check this gist, i will update the gist soon again for sending the data from query and structuring the fakedatabase array too, gist.github.com/parwat08/561bbc386c33e2d448ca8962e40ae405Dope
cloud.githubusercontent.com/assets/19905275/24898435/…Dope
@ShubhamVerma this is the gist to pass data from query resolver gist.github.com/parwat08/b3414080e03ff0e68b1227957a9b0d22, please let me know whether this solved problem or not and please check the export of fakedatabase [] the export may be differDope
Thank You so much, It worked for me. You saved my time. I really want to appreciate york effort and time. Best wishes.Ostensorium
But tell me one thing- why we are using resolve method in schema.getAllUser(){ ..... } ? Though we are also using resolve() in the resolver method?Ostensorium
because, schema.getAllUser too contains the GraphQLObjectType called fakedatabaseType, and each of the field resolver have the access to parent object, and the id, name, description fields resolver function from the fakedatabasetype too have the access to parent object from the type getAllUserDope
@ShubhamVerma yes, you can as it is a static data, the structure of the fake data should match accordingly with the fields of the ObjectType (vice-versa), but while extracting data from database, instead of gathering all data and structuring the data in main root query. You need to access the main id get data from it in rootQuery, and grab the required data by providing the parent object from rootQuery to the nested ObjectType resolver functions...., as i had done in answerDope

© 2022 - 2024 — McMap. All rights reserved.