缺少 GET 查询:在 Express 服务器上使用 Apollo 实现 GraphQL
GET query missing: Implementing GraphQL Using Apollo On an Express Server
我正在学习这里的教程:Implementing GraphQL Using Apollo On an Express Server and I'm getting the error GET query missing in the browser at http://localhost:7700/graphql。
首先我自己敲了所有的代码。当我遇到错误时,我从GitHub: kimobrian/GraphQL-Express: An Express Server implemented using GraphQL下载了代码以排除我犯错的可能性。但是,我仍然遇到同样的错误。
我认为最好向存储库提供 link 而不是将代码粘贴到此处,因为我使用的是存储库中的相同代码。另外,我不确定哪个文件可能包含问题。
$ 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 });
请检查the official docs了解更多详情。
我通过在 Apollo Server 构造函数中添加两个字段来修复它:playground: true
和 introspection: true
:
const apolloServer = new ApolloServer({
schema,
context: (ctx: Context) => ctx,
playground: true,
introspection: true,
});
注意:请注意,应正确设置 cors 以使您的 graphql 服务器能够响应请求。
找到 here。
我正在学习这里的教程:Implementing GraphQL Using Apollo On an Express Server and I'm getting the error GET query missing in the browser at http://localhost:7700/graphql。
首先我自己敲了所有的代码。当我遇到错误时,我从GitHub: kimobrian/GraphQL-Express: An Express Server implemented using GraphQL下载了代码以排除我犯错的可能性。但是,我仍然遇到同样的错误。
我认为最好向存储库提供 link 而不是将代码粘贴到此处,因为我使用的是存储库中的相同代码。另外,我不确定哪个文件可能包含问题。
$ 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 });
请检查the official docs了解更多详情。
我通过在 Apollo Server 构造函数中添加两个字段来修复它:playground: true
和 introspection: true
:
const apolloServer = new ApolloServer({
schema,
context: (ctx: Context) => ctx,
playground: true,
introspection: true,
});
注意:请注意,应正确设置 cors 以使您的 graphql 服务器能够响应请求。
找到 here。