【问题标题】:GraphQL] The type of [...] must be Output Type but got: undefinedGraphQL] [...] 的类型必须是输出类型,但得到:未定义
【发布时间】:2018-06-30 17:36:32
【问题描述】:

我在引用 GraphQLObjectType 中的其他 GraphQLObjectTypes 时遇到问题。 我不断收到以下错误:

"message": "getBooks.stores 的类型必须是输出类型但得到: 未定义。”

我认为在 books.stores 中使用解析有助于解决问题,但事实并非如此。有人可以帮帮我吗?

代码

var express = require('express');
var graphqlHTTP = require('express-graphql');
var graphql = require('graphql');

var { buildSchema, GraphQLSchema, GraphQLObjectType,GraphQLString,GraphQLList } = require('graphql');

var books = new GraphQLObjectType({
  name:'getBooks',
  fields: {
    isbn: { type: GraphQLString},
    stores: {
      type: stores,
      resolve: function(){
        var store = [];
        store.storeName = "this will contain the name of a shop";
        return store;
      }
    }
  }
});

var stores = new GraphQLObjectType({
  name:'storeList',
  fields: {
    storeName: { type: GraphQLString}
  }
});


var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    books: {
      type: books,
      // `args` describes the arguments that the `user` query accepts
      args: {
        id: { type: new GraphQLList(GraphQLString) }// graphql.GraphQLStringj
      },
      resolve: function (_, {id}) {
        var data = [];
        data.isbn = '32131231';
        return data;
      }
    }
  }
});

var schema = new GraphQLSchema({query: queryType});

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');

【问题讨论】:

    标签: node.js graphql object-type


    【解决方案1】:

    它找不到商店,因为它是在书籍之后定义的。 所以我把商店代码放在书籍之前。

    【讨论】:

      【解决方案2】:

      或者你可以将字段设置为函数类型,以便它可以包装类型关系 例如

      fields: () => ({
      
        //Enter Your Code
      
      })
      

      【讨论】:

      • 添加上,箭头功能允许这样做,因此您不必担心商店类型的顺序。此外,通过这种方式,您可以模块化架构,因此如果您的应用程序变大,在特定文件夹/文件中查找特定架构会更干净。
      猜你喜欢
      • 2019-12-23
      • 2018-12-18
      • 2019-09-19
      • 2017-08-16
      • 2018-06-25
      • 2018-11-18
      • 2016-10-14
      • 2016-06-29
      • 2017-12-12
      相关资源
      最近更新 更多