本文介绍了GraphQL:错误:模式查询必须是对象类型,但是得到:[对象对象]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为rootQuery继承架构,以获得更多的模块化.当前的设置如下:

I'm currently trying to inherit schemas for a rootQuery in order to get more modularity. The setup currently looks as follows:

invoiceSchema.js

invoiceSchema.js

import {
    GraphQLObjectType,
    GraphQLInt,
} from 'graphql';
export default new GraphQLObjectType({
    name: 'Invoice',
    description: 'A monthly billing invoice for an organization',
    fields: () => ({
        amountDue: {
            type: GraphQLInt,
            description: 'The amount the card will be charged (total + startingBalance with a min value of 0)'
        },
    })
});

rootQuery.js

rootQuery.js

import {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLList,
    GraphQLID
} from 'graphql';
import Invoice from './invoiceSchema';

export default {
    Invoice: {
        type: Invoice,
        resolve(parentValue, args){
            return 'Hello world';
        }
    }
};

schema.js

schema.js

import query from './rootQuery';
import {GraphQLSchema} from 'graphql';

export default new GraphQLSchema({query});


当尝试执行以下错误并希望获得一些帮助和见解时,因为我在invoiceSchema.js中导出的​​内容显然是ObjectType而不是对象Object.


When trying to do the following error and hoped for some help and insight, as what I'm exporting in invoiceSchema.js is clearly an ObjectType and not an object Object.

C:\project\node_modules\graphql\jsutils\invariant.js:19
    throw new Error(message);
    ^

Error: Schema query must be Object Type but got: [object Object].
    at invariant (C:\project\node_modules\graphql\jsutils\invariant.js:19:11)
    at new GraphQLSchema (C:\project\node_modules\graphql\type\schema.js:72:88)
    at Object.<anonymous> (C:/project/api/schema/schema.js:6:16)
    at Module._compile (module.js:573:30)
    at loader (C:\project\node_modules\babel-register\lib\node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (C:\project\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
[nodemon] app crashed - waiting for file changes before starting...

实际上是从此处得到的,我我想知道为什么它不起作用...

Actually got the idea from here and I'm wondering why it doesn't work...

推荐答案

您的根查询必须是 GraphQLObjectType 的实例,但是, rootQuery.js 会导出一个普通对象.您需要将导出更改为以下内容:

Your root query needs to be an instance of GraphQLObjectType, however, rootQuery.js is exporting a plain Object instead. You'll need to change your export to something like this:

export default new GraphQLObjectType({
  name: 'RootQuery',
  fields: () => ({
    invoice: {
        type: Invoice,
        resolve(parentValue, args){
            return 'Hello world';
        }
    }
  })
};

注意:通常的做法是将所有字段名称(包括查询和突变名称)保留在camelCase中,并使用PascalCase作为类型名称,以帮助区分两者.

Note: it's common practice to keep all field names, including query and mutation names, in camelCase and use PascalCase for type names, to help distinguish between the two.

此外,如果您要对架构进行模块化,则使用 graphql-tools 用于生成您的架构.IMO,它使您的模式更具可读性,并有助于避免在对模式进行模块化时可能遇到的一些更常见的陷阱.该文档提供了一个很好的示例,说明了如何使用 makeExecutableSchema 此处.

Also, if you are modularizing your schema, you may find it helpful to utilize graphql-tools for generating your schema instead. IMO, it makes your schema more readable and helps avoid some of the more common pitfalls you may face when modularizing a schema. The docs have a great example of how to modularize your schema with makeExecutableSchema here.

这篇关于GraphQL:错误:模式查询必须是对象类型,但是得到:[对象对象]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 03:07
查看更多