Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 5 additions & 36 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,12 @@ name: CI

on:
push:
branches: [main, master]
branches: [main]
pull_request:
branches: [main, master]
branches: [main]

jobs:
ci:
name: OTP ${{ matrix.otp }}
runs-on: ubuntu-latest
strategy:
matrix:
otp: ['27.2', '28.0']
steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
rebar3-version: '3.24'

- name: Restore rebar3 cache
uses: actions/cache@v4
with:
path: |
_build
~/.cache/rebar3
key: ${{ runner.os }}-otp-${{ matrix.otp }}-rebar3-${{ hashFiles('rebar.lock') }}
restore-keys: |
${{ runner.os }}-otp-${{ matrix.otp }}-rebar3-

- name: Compile
run: rebar3 compile

- name: Format check
run: rebar3 fmt --check

- name: XRef
run: rebar3 xref

- name: Dialyzer
run: rebar3 dialyzer
uses: Taure/erlang-ci/.github/workflows/ci.yml@v1
with:
enable-eunit: false
8 changes: 8 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

{shell, [{apps, [egql_nova]}]}.

{xref_ignores, [
{egql_nova, execute, 2},
{egql_nova_controller, graphql, 1},
{egql_nova_controller, graphiql, 1},
{egql_nova_plug, pre_request, 2},
{egql_nova_plug, post_request, 2}
]}.

{project_plugins, [
erlfmt,
rebar3_ex_doc,
Expand Down
11 changes: 6 additions & 5 deletions src/egql_nova.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ execute(Query, Params) ->
-spec execute(binary(), map(), map()) -> map().
execute(Query, Params, Ctx) ->
OpName = maps:get(<<"operationName">>, Params, undefined),
Vars = case maps:get(<<"variables">>, Params, #{}) of
null -> #{};
V when is_map(V) -> V;
_ -> #{}
end,
Vars =
case maps:get(<<"variables">>, Params, #{}) of
null -> #{};
V when is_map(V) -> V;
_ -> #{}
end,
case graphql:parse(Query) of
{ok, AST} ->
try
Expand Down
57 changes: 30 additions & 27 deletions src/egql_nova_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ graphql(#{json := Body} = Req) ->
Result = egql_nova:execute(Query, Body, Ctx),
{json, 200, #{}, Result};
graphql(#{body := RawBody} = Req) ->
Body = jsone:decode(RawBody),
Body = json:decode(RawBody),
Query = maps:get(<<"query">>, Body, <<>>),
Ctx = build_context(Req),
Result = egql_nova:execute(Query, Body, Ctx),
Expand All @@ -22,32 +22,35 @@ graphiql(_Req) ->
{ok, 200, #{<<"content-type">> => <<"text/html">>}, graphiql_html()}.

build_context(Req) ->
ExtraCtx = case Req of
#{extra_state := #{context := C}} when is_map(C) -> C;
_ -> #{}
end,
ExtraCtx =
case Req of
#{extra_state := #{context := C}} when is_map(C) -> C;
_ -> #{}
end,
ExtraCtx.

graphiql_html() ->
<<"<!DOCTYPE html>
<html>
<head>
<title>GraphiQL</title>
<style>
body { height: 100vh; margin: 0; overflow: hidden; }
#graphiql { height: 100vh; }
</style>
<script crossorigin src=\"https://unpkg.com/react@18/umd/react.production.min.js\"></script>
<script crossorigin src=\"https://unpkg.com/react-dom@18/umd/react-dom.production.min.js\"></script>
<link rel=\"stylesheet\" href=\"https://unpkg.com/graphiql/graphiql.min.css\" />
<script crossorigin src=\"https://unpkg.com/graphiql/graphiql.min.js\"></script>
</head>
<body>
<div id=\"graphiql\">Loading...</div>
<script>
const fetcher = GraphiQL.createFetcher({ url: window.location.pathname });
ReactDOM.createRoot(document.getElementById('graphiql'))
.render(React.createElement(GraphiQL, { fetcher }));
</script>
</body>
</html>">>.
<<
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <title>GraphiQL</title>\n"
" <style>\n"
" body { height: 100vh; margin: 0; overflow: hidden; }\n"
" #graphiql { height: 100vh; }\n"
" </style>\n"
" <script crossorigin src=\"https://unpkg.com/react@18/umd/react.production.min.js\"></script>\n"
" <script crossorigin src=\"https://unpkg.com/react-dom@18/umd/react-dom.production.min.js\"></script>\n"
" <link rel=\"stylesheet\" href=\"https://unpkg.com/graphiql/graphiql.min.css\" />\n"
" <script crossorigin src=\"https://unpkg.com/graphiql/graphiql.min.js\"></script>\n"
"</head>\n"
"<body>\n"
" <div id=\"graphiql\">Loading...</div>\n"
" <script>\n"
" const fetcher = GraphiQL.createFetcher({ url: window.location.pathname });\n"
" ReactDOM.createRoot(document.getElementById('graphiql'))\n"
" .render(React.createElement(GraphiQL, { fetcher }));\n"
" </script>\n"
"</body>\n"
"</html>"
>>.
15 changes: 8 additions & 7 deletions src/egql_nova_plug.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

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,
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}.
Expand Down
Loading