Skip to content

Commit

Permalink
Working for all but schema
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryseed committed Dec 25, 2020
1 parent d15664d commit b59d0d4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 18 deletions.
49 changes: 36 additions & 13 deletions lib/absinthe/schema/notation/sdl_render.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,21 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
string(@adapter.to_external_name(input_value.name, :argument)),
": ",
render(input_value.type, type_definitions),
default(input_value.default_value_blueprint)
default(input_value.default_value_blueprint),
directives(input_value.directives, type_definitions)
])
|> description(input_value.description)
end

defp render(%Blueprint.Schema.FieldDefinition{} = field, type_definitions) do
directives = Enum.reject(field.directives, &(&1.name == "deprecated"))

concat([
string(@adapter.to_external_name(field.name, :field)),
arguments(field.arguments, type_definitions),
": ",
render(field.type, type_definitions)
render(field.type, type_definitions),
directives(directives, type_definitions)
])
|> deprecated(field.deprecation)
|> description(field.description)
Expand All @@ -128,8 +132,11 @@ defmodule Absinthe.Schema.Notation.SDL.Render do

defp render(%Blueprint.Schema.InputObjectTypeDefinition{} = input_object_type, type_definitions) do
block(
"input",
string(input_object_type.name),
concat([
"input ",
string(input_object_type.name),
directives(input_object_type.directives, type_definitions)
]),
render_list(input_object_type.fields, type_definitions)
)
|> description(input_object_type.description)
Expand All @@ -149,8 +156,11 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
end)

concat([
"union ",
string(union_type.name),
concat([
"union ",
string(union_type.name),
directives(union_type.directives, type_definitions)
]),
" = ",
join(types, " | ")
])
Expand All @@ -160,29 +170,42 @@ defmodule Absinthe.Schema.Notation.SDL.Render do
defp render(%Blueprint.Schema.InterfaceTypeDefinition{} = interface_type, type_definitions) do
block(
"interface",
string(interface_type.name),
concat([
string(interface_type.name),
directives(interface_type.directives, type_definitions)
]),
render_list(interface_type.fields, type_definitions)
)
|> description(interface_type.description)
end

defp render(%Blueprint.Schema.EnumTypeDefinition{} = enum_type, type_definitions) do
block(
"enum",
string(enum_type.name),
concat([
"enum ",
string(enum_type.name),
directives(enum_type.directives, type_definitions)
]),
render_list(enum_type.values, type_definitions)
)
|> description(enum_type.description)
end

defp render(%Blueprint.Schema.EnumValueDefinition{} = enum_value, _type_definitions) do
string(enum_value.name)
defp render(%Blueprint.Schema.EnumValueDefinition{} = enum_value, type_definitions) do
concat([
string(enum_value.name),
directives(enum_value.directives, type_definitions)
])
|> deprecated(enum_value.deprecation)
|> description(enum_value.description)
end

defp render(%Blueprint.Schema.ScalarTypeDefinition{} = scalar_type, _type_definitions) do
space("scalar", string(scalar_type.name))
defp render(%Blueprint.Schema.ScalarTypeDefinition{} = scalar_type, type_definitions) do
concat([
"scalar ",
string(scalar_type.name),
directives(scalar_type.directives, type_definitions)
])
|> description(scalar_type.description)
end

Expand Down
55 changes: 50 additions & 5 deletions test/absinthe/schema/type_system_directive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,79 @@ defmodule TypeSystemDirectiveTest do
arg :name, non_null(:string)
arg :number, :integer
arg :complex, :complex
on [:object]

repeatable true

on [
:schema,
:scalar,
:object,
:field_definition,
:argument_definition,
:interface,
:union,
:enum,
:enum_value,
:input_object,
:input_field_definition
]
end
end

defmodule SdlTestSchema do
use Absinthe.Schema

alias Absinthe.Blueprint.Schema

@prototype_schema WithTypeSystemDirective

@sdl """
schema {
query: Query
}
type Post @feature(name: "BAR", number: 3, complex: {str: "foo"}) {
interface Animal @feature(name: ":interface") {
legCount: Int!
}
input SearchFilter @feature(name: ":input_object") {
query: String = "default" @feature(name: ":input_field_definition")
}
type Post @feature(name: ":object", number: 3, complex: {str: "foo"}) {
name: String @deprecated(reason: "Bye")
}
scalar SweetScalar @feature(name: ":scalar")
type Query {
post: Post
post: Post @feature(name: ":field_definition")
sweet: SweetScalar
pet: Dog
which: Category
search(filter: SearchFilter @feature(name: ":argument_definition")): SearchResult
}
type Dog implements Animal {
legCount: Int!
name: String!
}
enum Category @feature(name: ":enum") {
THIS
THAT @feature(name: ":enum_value")
}
union SearchResult @feature(name: ":union") = Dog | Post
"""
import_sdl @sdl
def sdl, do: @sdl

def hydrate(%{identifier: :animal}, _) do
{:resolve_type, &__MODULE__.resolve_type/1}
end

def hydrate(_node, _ancestors), do: []

def resolve_type(_), do: false
end

test "Render SDL from blueprint defined with SDL" do
Expand Down

0 comments on commit b59d0d4

Please sign in to comment.