【发布时间】:2021-08-25 19:50:03
【问题描述】:
所以我正在尝试 GraphQL,但遇到了一个奇怪的问题。返回 data 的同一查询现在返回 null。
我正在使用数组形式的虚拟“数据库”
当我尝试添加新数组和类型 Authors 时出现此问题。我已删除所有内容以返回初始代码,但它仍然不会从数组中返回正确的数据。
schema.js
const graphql = require('graphql');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLSchema
} = graphql;
// dummy data
var booksarr = [
{name: 'Name of the Wind', genre: 'Fantasy' , id: 1},
{name: 'The Final Empire', genre: 'Fantasy' , id: 2},
{name: 'The Long Earth', genre: 'Sci-Fi' , id: 3}
];
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () =>({
id: {type: GraphQLID},
name: {type: GraphQLString},
genre: {type: GraphQLString}
})
});
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
book: {
type: BookType,
args: { id: { type: GraphQLID } },
resolve(parent, args){
//Get data from db / sources
return _.find(booksarr,{id: args.id});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
app.js
const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const schema = require('./schema/schema');
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
app.listen(8080,() =>{
console.log("Now listening on port 8080");
});
【问题讨论】:
标签: node.js graphql express-graphql