Zeit-now v2 + apollo-server-express:游乐场错误:无法访问服务器



目前我正在尝试为我的nextjs项目获取一个api。 对于部署,我使用的是 zeit 的 NOW v2(通过"now dev"在本地)。

除了 graphql 服务器之外,一切正常。

在操场上和通过客户端,我收到 404 错误。 查询正在正确执行,但我得到一个错误对象(查询结果在响应字段中;404)。

在操场-gui:相同的问题和操场输入字段中检查它,显示消息"无法访问服务器"。

游乐场初始错误:

{
"error": "Response not successful: Received status code 404"
}

你好查询后的游乐场:

{
"error": {
"data": {
"hello": "Hello world!"
}
}
}

浏览器控制台游乐场:

Error: "Response not successful: Received status code 404"

这是我现在加载的 graphql 服务器:

import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({ typeDefs, resolvers,
introspection: true, playground: true,
subscriptions: {path: '/api'},
});
const app = express();
server.applyMiddleware({ app, path: "/api", cors: true });
module.exports = app;

也尝试了这个例子。同样的问题。

谁能告诉我如何让它正常运行?

我遇到了类似的问题(无法访问服务器)。这是一个授权问题。GraphQL Playground 文档提到了request.credentials设置:

const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
playground: {
settings: {
// So that auth works
// Docs: https://github.com/prisma/graphql-playground
['request.credentials']: 'same-origin',
},
},
subscriptions: {path: '/api'}
});

最新更新