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

Add the specifiedBy type system directive #1193

Merged
merged 7 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 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)
- Breaking Bugfix: [Validate repeatable directives on schemas](https://github.com/absinthe-graphql/absinthe/pull/1179)
- Bug Fix: Adds **optional fix** for non compliant built-in scalar Int type. `use Absinthe.Schema, use_spec_compliant_int_scalar: true` in your schema to use the fixed Int type. It is also advisable to upgrade for custom types if you are leveraging the use of integers outside the GraphQl standard. [#1131](https://github.com/absinthe-graphql/absinthe/pull/1131).
- Feature: [Support error tuples when scalar parsing fails](https://github.com/absinthe-graphql/absinthe/pull/1187)
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 @@ -64,7 +64,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 @@ -75,7 +75,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 @@ -1600,7 +1600,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 @@ -1979,6 +1979,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)}
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch.

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