Skip to content

Commit

Permalink
Merge pull request #202 from rewritten/reorder-phase-after-type-exten…
Browse files Browse the repository at this point in the history
…sions

Make sure Relay phase happens after all the imports
  • Loading branch information
benwilson512 authored Feb 21, 2023
2 parents b6f3636 + 6e9bde3 commit 2e0c18a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/absinthe/relay/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ defmodule Absinthe.Relay.Schema do

def pipeline(pipeline) do
pipeline
|> Absinthe.Pipeline.insert_after(Absinthe.Phase.Schema.TypeImports, __MODULE__.Phase)
|> Absinthe.Pipeline.insert_before(
Absinthe.Phase.Schema.ApplyDeclaration,
__MODULE__.Phase
)
end
end
94 changes: 94 additions & 0 deletions test/lib/absinthe/relay/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,98 @@ defmodule Absinthe.Relay.ConnectionTest do
assert StarWars.Schema.__absinthe_type__(:ship_edge)
end
end

describe "when importing connections in type extensions" do
defmodule PetsExtensions do
use Absinthe.Schema.Notation
use Absinthe.Relay.Schema.Notation, :classic

@pets %{
"1" => %{id: "1", name: "Svenja"},
"2" => %{id: "2", name: "Jock"},
"3" => %{id: "3", name: "Sherlock"}
}

object :pet do
field :name, :string
end

connection(node_type: :pet)

extend object(:person) do
connection field :pets, node_type: :pet do
resolve fn resolve_args, %{source: person} ->
Absinthe.Relay.Connection.from_list(
Enum.map(person.pets, &Map.get(@pets, &1)),
resolve_args
)
end
end
end
end

defmodule PersonExtensions do
use Absinthe.Schema.Notation
use Absinthe.Relay.Schema.Notation, :classic

@people %{
"jack" => %{id: "jack", name: "Jack", age: 35, pets: ["1", "2"], favorite_pets: ["2"]},
"jill" => %{id: "jill", name: "Jill", age: 31, pets: ["3"], favorite_pets: ["3"]}
}

object :person do
field :name, :string
field :age, :integer

connection field :friends, node_type: :person do
resolve fn resolve_args, %{source: _person} ->
Absinthe.Relay.Connection.from_list(
Map.values(@people),
resolve_args
)
end
end
end

connection(node_type: :person)
end

defmodule SchemaWithConnectionInTypeExtension do
use Absinthe.Schema
use Absinthe.Relay.Schema, :classic

import_types PetsExtensions
import_types PersonExtensions
import_type_extensions(PetsExtensions)

@people %{
"jack" => %{id: "jack", name: "Jack", age: 35, pets: ["1", "2"], favorite_pets: ["2"]},
"jill" => %{id: "jill", name: "Jill", age: 31, pets: ["3"], favorite_pets: ["3"]}
}

extend object(:query) do
connection field :people, node_type: :person do
resolve fn _, _ -> {:ok, Map.values(@people)} end
end
end

query do
end
end

test "It keeps args from the local type extension" do
assert %{after: _, before: _, first: _, last: _} =
SchemaWithConnectionInTypeExtension.__absinthe_type__(:query).fields.people.args
end

test "It keeps args from the imported type" do
assert %{after: _, before: _, first: _, last: _} =
SchemaWithConnectionInTypeExtension.__absinthe_type__(:person).fields.friends.args
end

test "It keeps args from the imported type extension" do
assert %{after: _, before: _, first: _, last: _} =
SchemaWithConnectionInTypeExtension.__absinthe_type__(:person).fields.pets.args
end
end
end

0 comments on commit 2e0c18a

Please sign in to comment.