[英]GraphQL with express error : Query.example field type must be Output Type but got: [object Object]
[英]GraphQL: Error: Schema query must be Object Type but got: [object Object]
我目前正在嘗試繼承rootQuery的模式,以獲得更多的模塊化。 設置目前如下所示:
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
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
import query from './rootQuery';
import {GraphQLSchema} from 'graphql';
export default new GraphQLSchema({query});
當嘗試執行以下錯誤並希望得到一些幫助和見解時,因為我在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...
實際上從這里得到了這個想法,我想知道為什么它不起作用......
您的根查詢需要是GraphQLObjectType
的實例,但是, rootQuery.js
正在導出普通的Object。 您需要將導出更改為以下內容:
export default new GraphQLObjectType({
name: 'RootQuery',
fields: () => ({
invoice: {
type: Invoice,
resolve(parentValue, args){
return 'Hello world';
}
}
})
};
注意:通常的做法是將所有字段名稱(包括查詢和變異名稱)保存在camelCase中,並使用PascalCase作為類型名稱,以幫助區分這兩者。
此外,如果您正在模塊化模式,您可能會發現使用graphql-tools
來生成模式會很有幫助。 IMO,它使您的架構更具可讀性,並有助於避免在模塊化模式時可能遇到的一些更常見的陷阱。 該文件對如何使用您的模塊化架構一個很好的例子makeExecutableSchema
這里 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.