简体   繁体   English

GraphQL:错误:架构查询必须是对象类型但得到:[object Object]

[英]GraphQL: Error: Schema query must be Object Type but got: [object Object]

I'm currently trying to inherit schemas for a rootQuery in order to get more modularity. 我目前正在尝试继承rootQuery的模式,以获得更多的模块化。 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});

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. 当尝试执行以下错误并希望得到一些帮助和见解时,因为我在invoiceSchema.js中导出的​​内容显然是ObjectType而不是对象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... 实际上从这里得到了这个想法,我想知道为什么它不起作用......

Your root query needs to be an instance of GraphQLObjectType , however, rootQuery.js is exporting a plain Object instead. 您的根查询需要是GraphQLObjectType的实例,但是, rootQuery.js正在导出普通的Object。 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';
        } 
    }
  })
};

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. 注意:通常的做法是将所有字段名称(包括查询和变异名称)保存在camelCase中,并使用PascalCase作为类型名称,以帮助区分这两者。

Also, if you are modularizing your schema, you may find it helpful to utilize graphql-tools for generating your schema instead. 此外,如果您正在模块化模式,您可能会发现使用graphql-tools来生成模式会很有帮助。 IMO, it makes your schema more readable and helps avoid some of the more common pitfalls you may face when modularizing a schema. IMO,它使您的架构更具可读性,并有助于避免在模块化模式时可能遇到的一些更常见的陷阱。 The docs have a great example of how to modularize your schema with makeExecutableSchema here . 该文件对如何使用您的模块化架构一个很好的例子makeExecutableSchema 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有错误的GraphQL:Query.example字段类型必须为Output Type,但得到:[object Object] - GraphQL with express error : Query.example field type must be Output Type but got: [object Object] GraphQL交叉引用引发错误:字段类型必须为输出类型,但得到:[对象对象] - GraphQL Cross References throws error: field type must be Output Type but got: [object Object] 在GraphQL架构中将通用对象用作输入类型时出现问题错误:架构必须包含唯一的命名类型 - Problem using a generic object as an input type in a GraphQL schema Error: Schema must contain unique named types Mongo过滤器转换为GraphQL查询| 必须是一个对象,得到_String_ - Mongo filter into GraphQL query | must be an object, got _String_ graphql 中未知对象的模式类型 - schema type for an unknown object in graphql GraphQL模式定义中的“ Got Object”异常 - 'Got Object' exception in GraphQL schema definition Graphql的Document()的参数\\“ obj \\”必须是一个对象,得到了` - Graphql `Parameter \“obj\” to Document() must be an object, got ` GraphQL模式测试:从模式到对象类型 - GraphQL Schema Testing: From Schema to Object Type 具有动态键的对象的 GraphQL 模式类型 - GraphQL schema type for object with dynamic keys 如何在 GraphQL 模式中定义一个空的对象类型? - How to define an empty Object Type in a GraphQL schema?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM