我在使用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');
答案 0 :(得分:0)
它无法找到商店,因为它是在书籍之后定义的。 所以我把商店代码放在书本之前。
答案 1 :(得分:0)
或者您可以将字段设置为函数类型,以便它可以包装类型关系 例如
#if TARGET_OS_IOS
// iOS supported
#elif TARGET_OS_OSX
// macOS supported
#endif