【问题标题】:GraphQL with express error : Query.example field type must be Output Type but got: [object Object]带有快速错误的 GraphQL:Query.example 字段类型必须是输出类型,但得到:[object Object]
【发布时间】:2017-01-22 14:38:25
【问题描述】:

我正在尝试使用 graphql 运行 express 服务器,但出现以下错误:

错误:Query.example 字段类型必须是输出类型但得到:[object Object]。

路由中使用的express-graphql:

app.use('/graphql', graphqlHTTP(req => ({
 schema,
 pretty: true
})));

架构如下所示:

export default new GraphQLSchema({
 query: new GraphQLObjectType({
    name: 'Query',
    fields: queries
 })
});

查询只有一个从这里导入的查询:

import {
 GraphQLList,
 GraphQLID,
 GraphQLNonNull
} from 'graphql';

import exampleScheme from '../models/schemes/example';
import {example as exampleData}  from '../models/jsons'

export default {
 type: exampleScheme,
 resolve (root, params, options) {
     return exampleData;
 }
};

模式 (exampleScheme) 和数据 (exampleScheme) 正在处理另一个项目,所以我认为它们不是问题。

有什么想法吗? “输出类型”是什么意思?

【问题讨论】:

    标签: node.js graphql


    【解决方案1】:

    如果

    export default {
     type: exampleScheme,
     resolve (root, params, options) {
         return exampleData;
     }
    };
    

    是您的查询,那么您弄错了, 查询的字段,因此您的示例中的查询应该如下所示:

    export default {
     helloString: {
         type: GraphQLString,
         resolve (root, params, options) {
             return "Hello World!";
         }
     }
     ... <Next query>
    };
    

    无论如何,错误是“类型”不是有效的 GraphQLType。 那么查询将如下所示

    query {
       testString
    }
    

    将导致:

    {
        testString: "Hello World!"
    }
    

    如果意思是 exampleScheme 是其他对象, 请确保您使用以下方式创建它:

    new GraphQLObjectType({ .... Options });
    

    希望对您有所帮助! :)

    【讨论】:

    • 感谢 HagaiCo,因为我从另一个项目导入了方案,示例方案是“GraphQLSchema”而不是“GraphQLObjectType”。
    猜你喜欢
    • 2018-03-28
    • 2017-03-07
    • 2018-01-30
    • 2017-09-03
    • 2016-06-29
    • 2017-12-12
    • 2017-09-21
    • 2018-10-14
    • 2023-01-10
    相关资源
    最近更新 更多