错误:只能在GraphQL中创建GraphQLType的列表,但得到:函数GraphQLObjectType(config)? [英] Error: Can only create List of a GraphQLType but got: function GraphQLObjectType(config) , in GraphQL?

查看:176
本文介绍了错误:只能在GraphQL中创建GraphQLType的列表,但得到:函数GraphQLObjectType(config)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在GraphQL上的演示项目.但是卡在这里.我想发送整个对象作为结果. 我要发送的对象是:

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;
  }.

我的代码是-

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'); });

请让我明白如何发送对象fakeDatabase

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

推荐答案

  1. 第一个fakeDatabase应该是数组[],因为schema.getAllUser

第二步,您必须使用字段idnamedescription

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'
    }
]


和GraphQLObjectType代表数据


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 });

希望这会有所帮助!

这篇关于错误:只能在GraphQL中创建GraphQLType的列表,但得到:函数GraphQLObjectType(config)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆