![](/img/trans.png)
[英]Problem deploying Apollo Express server to Heroku: "GET query missing."
[英]GET query missing: Implementing GraphQL Using Apollo On an Express Server
我正在关注此处的教程: 在 Express Server 上使用 Apollo 实现 GraphQL并且我在http://localhost:7700/graphql的浏览器中收到错误GET 查询丢失。
首先,我自己输入了所有代码。 当我遇到错误时,我从GitHub下载了代码:kimobrian/GraphQL-Express: An Express Server 使用 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
错误是在http://localhost:7700/graphql的浏览器中缺少 GET 查询。 在 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 });
请查看官方文档以获取更多详细信息。
我通过在 Apollo Server 构造函数中添加两个字段来修复它: playground: true
和introspection: true
:
const apolloServer = new ApolloServer({
schema,
context: (ctx: Context) => ctx,
playground: true,
introspection: true,
});
注意:请注意,应该为您的 graphql 服务器正确设置 cors,以便能够回答请求。
在这里找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.