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

Ensure input/output types are correctly placed for SDL schemas #1142

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
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ defmodule Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced do

defp handle_schemas(%Blueprint.Schema.SchemaDefinition{} = schema) do
types = Map.new(schema.type_definitions, &{&1.identifier, &1})
schema = Blueprint.prewalk(schema, &validate_type(&1, types))
schema = Blueprint.prewalk(schema, &validate_type(&1, types, schema))
{:halt, schema}
end

defp handle_schemas(obj) do
obj
end

defp validate_type(%Blueprint.Schema.InputValueDefinition{} = arg, types) do
arg_type = Map.get(types, Blueprint.TypeReference.unwrap(arg.type))
defp validate_type(%Blueprint.Schema.InputValueDefinition{} = arg, types, schema) do
type =
Blueprint.TypeReference.unwrap(arg.type)
|> Blueprint.TypeReference.to_type(schema)

arg_type = Map.get(types, type)

if arg_type && wrong_type?(Blueprint.Schema.InputValueDefinition, arg_type) do
detail = %{
Expand All @@ -33,11 +37,15 @@ defmodule Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced do
end
end

defp validate_type(%struct{fields: fields} = type, types) do
defp validate_type(%struct{fields: fields} = type, types, schema) do
fields =
Enum.map(fields, fn
%{type: _} = field ->
field_type = Map.get(types, Blueprint.TypeReference.unwrap(field.type))
type =
Blueprint.TypeReference.unwrap(field.type)
|> Blueprint.TypeReference.to_type(schema)

field_type = Map.get(types, type)

if field_type && wrong_type?(struct, field_type) do
detail = %{
Expand All @@ -59,7 +67,7 @@ defmodule Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced do
%{type | fields: fields}
end

defp validate_type(type, _types) do
defp validate_type(type, _types, _schema) do
type
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Absinthe.Schema.Rule.InputOutputTypesCorrectlyPlacedTest do
use Absinthe.Case, async: true

describe "rule" do
describe "macro schema" do
test "is enforced with output types on arguments" do
assert_schema_error("invalid_output_types", [
%{
Expand Down Expand Up @@ -56,4 +56,74 @@ defmodule Absinthe.Schema.Rule.InputOutputTypesCorrectlyPlacedTest do
])
end
end

describe "sdl schema" do
test "is enforced with output types on arguments" do
assert_schema_error("invalid_output_types_sdl", [
%{
extra: %{
field: :blah,
parent: Absinthe.Blueprint.Schema.ObjectTypeDefinition,
struct: Absinthe.Blueprint.Schema.InputObjectTypeDefinition,
type: :input
},
locations: [
%{
file: "test/support/fixtures/dynamic/invalid_output_types_sdl.exs",
line: 4
}
],
phase: Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced
},
%{
extra: %{
argument: :invalid_arg,
struct: Absinthe.Blueprint.Schema.ObjectTypeDefinition,
type: :user
},
locations: [
%{
file: "test/support/fixtures/dynamic/invalid_output_types_sdl.exs",
line: 4
}
],
phase: Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced
}
])
end

test "is enforced with input types on arguments" do
assert_schema_error("invalid_input_types_sdl", [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test asserts there are two errors, whilst the macro based schema only has one. This points to an issue with the macro based schema in that input object fields are FieldDefinition structs and not InputValueDefinition structs like there are in the SDL schema.

%{
extra: %{
argument: :blah,
struct: Absinthe.Blueprint.Schema.ObjectTypeDefinition,
type: :user
},
locations: [
%{
file: "test/support/fixtures/dynamic/invalid_input_types_sdl.exs",
line: 4
}
],
phase: Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced
},
%{
extra: %{
field: :blah,
parent: Absinthe.Blueprint.Schema.InputObjectTypeDefinition,
struct: Absinthe.Blueprint.Schema.ObjectTypeDefinition,
type: :user
},
locations: [
%{
file: "test/support/fixtures/dynamic/invalid_input_types_sdl.exs",
line: 4
}
],
phase: Absinthe.Phase.Schema.Validation.InputOutputTypesCorrectlyPlaced
}
])
end
end
end
15 changes: 15 additions & 0 deletions test/support/fixtures/dynamic/invalid_input_types_sdl.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Absinthe.Fixtures.InvalidOutputTypesSdlSchema do
use Absinthe.Schema

import_sdl """
type User

input Foo {
blah: User
}

type Query {
foo(arg: Foo): User
}
"""
end
17 changes: 17 additions & 0 deletions test/support/fixtures/dynamic/invalid_output_types_sdl.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Absinthe.Fixtures.InvalidInputTypesSdlSchema do
use Absinthe.Schema

import_sdl """
type User

input Input

type BadObject {
blah: Input
}

type Query {
foo(invalidArg: User): User
}
"""
end