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

Apply type system directives in macro based schemas #1140

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
2 changes: 2 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ locals_without_parens = [
config: 1,
deprecate: 1,
description: 1,
directive: 1,
directive: 2,
directive: 3,
enum: 2,
enum: 3,
Expand Down
10 changes: 10 additions & 0 deletions lib/absinthe/blueprint/input/value.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ defmodule Absinthe.Blueprint.Input.Value do
def valid?(%{normalized: %Absinthe.Blueprint.Input.Null{}}), do: true
def valid?(%{normalized: nil}), do: false
def valid?(%{normalized: _}), do: true

def build(value) do
%Absinthe.Blueprint.Input.Value{
data: value,
normalized: nil,
raw: %Absinthe.Blueprint.Input.RawValue{
content: Absinthe.Blueprint.Input.parse(value)
}
}
end
end
5 changes: 5 additions & 0 deletions lib/absinthe/blueprint/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ defmodule Absinthe.Blueprint.Schema do
build_types(rest, [field | stack], buff)
end

defp build_types([{:directive, trigger} | rest], [field | stack], buff) do
field = Map.update!(field, :directives, &[trigger | &1])
build_types(rest, [field | stack], buff)
end

defp build_types([{:trigger, trigger} | rest], [field | stack], buff) do
field = Map.update!(field, :triggers, &[trigger | &1])
build_types(rest, [field | stack], buff)
Expand Down
3 changes: 3 additions & 0 deletions lib/absinthe/middleware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ defmodule Absinthe.Middleware do
[{:ref, Absinthe.Type.BuiltIns.Introspection, _}] ->
expanded

[{:ref, Absinthe.Phase.Schema.DeprecatedDirectiveFields, _}] ->
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really happy with this, but can be removed when these fields are no longer necessary.

expanded

_ ->
schema.middleware(expanded, field, object)
end
Expand Down
58 changes: 58 additions & 0 deletions lib/absinthe/phase/schema/deprecated_directive_fields.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Absinthe.Phase.Schema.DeprecatedDirectiveFields do
@moduledoc false
# The spec of Oct 2015 has the onOperation, onFragment and onField
# fields for directives (https://spec.graphql.org/October2015/#sec-Schema-Introspection)
# See https://github.com/graphql/graphql-spec/pull/152 for the rationale.
# These fields are deprecated and can be removed in the future.
alias Absinthe.Blueprint

use Absinthe.Schema.Notation

@behaviour Absinthe.Phase

def run(input, _options \\ []) do
blueprint = Blueprint.prewalk(input, &handle_node/1)

{:ok, blueprint}
end

defp handle_node(%Blueprint.Schema.ObjectTypeDefinition{identifier: :__directive} = node) do
[types] = __MODULE__.__absinthe_blueprint__().schema_definitions

new_node = Enum.find(types.type_definitions, &(&1.identifier == :deprecated_directive_fields))

fields = node.fields ++ new_node.fields

%{node | fields: fields}
end

defp handle_node(node) do
node
end

object :deprecated_directive_fields do
field :on_operation, :boolean do
deprecate "Check `locations` field for enum value OPERATION"

resolve fn _, %{source: source} ->
{:ok, Enum.any?(source.locations, &Enum.member?([:query, :mutation, :subscription], &1))}
end
end

field :on_fragment, :boolean do
deprecate "Check `locations` field for enum value FRAGMENT_SPREAD"

resolve fn _, %{source: source} ->
{:ok, Enum.member?(source.locations, :fragment_spread)}
end
end

field :on_field, :boolean do
deprecate "Check `locations` field for enum value FIELD"

resolve fn _, %{source: source} ->
{:ok, Enum.member?(source.locations, :field)}
end
end
end
end
1 change: 1 addition & 0 deletions lib/absinthe/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ defmodule Absinthe.Pipeline do

[
Phase.Schema.TypeImports,
Phase.Schema.DeprecatedDirectiveFields,
Phase.Schema.ApplyDeclaration,
Phase.Schema.Introspection,
{Phase.Schema.Hydrate, options},
Expand Down
Loading