Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conform built in types to spec #1017

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is enumerated now, and not just a string, I wonder if this can be simplified (ie: not use all that module / string manipulation)

It seems like the goal is just to have a function kind that returns an atom... We could get rid of the macro altogether:

defmodule Absinthe.Type.Object do
  @behaviour Absinthe.Introspection.Kind
  def kind, do: :object
end

defmodule Absinthe.Introspection.Kind do
  @type introspection_kind :: :enum | :object # ...
  @callback kind() :: introspection_kind()
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll do this separately...

Copy link
Contributor

@binaryseed binaryseed Jan 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually while making this change, I found some further issues with our __TypeKind!

Further corrections here: #1019

+1 to being explicit!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, good catch!

|> 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