Skip to content

Commit

Permalink
Merge pull request #1193 from maartenvanvliet/issues/918
Browse files Browse the repository at this point in the history
Add the specifiedBy type system directive
  • Loading branch information
benwilson512 authored May 13, 2023
2 parents 086393b + 8b004d2 commit d25f16d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Bug Fix: [Validate type references for invalid wrapped types](https://github.com/absinthe-graphql/absinthe/pull/1195)
- Feature: [Add `specifiedBy` type system directive](https://github.com/absinthe-graphql/absinthe/pull/1193)
- Bug Fix: [Object type extensions may be empty](https://github.com/absinthe-graphql/absinthe/pull/1228)
- Bug Fix: [Validate input object not being an Enum](https://github.com/absinthe-graphql/absinthe/pull/1231)
- Bug Fix: [Deduplicate directives when building schema](https://github.com/absinthe-graphql/absinthe/pull/1242)
Expand Down
4 changes: 2 additions & 2 deletions lib/absinthe/phase/document/validation/known_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ defmodule Absinthe.Phase.Document.Validation.KnownDirectives do
defp error_unknown(node) do
%Phase.Error{
phase: __MODULE__,
message: "Unknown directive `#{node.name}'.",
message: "Unknown directive `#{node.name}`.",
locations: [node.source_location]
}
end
Expand All @@ -74,7 +74,7 @@ defmodule Absinthe.Phase.Document.Validation.KnownDirectives do

%Phase.Error{
phase: __MODULE__,
message: "Directive `#{node.name}' may not be used on #{placement_name}.",
message: "Directive `#{node.name}` may not be used on #{placement_name}.",
locations: [node.source_location]
}
end
Expand Down
7 changes: 6 additions & 1 deletion lib/absinthe/schema/notation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ defmodule Absinthe.Schema.Notation do
attrs =
attrs
|> Keyword.put(:identifier, identifier)
|> Keyword.put_new(:name, to_string(identifier))
|> Keyword.put_new(:name, default_name(Schema.DirectiveDefinition, identifier))
|> Keyword.update(:description, nil, &wrap_in_unquote/1)

scoped_def(env, Schema.DirectiveDefinition, identifier, attrs, block)
Expand Down Expand Up @@ -1985,6 +1985,11 @@ defmodule Absinthe.Schema.Notation do
|> Atom.to_string()
end

defp default_name(Schema.DirectiveDefinition, identifier) do
identifier
|> Atom.to_string()
end

defp default_name(_, identifier) do
identifier
|> Atom.to_string()
Expand Down
11 changes: 11 additions & 0 deletions lib/absinthe/schema/prototype/notation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ defmodule Absinthe.Schema.Prototype.Notation do
expand &__MODULE__.expand_deprecate/2
end

directive :specified_by do
description "Exposes a URL that specifies the behavior of this scalar."

repeatable false

arg :url, non_null(:string),
description: "The URL that specifies the behavior of this scalar."

on [:scalar]
end

def pipeline(pipeline) do
pipeline
|> Absinthe.Pipeline.without(Absinthe.Phase.Schema.Validation.QueryTypeMustBeObject)
Expand Down
6 changes: 5 additions & 1 deletion lib/absinthe/type/built_ins/introspection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ defmodule Absinthe.Type.BuiltIns.Introspection do
object :__directive do
description "Represents a directive"

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

field :description, :string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ defmodule Elixir.Absinthe.Integration.Execution.Introspection.DirectivesTest do
"onFragment" => true,
"onOperation" => false,
"isRepeatable" => false
},
%{
"isRepeatable" => false,
"locations" => ["SCALAR"],
"name" => "specifiedBy",
"onField" => false,
"onFragment" => false,
"onOperation" => false,
"args" => [
%{
"name" => "url",
"type" => %{
"kind" => "NON_NULL",
"ofType" => %{"kind" => "SCALAR", "name" => "String"}
}
}
]
}
]
}
Expand Down
10 changes: 10 additions & 0 deletions test/absinthe/introspection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ defmodule Absinthe.IntrospectionTest do
"onField" => true,
"onFragment" => true,
"onOperation" => false
},
%{
"description" =>
"Exposes a URL that specifies the behavior of this scalar.",
"isRepeatable" => false,
"locations" => ["SCALAR"],
"name" => "specifiedBy",
"onField" => false,
"onFragment" => false,
"onOperation" => false
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ defmodule Absinthe.Phase.Document.Validation.KnownDirectivesTest do
phase: @phase,
async: true

alias Absinthe.{Blueprint}
alias Absinthe.Blueprint

def unknown_directive(name, line) do
bad_value(
Blueprint.Directive,
"Unknown directive `#{name}'.",
"Unknown directive `#{name}`.",
line,
name: name
)
Expand All @@ -19,7 +19,7 @@ defmodule Absinthe.Phase.Document.Validation.KnownDirectivesTest do
def misplaced_directive(name, placement, line) do
bad_value(
Blueprint.Directive,
"Directive `#{name}' may not be used on #{placement}.",
"Directive `#{name}` may not be used on #{placement}.",
line,
name: name
)
Expand Down
2 changes: 1 addition & 1 deletion test/absinthe/strict_schema_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Absinthe.StrictSchemaTest do
variables = %{"input" => %{"naiveDatetime" => "2017-01-27T20:31:55"}}

assert_error_message(
"Unknown directive `foo_bar_directive'.",
"Unknown directive `foo_bar_directive`.",
run(document, Absinthe.Fixtures.StrictSchema,
adapter: Absinthe.Adapter.StrictLanguageConventions,
variables: variables
Expand Down

0 comments on commit d25f16d

Please sign in to comment.