【问题标题】:Passing a token through Query?通过查询传递令牌?
【发布时间】:2018-11-23 09:56:17
【问题描述】:
我有一个 Graph QL 服务器正在运行(Apollo Server 2),它背后的 API 要求每个请求都包含一个令牌。
目前令牌来自 HTTP 请求 Cookie。这很简单,可以工作。当请求进来时,从 header 中获取 cookie 并将其传递给 HTTP 请求以通过解析器发送到 API 服务器。
我想这样做,以便 GraphQL 客户端可以通过 POST 查询本身传递此令牌。
基本上想知道我是否可以定义某种全局 GQL 变量。 “所有查询,这个变量是必需的。”
【问题讨论】:
标签:
graphql
graphql-js
apollo-server
【解决方案1】:
我在 Typescript 中有一个类似的实现,为了实现这样的目标,我定义了一个对象:
const globalInput = {
token: {
type: GraphQLString;
}
}
然后在你的 GraphQLObjectType 中使用它:
const Query = new GraphQLObjectType({
name: 'Query',
fields: () => ({
myObject: {
type: MyTypeObject,
args: { ...globalInput },
resolve: (source: any, args: any) => {
// global input values can be access in args
// ex: args.token
return {}
}
}
})
})
问题是我需要在每个对象类型中扩展它(...globalInput)。
但它确实有效。