Skip to content

Commit

Permalink
Add naive GraphQL encoder function
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryseed committed Apr 4, 2018
1 parent 88e5461 commit 5571ff7
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
{:ok, inspect(serializer.(value))}

map when is_map(map) ->
externalized = to_external(value)
{:ok, inspect(externalized)}
{:ok, encode(value)}

_ ->
{:ok, to_string(value)}
Expand All @@ -297,14 +296,25 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end
end

def to_external(map) when is_map(map) do
Enum.into(map, %{}, fn {key, value} ->
{Absinthe.Adapter.LanguageConventions.to_external_name(to_string(key), :argument),
to_external(value)}
end)
def encode(true), do: "true"
def encode(false), do: "false"
def encode(num) when is_number(num), do: "#{num}"
def encode(bin) when is_binary(bin), do: ~s("#{bin}")

def encode(map) when is_map(map) do
encoded = map |> Enum.map(&encode/1) |> Enum.join(", ")
"{#{encoded}}"
end

def encode(list) when is_list(list) do
encoded = list |> Enum.map(&encode/1) |> Enum.join(", ")
"[#{encoded}]"
end

def to_external(value), do: value
def encode({key, value}) do
encoded_key = Absinthe.Utils.camelize(to_string(key), lower: true)
"#{encoded_key}: #{encode(value)}"
end

object :__enumvalue, name: "__EnumValue" do
field :name, :string
Expand Down

0 comments on commit 5571ff7

Please sign in to comment.