-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ordered result example #1
Open
benwilson512
wants to merge
3
commits into
master
Choose a base branch
from
ordered
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
run.config: | ||
engine: elixir | ||
|
||
engine.config: | ||
runtime: elixir-1.5 | ||
|
||
engine.config: | ||
erlang_runtime: erlang-20.1 | ||
|
||
dev_packages: | ||
- inotify-tools | ||
|
||
data.db: | ||
image: nanobox/postgresql:9.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
defmodule BlogWeb.JSON do | ||
def encode!(data, _) do | ||
:jiffy.encode(data, [:use_nil]) | ||
end | ||
def decode(string) do | ||
{:ok, :jiffy.decode(string, [:return_maps])} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
defmodule BlogWeb.OrdGraphQLResult do | ||
|
||
@moduledoc false | ||
|
||
# Produces data fit for external encoding from annotated value tree | ||
|
||
alias Absinthe.{Blueprint, Phase, Type} | ||
use Absinthe.Phase | ||
|
||
@spec run(Blueprint.t | Phase.Error.t, Keyword.t) :: {:ok, map} | ||
def run(%Blueprint{} = bp, _options \\ []) do | ||
result = Map.merge(bp.result, process(bp)) | ||
{:ok, %{bp | result: result}} | ||
end | ||
|
||
defp process(blueprint) do | ||
result = case blueprint.execution do | ||
%{validation_errors: [], result: result} -> | ||
{:ok, data(result, [])} | ||
%{validation_errors: errors} -> | ||
{:validation_failed, errors} | ||
end | ||
format_result(result) | ||
end | ||
|
||
defp format_result(:execution_failed) do | ||
%{data: nil} | ||
end | ||
defp format_result({:ok, {data, []}}) do | ||
%{data: data} | ||
end | ||
defp format_result({:ok, {data, errors}}) do | ||
errors = errors |> Enum.uniq |> Enum.map(&format_error/1) | ||
%{data: data, errors: errors} | ||
end | ||
defp format_result({:validation_failed, errors}) do | ||
errors = errors |> Enum.uniq |> Enum.map(&format_error/1) | ||
%{errors: errors} | ||
end | ||
defp format_result({:parse_failed, error}) do | ||
%{errors: [format_error(error)]} | ||
end | ||
|
||
defp data(%{errors: [_|_] = field_errors}, errors), do: {nil, field_errors ++ errors} | ||
|
||
# Leaf | ||
defp data(%{value: nil}, errors), do: {nil, errors} | ||
defp data(%{value: value, emitter: emitter}, errors) do | ||
value = | ||
case Type.unwrap(emitter.schema_node.type) do | ||
%Type.Scalar{} = schema_node -> | ||
Type.Scalar.serialize(schema_node, value) | ||
%Type.Enum{} = schema_node -> | ||
Type.Enum.serialize(schema_node, value) | ||
end | ||
{value, errors} | ||
end | ||
|
||
# Object | ||
defp data(%{fields: fields}, errors), do: field_data(fields, errors) | ||
|
||
# List | ||
defp data(%{values: values}, errors), do: list_data(values, errors) | ||
|
||
defp list_data(fields, errors, acc \\ []) | ||
defp list_data([], errors, acc), do: {:lists.reverse(acc), errors} | ||
defp list_data([%{errors: errs} = field | fields], errors, acc) do | ||
{value, errors} = data(field, errors) | ||
list_data(fields, errs ++ errors, [value | acc]) | ||
end | ||
|
||
defp field_data(fields, errors, acc \\ []) | ||
defp field_data([], errors, acc), do: {{:lists.reverse(acc)}, errors} | ||
defp field_data([%Absinthe.Resolution{} = res | _], _errors, _acc) do | ||
raise """ | ||
Found unresolved resolution struct! | ||
|
||
You probably forgot to run the resolution phase again. | ||
|
||
#{inspect res} | ||
""" | ||
end | ||
defp field_data([field | fields], errors, acc) do | ||
{value, errors} = data(field, errors) | ||
field_data(fields, errors, [{field_name(field.emitter), value} | acc]) | ||
end | ||
|
||
defp field_name(%{alias: nil, name: name}), do: name | ||
defp field_name(%{alias: name}), do: name | ||
defp field_name(%{name: name}), do: name | ||
|
||
defp format_error(%Phase.Error{locations: []} = error) do | ||
error_object = %{message: error.message} | ||
Map.merge(error.extra, error_object) | ||
end | ||
defp format_error(%Phase.Error{} = error) do | ||
error_object = %{ | ||
message: error.message, | ||
locations: Enum.flat_map(error.locations, &format_location/1), | ||
} | ||
error_object = case error.path do | ||
[] -> error_object | ||
path -> Map.put(error_object, :path, path) | ||
end | ||
Map.merge(Map.new(error.extra), error_object) | ||
end | ||
|
||
defp format_location(%{line: line, column: col}) do | ||
[%{line: line || 0, column: col || 0}] | ||
end | ||
defp format_location(_), do: [] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,21 @@ defmodule BlogWeb.Router do | |
pipe_through :api | ||
|
||
forward "/graphiql", Absinthe.Plug.GraphiQL, | ||
schema: BlogWeb.Schema | ||
schema: BlogWeb.Schema, | ||
json_codec: BlogWeb.JSON, | ||
pipeline: {__MODULE__, :absinthe_pipeline} | ||
|
||
forward "/", Absinthe.Plug, | ||
schema: BlogWeb.Schema | ||
schema: BlogWeb.Schema, | ||
json_codec: BlogWeb.JSON, | ||
pipeline: {__MODULE__, :absinthe_pipeline} | ||
end | ||
|
||
def absinthe_pipeline(config, pipeline_opts) do | ||
pipeline_opts = Keyword.put(pipeline_opts, :result_phase, BlogWeb.OrdGraphQLResult) | ||
config.schema_mod | ||
|> Absinthe.Pipeline.for_document(pipeline_opts) | ||
|> Absinthe.Pipeline.replace(Absinthe.Phase.Document.Result, BlogWeb.OrdGraphQLResult) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Configuration here is just slightly more complicated than we want. |
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
defmodule BlogWebTest do | ||
use BlogWeb.ConnCase | ||
|
||
@query """ | ||
{ | ||
posts { | ||
id, title | ||
} | ||
} | ||
""" | ||
test "ordered result" do | ||
conn = build_conn() | ||
conn = get conn, "/api", query: @query | ||
assert conn.resp_body == ~s({"data":{"posts":[{"id":"1","title":"Test Post"}]}}) | ||
end | ||
|
||
@query """ | ||
{ | ||
posts { | ||
title, id | ||
} | ||
} | ||
""" | ||
test "ordered result works in reverse" do | ||
conn = build_conn() | ||
conn = get conn, "/api", query: @query | ||
assert conn.resp_body == ~s({"data":{"posts":[{"title":"Test Post","id":"1"}]}}) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line here in the normal result phase is:
This is the only line in the entire file that is different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main challenge here is that for large results this function gets called a LOT, and so I want to avoid dynamic lookup. One idea is to use macros: