【问题标题】:Error: Can only create List of a GraphQLType but got: function GraphQLObjectType(config) , in GraphQL?错误:只能创建 GraphQLType 列表,但在 GraphQL 中得到:函数 GraphQLObjectType(config) ?
【发布时间】:2017-09-06 08:50:46
【问题描述】:

我正在开发一个关于 GraphQL 的演示项目。却卡在了这里。我想发送整个对象作为结果。 我要发送的对象是:

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

但是当我发送访问它的请求时,我得到了错误:

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

【问题讨论】:

    标签: node.js graphql graphql-js graphql-subscriptions


    【解决方案1】:
    1. 第一个 fakeDatabase 应该是数组 [],因为 schema.getAllUser 中的字段 data 类型为 GraphQLList

    2. 其次,您必须创建一个GraphQLObjectType,其中包含idnamedescription 字段


    可能是这样的......

    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

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

    希望这有帮助!

    【讨论】:

    • 谢谢。现在错误已被删除,但结果在 data- { "data": { "getAllUser": { "data": null } } }
    • 根据我的答案中设置的数组元素,您必须从 getalluser ObjectType 中 data 字段的解析器发送 fakeDatabase 数组 []
    • 是的,你是对的,我正在发送 fakeBatabase 。在发送它之前,我有控制台然后它打印正常。但是 fakeBatabase 没有响应/结果。
    • 这是我的 queryType:- getAllUser: { type: userType.getAllUser, args: { }, resolve: function (source, args) { return data.fakeDatabase. } }
    • 我正在发送正确的数据,我确信因为在发送之前我有控制台并且在发送之前一切都很好。
    猜你喜欢
    • 2016-12-28
    • 2018-01-05
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 2019-03-13
    • 2017-11-29
    • 2017-09-19
    • 2019-08-14
    相关资源
    最近更新 更多