Skip to content

Commit

Permalink
Merge pull request #1017 from maartenvanvliet/issues/conform-introspe…
Browse files Browse the repository at this point in the history
…ction-schema-spec

Conform built in types to spec
  • Loading branch information
binaryseed authored Jan 2, 2021
2 parents 4cb5beb + 99dbae1 commit 4a2573e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
9 changes: 5 additions & 4 deletions lib/absinthe/introspection/kind.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ defmodule Absinthe.Introspection.Kind do
__MODULE__
|> Module.split()
|> List.last()
|> Absinthe.Introspection.Kind.upcase()
|> Absinthe.Introspection.Kind.downcase()
|> String.to_existing_atom()
end

defoverridable kind: 0
end
end

def upcase(name) do
def downcase(name) do
Regex.scan(~r{[A-Z]+[a-z]+}, name)
|> List.flatten()
|> Enum.map(&String.upcase/1)
|> Enum.map(&String.downcase/1)
|> Enum.join("_")
end

@callback kind :: binary
@callback kind :: atom
end
39 changes: 26 additions & 13 deletions lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
field :locations, non_null(list_of(non_null(:__directive_location)))
end

enum :__type_kind,
values: [
:schema,
:scalar,
:object,
:interface,
:union,
:enum,
:non_null,
:input_object,
:list
]

enum :__directive_location,
values: [
:query,
Expand All @@ -118,7 +131,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
description "Represents scalars, interfaces, object types, unions, enums in the system"

field :kind,
type: :string,
type: non_null(:__type_kind),
resolve: fn _, %{source: %{__struct__: type}} ->
{:ok, type.kind}
end
Expand Down Expand Up @@ -157,7 +170,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

field :interfaces,
type: list_of(:__type),
type: list_of(non_null(:__type)),
resolve: fn
_, %{schema: schema, source: %{interfaces: interfaces}} ->
interfaces =
Expand All @@ -172,7 +185,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

field :possible_types,
type: list_of(:__type),
type: list_of(non_null(:__type)),
resolve: fn
_, %{schema: schema, source: %{types: types}} ->
possible_types =
Expand All @@ -190,7 +203,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

field :enum_values,
type: list_of(:__enumvalue),
type: list_of(non_null(:__enumvalue)),
args: [
include_deprecated: [
type: :boolean,
Expand All @@ -217,7 +230,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

field :input_fields,
type: list_of(:__inputvalue),
type: list_of(non_null(:__inputvalue)),
resolve: fn
_, %{source: %Absinthe.Type.InputObject{fields: fields}} ->
input_fields =
Expand All @@ -244,15 +257,15 @@ defmodule Absinthe.Type.BuiltIns.Introspection do

object :__field do
field :name,
type: :string,
type: non_null(:string),
resolve: fn _, %{adapter: adapter, source: source} ->
{:ok, adapter.to_external_name(source.name, :field)}
end

field :description, :string

field :args,
type: list_of(:__inputvalue),
type: non_null(list_of(non_null(:__inputvalue))),
resolve: fn _, %{source: %{args: args}} ->
args =
args
Expand All @@ -263,7 +276,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

field :type,
type: :__type,
type: non_null(:__type),
resolve: fn _, %{schema: schema, source: source} ->
result =
case source.type do
Expand All @@ -278,7 +291,7 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

field :is_deprecated,
type: :boolean,
type: non_null(:boolean),
resolve: fn
_, %{source: %{deprecation: nil}} ->
{:ok, false}
Expand All @@ -300,15 +313,15 @@ defmodule Absinthe.Type.BuiltIns.Introspection do

object :__inputvalue, name: "__InputValue" do
field :name,
type: :string,
type: non_null(:string),
resolve: fn _, %{adapter: adapter, source: source} ->
{:ok, adapter.to_external_name(source.name, :field)}
end

field :description, :string

field :type,
type: :__type,
type: non_null(:__type),
resolve: fn _, %{schema: schema, source: %{type: ident}} ->
type = Absinthe.Schema.lookup_type(schema, ident, unwrap: false)
{:ok, type}
Expand All @@ -329,12 +342,12 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
end

object :__enumvalue, name: "__EnumValue" do
field :name, :string
field :name, non_null(:string)

field :description, :string

field :is_deprecated,
type: :boolean,
type: non_null(:boolean),
resolve: fn
_, %{source: %{deprecation: nil}} ->
{:ok, false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule Elixir.Absinthe.Integration.Execution.Introspection.SchemaTypesTest do
"__InputValue",
"__Schema",
"__Type",
"__TypeKind",
"Boolean",
"Business",
"Contact",
Expand Down

0 comments on commit 4a2573e

Please sign in to comment.