forked from novaframework/egql_nova
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathegql_nova_plug.erl
More file actions
27 lines (23 loc) · 912 Bytes
/
egql_nova_plug.erl
File metadata and controls
27 lines (23 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-module(egql_nova_plug).
-export([pre_request/2, post_request/2]).
%% Pre-request plugin for injecting GraphQL context from the Nova request.
%% Add to your sys.config plugins:
%% {pre_request, egql_nova_plug, #{context_fun => fun my_mod:build_ctx/1}}
%%
%% The context_fun receives the full cowboy request map and should return
%% a map that gets merged into the GraphQL execution context.
pre_request(Req, #{context_fun := Fun} = _Options) when is_function(Fun, 1) ->
Ctx = Fun(Req),
Req2 =
case Req of
#{extra_state := ES} when is_map(ES) ->
ExistingCtx = maps:get(context, ES, #{}),
Req#{extra_state => ES#{context => maps:merge(ExistingCtx, Ctx)}};
_ ->
Req#{extra_state => #{context => Ctx}}
end,
{ok, Req2};
pre_request(Req, _Options) ->
{ok, Req}.
post_request(Req, _Options) ->
{ok, Req}.