Get Query Missing:在Express服务器上使用Apollo实现GraphQL [英] GET query missing: Implementing GraphQL Using Apollo On an Express Server
本文介绍了Get Query Missing:在Express服务器上使用Apollo实现GraphQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在遵循此处的教程:Implementing GraphQL Using Apollo On an Express Server,但在浏览器中的http://localhost:7700/graphql处收到错误Get Query Missing。
首先,我自己输入了所有的代码。当我遇到错误时,我从GitHub: kimobrian/GraphQL-Express: An Express Server implemented using GraphQL下载了代码,以消除我犯错误的可能性。但是,我仍然收到相同的错误。我认为提供repo的链接比在这里粘贴代码更好,因为我使用的是repo中的相同代码。此外,我不确定哪个文件可能包含该问题。
$ npm start
> tutorial-server@1.0.0 start kimobrian/GraphQL-Express.git
> nodemon ./server.js --exec babel-node -e js
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node ./server.js`
GraphQL Server is now running on http://localhost:7700
错误Get Query Missing在浏览器中的http://localhost:7700/graphql。在Firefox和Chromium中也是如此。
更新:我发现的唯一有相关信息的问题是:nodejs with Graphql。建议的解决方案是
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));
然而,这正是我已经拥有的代码(来自本教程)。这是我的全部server.js
:
import express from 'express';
import cors from 'cors';
import {
graphqlExpress,
graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
import { schema } from './src/schema';
const PORT = 7700;
const server = express();
server.use('*', cors({ origin: 'http://localhost:7800' }));
server.use('/graphql', bodyParser.json(), graphqlExpress({
schema
}));
server.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql'
}));
server.listen(PORT, () =>
console.log(`GraphQL Server is now running on http://localhost:${PORT}`)
);
api
自该教程编写以来,推荐答案和包名本身都已更改(包现在为apollo-server-express
)。您不再需要导入cors
或body-parser
。最简单的入门方法是实际使用Apollo-SERVER:
const server = new ApolloServer({ typeDefs, resolvers });
const port = 7700;
server.listen({ port });
如果您还想自己应用其他中间件,可以使用apollo-server-express
:
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
server.applyMiddleware({ app });
const port = 7700;
app.listen({ port });
有关更多详细信息,请查看the official docs。
这篇关于Get Query Missing:在Express服务器上使用Apollo实现GraphQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文