我试图将信息从我的解析器函数传递到我的中间件,以便我可以在响应中设置一个 cookie。
用例是我想生成一个 Oauth2 授权链接,它允许客户端与第三方开始 Oauth2 流程。我想生成一个“状态”对象,我可以在响应中将其设置为 cookie。
我试过打电话
%{resolution | context: state}
在解析器中,但不幸的是,这似乎并没有传递该状态。
这是我的解析器功能的简化示例
def get_oauth_link(_parent, _args, resolution) do
state = random_string(10)
part_one = "https://github.com/login/oauth/authorize"
part_two = "?client_id=XXX"
part_three = "&redirect_uri=http://localhost:3000/login/callback"
part_four = "&state=" <> state
part_five = "&scope=user:email"
url = part_one <> part_two <> part_three <> part_four <> part_five
# try to add the state to the resolution to
%{resolution | context: state}
{:ok, url}
end
然后在我的架构中
@desc "Get oauth link"
field :oauthlink non_null(:string) do
resolve(&Resolvers.OauthResolver.get_oauth_link/3)
# middleware after resolution to set a cookie with the provided state
middleware fn resolution, _ ->
# Here i want to extract the state generated in
# the resolver and add to the context
IO.inspect(resolution, label: "Inside resolution")
end
end
我希望能够在此处记录的“absinthe_before_send”方法中设置 cookie:https ://hexdocs.pm/absinthe_plug/Absinthe.Plug.html#module-before-send
这样做的最佳方法是什么?上面的方法对我来说似乎很直观,但在后解析中间件中状态不可用。