GraphQL] [...] 的类型必须是输出类型但得到:未定义

Elv*_*ra 3 node.js object-type graphql

我在引用 GraphQLObjectType 中的其他 GraphQLObjectType 时遇到了麻烦。我不断收到以下错误:

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

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

代码

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');
Run Code Online (Sandbox Code Playgroud)

小智 5

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

fields: () => ({

  //Enter Your Code

})
Run Code Online (Sandbox Code Playgroud)


Elv*_*ra 1

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