node.js - GET 查询缺失 : Implementing GraphQL Using Apollo On an Express Server

标签 node.js express graphql apollo apollo-server

我正在关注这里的教程:Implementing GraphQL Using Apollo On an Express Server我在浏览器中收到错误 GET 查询丢失 http://localhost:7700/graphql .

首先我自己输入了所有代码。当我遇到错误时,我从GitHub: kimobrian/GraphQL-Express: An Express Server implemented using GraphQL下载了代码以消除我犯错误的可能性。但是,我仍然遇到同样的错误。

我认为最好提供存储库的链接,而不是在此处粘贴代码,因为我使用存储库中的相同代码。另外,我不确定哪个文件可能包含问题。

$ 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 查询,地址为 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}`)
);

最佳答案

我通过在 Apollo Server 构造函数中添加两个字段来修复它:playground: trueintrospection: true:

const apolloServer = new ApolloServer({
  schema,
  context: (ctx: Context) => ctx,
  playground: true,
  introspection: true,
});

注意:请注意,应正确设置 cors,以便您的 graphql 服务器能够应答请求。

找到here .

关于node.js - GET 查询缺失 : Implementing GraphQL Using Apollo On an Express Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54265160/

相关文章:

javascript - 使用 express 记录所有 GraphQL 响应

javascript - 如何使用 Prisma API 生成 JWT token ?

graphql - 如何使用 apollo 服务器从变量运行 graphql 查询

graphql - 如何使用模式语言创建泛型?

node.js - Amazon SES - SMTP 错误状态代码 403 : SignatureDoesNotMatch

node.js - 无法使用aws-sdk在nodejs中上传图片

node.js - Socket.io 中的连接不稳定

node.js - 如何从 Express API 获取数据并将其显示到 React 应用程序?

node.js - NodeJs Express 设置 View 以查看呈现为 html 的文件夹内容

node.js - 强制 npm install 为其他平台安装可选依赖项