Split GraphQL as separate context
This commit is contained in:
38
lib/graphql/schema/custom/point.ex
Normal file
38
lib/graphql/schema/custom/point.ex
Normal file
@@ -0,0 +1,38 @@
|
||||
defmodule Mobilizon.GraphQL.Schema.Custom.Point do
|
||||
@moduledoc """
|
||||
The geom scalar type allows Geo.PostGIS.Geometry strings to be passed in and out.
|
||||
Requires `{:geo, "~> 3.0"},` package: https://github.com/elixir-ecto/ecto
|
||||
"""
|
||||
use Absinthe.Schema.Notation
|
||||
|
||||
scalar :point, name: "Point" do
|
||||
description("""
|
||||
The `Point` scalar type represents Point geographic information compliant string data,
|
||||
represented as floats separated by a semi-colon. The geodetic system is WGS 84
|
||||
""")
|
||||
|
||||
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}
|
||||
defp decode(%Absinthe.Blueprint.Input.String{value: value}) do
|
||||
with [_, _] = lonlat <- String.split(value, ";", trim: true),
|
||||
[{lon, ""}, {lat, ""}] <- Enum.map(lonlat, &Float.parse(&1)) do
|
||||
{:ok, %Geo.Point{coordinates: {lon, lat}, srid: 4326}}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
|
||||
defp decode(%Absinthe.Blueprint.Input.Null{}) do
|
||||
{:ok, nil}
|
||||
end
|
||||
|
||||
defp decode(_) do
|
||||
:error
|
||||
end
|
||||
|
||||
defp encode(%Geo.Point{coordinates: {lon, lat}, srid: 4326}), do: "#{lon};#{lat}"
|
||||
end
|
||||
36
lib/graphql/schema/custom/uuid4.ex
Normal file
36
lib/graphql/schema/custom/uuid4.ex
Normal file
@@ -0,0 +1,36 @@
|
||||
defmodule Mobilizon.GraphQL.Schema.Custom.UUID do
|
||||
@moduledoc """
|
||||
The UUID4 scalar type allows UUID compliant strings to be passed in and out.
|
||||
Requires `{ :ecto, ">= 0.0.0" }` package: https://github.com/elixir-ecto/ecto
|
||||
"""
|
||||
use Absinthe.Schema.Notation
|
||||
|
||||
alias Ecto.UUID
|
||||
|
||||
scalar :uuid, name: "UUID" do
|
||||
description("""
|
||||
The `UUID` scalar type represents UUID4 compliant string data, represented as UTF-8
|
||||
character sequences. The UUID4 type is most often used to represent unique
|
||||
human-readable ID strings.
|
||||
""")
|
||||
|
||||
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}
|
||||
defp decode(%Absinthe.Blueprint.Input.String{value: value}) do
|
||||
UUID.cast(value)
|
||||
end
|
||||
|
||||
defp decode(%Absinthe.Blueprint.Input.Null{}) do
|
||||
{:ok, nil}
|
||||
end
|
||||
|
||||
defp decode(_) do
|
||||
:error
|
||||
end
|
||||
|
||||
defp encode(value), do: value
|
||||
end
|
||||
Reference in New Issue
Block a user