【发布时间】:2021-04-16 16:26:24
【问题描述】:
我正在尝试通过 Express 设置一个 GraphQL 方案,当我使用 Graphiql 加载它时,它不会选择架构。当我运行查询时,它说我必须提供一个查询字符串。我的代码如下。谁能发现我做错了什么?控制台中没有错误,并且文档资源管理器加载了一个永无止境的旋转加载循环。我是否以某种方式创建了一个无限循环?
这是架构:
const graphql = require('graphql');
const _= require('lodash');
const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;
//dummy data
var books = [
{name: 'book1', genre: 'genre1', id: '1'},
{name: 'book2', genre: 'genre2', id: '2'},
{name: 'book3', genre: 'genre3', id: '3'}
]
const BookType = new GraphQLObjectType({
name: 'Book',
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString},
genre: {type: GraphQLString}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
book: {
type: BookType,
args: {id: {type: GraphQLString}},
resolve(parent, args) {
//code to get data from db / other source
return _.find(books, {id: args.id});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
})
这是服务器:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
require("dotenv").config();
const axios = require("axios");
const {graphqlHTTP} = require('express-graphql')
const schema = require('./schema/schema')
// set up our middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("server/public"));
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true,
}));
//loading on port 5000
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log("server running on: ", PORT);
});
【问题讨论】:
-
当我将 graphiql 设置为 false
{ "errors": [ { "message": "Syntax Error: Unexpected <EOF>.", "locations": [ { "line": 3, "column": 1 } ] } ] }时出现以下错误,这可能是因为它没有找到我的字符串,因为它没有找到方案查询。为什么??我的节点控制台中绝对没有错误。 -
我在顶部添加了 Graphiql 加载时发生的情况的屏幕截图。我再次在我的快速服务器控制台中没有错误。我在这里完全不知所措