improvement: old json -> json_string

improvement: new json does not stringify outpu
This commit is contained in:
Zach Daniel 2021-06-15 11:16:10 -04:00
parent 020ad849ea
commit 5e593ec40c
3 changed files with 43 additions and 4 deletions

View file

@ -2297,7 +2297,7 @@ defmodule AshGraphql.Resource do
else
case type(type) do
nil ->
:json
Application.get_env(:ash_graphql, :json_type) || :json_string
type ->
type
@ -2348,7 +2348,10 @@ defmodule AshGraphql.Resource do
defp do_field_type(Ash.Type.Date, _, _), do: :date
defp do_field_type(Ash.Type.Decimal, _, _), do: :decimal
defp do_field_type(Ash.Type.Integer, _, _), do: :integer
defp do_field_type(Ash.Type.Map, _, _), do: :json
defp do_field_type(Ash.Type.Map, _, _),
do: Application.get_env(:ash_graphql, :json_type) || :json_string
defp do_field_type(Ash.Type.String, _, _), do: :string
defp do_field_type(Ash.Type.Term, _, _), do: :string
defp do_field_type(Ash.Type.UtcDatetime, _, _), do: :naive_datetime

View file

@ -32,6 +32,5 @@ defmodule AshGraphql.Types.JSON do
:error
end
def encode(nil), do: nil
def encode(value), do: Jason.encode!(value)
def encode(value), do: value
end

37
lib/types/json_string.ex Normal file
View file

@ -0,0 +1,37 @@
defmodule AshGraphql.Types.JSONString do
@moduledoc """
The Json scalar type allows arbitrary JSON values to be passed in and out.
"""
use Absinthe.Schema.Notation
scalar :json_string, name: "JsonString" do
description("""
The `Json` scalar type represents arbitrary json string data, represented as UTF-8
character sequences. The Json type is most often used to represent a free-form
human-readable json string.
""")
serialize(&encode/1)
parse(&decode/1)
end
@spec decode(Absinthe.Blueprint.Input.String.t()) :: {:ok, term()} | :error
@spec decode(Absinthe.Blueprint.Input.Null.t()) :: {:ok, nil}
def decode(%Absinthe.Blueprint.Input.String{value: value}) do
case Jason.decode(value) do
{:ok, result} -> {:ok, result}
_ -> :error
end
end
def decode(%Absinthe.Blueprint.Input.Null{}) do
{:ok, nil}
end
def decode(_) do
:error
end
def encode(nil), do: nil
def encode(value), do: Jason.encode!(value)
end