scalar.ex (3658B)
1 defmodule Absinthe.Type.Scalar do 2 @moduledoc """ 3 Represents a primitive value. 4 5 GraphQL responses take the form of a hierarchical tree; the leaves on these 6 trees are scalars. 7 8 Also see `Absinthe.Type.Object`. 9 10 ## Built-In Scalars 11 12 The following built-in scalar types are defined: 13 14 * `:boolean` - Represents `true` or `false`. See the [GraphQL Specification](https://www.graphql.org/learn/schema/#scalar-types). 15 * `:float` - Represents signed double‐precision fractional values as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). See the [GraphQL Specification](https://www.graphql.org/learn/schema/#scalar-types). 16 * `:id` - Represents a unique identifier, often used to refetch an object or as key for a cache. The ID type is serialized in the same way as a String; however, it is not intended to be human‐readable. See the [GraphQL Specification](https://www.graphql.org/learn/schema/#scalar-types). 17 * `:integer` - Represents a signed 32‐bit numeric non‐fractional value, greater than or equal to -2^31 and less than 2^31. Note that Absinthe uses the full word `:integer` to identify this type, but its `name` (used by variables, for instance), is `"Int"`. See the [GraphQL Specification](https://www.graphql.org/learn/schema/#scalar-types). 18 * `:string` - Represents textual data, represented as UTF‐8 character sequences. The String type is most often used by GraphQL to represent free‐form human‐readable text. See the [GraphQL Specification](https://www.graphql.org/learn/schema/#scalar-types). 19 ## Examples 20 21 Supporting a time format in ISOz format, using [Timex](http://hexdocs.pm/timex): 22 23 ``` 24 scalar :time do 25 description "Time (in ISOz format)" 26 parse &Timex.DateFormat.parse(&1, "{ISOz}") 27 serialize &Timex.DateFormat.format!(&1, "{ISOz}") 28 end 29 ``` 30 """ 31 32 use Absinthe.Introspection.TypeKind, :scalar 33 34 alias Absinthe.Type 35 36 @doc false 37 defdelegate functions(), to: Absinthe.Blueprint.Schema.ScalarTypeDefinition 38 39 def serialize(type, value) do 40 Type.function(type, :serialize).(value) 41 end 42 43 def parse(type, value, context \\ %{}) do 44 case Type.function(type, :parse) do 45 parser when is_function(parser, 1) -> 46 parser.(value) 47 48 parser when is_function(parser, 2) -> 49 parser.(value, context) 50 end 51 end 52 53 @typedoc """ 54 A defined scalar type. 55 56 Note new scalars should be defined using `Absinthe.Schema.Notation.scalar`. 57 58 * `:name` - The name of scalar. Should be a TitleCased `binary`. Set Automatically by `Absinthe.Schema.Notation.scalar`. 59 * `:description` - A nice description for introspection. 60 * `:serialize` - A function used to convert a value to a form suitable for JSON serialization 61 * `:parse` - A function used to convert the raw, incoming form of a scalar to the canonical internal format. 62 63 The `:__private__` and `:__reference__` keys are for internal use. 64 """ 65 @type t :: %__MODULE__{ 66 name: binary, 67 description: binary, 68 identifier: atom, 69 __private__: Keyword.t(), 70 definition: module, 71 __reference__: Type.Reference.t() 72 } 73 74 defstruct name: nil, 75 description: nil, 76 identifier: nil, 77 __private__: [], 78 definition: nil, 79 __reference__: nil, 80 parse: nil, 81 serialize: nil, 82 open_ended: false 83 84 @typedoc "The internal, canonical representation of a scalar value" 85 @type value_t :: any 86 87 if System.get_env("DEBUG_INSPECT") do 88 defimpl Inspect do 89 def inspect(scalar, _) do 90 "#<Scalar:#{scalar.name}>" 91 end 92 end 93 end 94 end