在我想使用的 GraphQL Playground 中设置测试 header 导致 "Server cannot be reached"
Setting a test header in GraphQL Playground that I want to use causes "Server cannot be reached"
所以我创建了一堆突变和查询并将它们拼接在一起,并且希望在组合中引入身份验证。我添加了一个 HTTP Header "x-token" 来保存我的 sign-in 令牌,以便能够删除他们的工作或用户本身等内容。
const getMe = async req => {
const token = req.headers['x-token'];
if (token) {
try {
return await jwt.verify(token, "notSoSecret");
} catch (e) {
throw new AuthenticationError(
'Your session expired. Sign in again.',
);
}
}
};
const server = new ApolloServer({
typeDefs: schema,
resolvers,
formatError: error => {
// remove the internal sequelize error message
// leave only the important validation error
const message = error.message
.replace('SequelizeValidationError: ', '')
.replace('Validation error: ', '');
return {
...error,
message,
};
},
context: async ({ req }) => {
const me = await getMe(req);
return {
models,
me,
secret: "notSoSecret",
}
},
path: "/graphql"
});
server.applyMiddleware({ app });
sequelize.sync().then(async () => {
createUsersWithJob();
});
app.get("/playground", graphiql({ endpoint: "/graphql" }));
const handler = serverless(app);
export { handler };
const createUsersWithJob = ... //creates seed data
所以当我添加令牌并查看我的命令行控制台时,我实际上看到我正在设置我想要的 header,但它一遍又一遍地循环并且不会停止.游乐场也出现错误 "Server cannot be reached"
{
"error": "Response not successful: Received status code 400"
}
和 运行 deleteUser 突变不起作用,或者任何其他突变和查询都不起作用,直到我删除我在操场上设置的 HTTP Header。
还有一个次要问题,即此根文件中的所有内容都会运行两次,但目前对我来说还没有概述的 header 问题那么严重。
如果有人对此有任何见解,我很想知道更多。提前致谢。
编辑:只是快速编辑一下,说当我对 pre-existing 用户进行硬编码时它工作正常。
我费了很大劲才让 GraphQL Playground 的 React 版本在一个非常简单的 html 设置中工作,但我想出了一些可能对你也有帮助的东西(祈祷)。
我在 GraphQLPlayground.init
调用的配置中添加了一个 headers
部分,如下所示:
const root = document.getElementById('root');
GraphQLPlayground.init(root, {
endpoint: "/graphql",
headers: {
"Authorization": "Bearer " + token
}
})
我有一个 ID 为 root
的元素,因为它嵌入在 HTML.
中
虽然我不确定这是否对您有帮助,因为我刚从您的代码示例中注意到您正在调用 graphiql
,这是与 GraphQL Playground 不同的 GraphQL 客户端..
GraphIQL:https://github.com/skevy/graphiql-app
GraphQL 游乐场:https://github.com/prisma/graphql-playground
所以我创建了一堆突变和查询并将它们拼接在一起,并且希望在组合中引入身份验证。我添加了一个 HTTP Header "x-token" 来保存我的 sign-in 令牌,以便能够删除他们的工作或用户本身等内容。
const getMe = async req => {
const token = req.headers['x-token'];
if (token) {
try {
return await jwt.verify(token, "notSoSecret");
} catch (e) {
throw new AuthenticationError(
'Your session expired. Sign in again.',
);
}
}
};
const server = new ApolloServer({
typeDefs: schema,
resolvers,
formatError: error => {
// remove the internal sequelize error message
// leave only the important validation error
const message = error.message
.replace('SequelizeValidationError: ', '')
.replace('Validation error: ', '');
return {
...error,
message,
};
},
context: async ({ req }) => {
const me = await getMe(req);
return {
models,
me,
secret: "notSoSecret",
}
},
path: "/graphql"
});
server.applyMiddleware({ app });
sequelize.sync().then(async () => {
createUsersWithJob();
});
app.get("/playground", graphiql({ endpoint: "/graphql" }));
const handler = serverless(app);
export { handler };
const createUsersWithJob = ... //creates seed data
所以当我添加令牌并查看我的命令行控制台时,我实际上看到我正在设置我想要的 header,但它一遍又一遍地循环并且不会停止.游乐场也出现错误 "Server cannot be reached"
{
"error": "Response not successful: Received status code 400"
}
和 运行 deleteUser 突变不起作用,或者任何其他突变和查询都不起作用,直到我删除我在操场上设置的 HTTP Header。
还有一个次要问题,即此根文件中的所有内容都会运行两次,但目前对我来说还没有概述的 header 问题那么严重。
如果有人对此有任何见解,我很想知道更多。提前致谢。
编辑:只是快速编辑一下,说当我对 pre-existing 用户进行硬编码时它工作正常。
我费了很大劲才让 GraphQL Playground 的 React 版本在一个非常简单的 html 设置中工作,但我想出了一些可能对你也有帮助的东西(祈祷)。
我在 GraphQLPlayground.init
调用的配置中添加了一个 headers
部分,如下所示:
const root = document.getElementById('root');
GraphQLPlayground.init(root, {
endpoint: "/graphql",
headers: {
"Authorization": "Bearer " + token
}
})
我有一个 ID 为 root
的元素,因为它嵌入在 HTML.
虽然我不确定这是否对您有帮助,因为我刚从您的代码示例中注意到您正在调用 graphiql
,这是与 GraphQL Playground 不同的 GraphQL 客户端..
GraphIQL:https://github.com/skevy/graphiql-app
GraphQL 游乐场:https://github.com/prisma/graphql-playground