Skip to content

Commit

Permalink
fix missing fragment types. (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmac authored Jul 3, 2024
1 parent 2d785ec commit 31e997b
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/graphql/stitching/planner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ def extract_locale_selections(
fragment = @request.fragment_definitions[node.name]
next unless @supergraph.locations_by_type[fragment.type.name].include?(current_location)

requires_typename = true
fragment_type = @supergraph.memoized_schema_types[fragment.type.name]
is_same_scope = fragment_type == parent_type
selection_set = is_same_scope ? locale_selections : []
extract_locale_selections(current_location, fragment_type, parent_index, fragment.selections, path, locale_variables, selection_set)

unless is_same_scope
locale_selections << GraphQL::Language::Nodes::InlineFragment.new(type: fragment.type, selections: selection_set)
requires_typename = true
end

else
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql/stitching/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module GraphQL
module Stitching
VERSION = "1.4.0"
VERSION = "1.4.1"
end
end
76 changes: 76 additions & 0 deletions test/graphql/stitching/integration/merged_child.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# frozen_string_literal: true

require "test_helper"
require_relative "../../../schemas/merged_child"

describe 'GraphQL::Stitching, merged child' do
def setup
@supergraph = compose_definitions({
"a" => Schemas::MergedChild::ParentSchema,
"b" => Schemas::MergedChild::ChildSchema,
})

@expected = {
"author" => {
"name" => "Frank Herbert",
"book" => {
"title" => "Dune",
"year" => 1965,
},
},
}
end

def test_resolves_fragment_spread_for_parent_of_merged_child
result = plan_and_execute(@supergraph, %|
query {
author { ...AuthorAttrs }
}
fragment AuthorAttrs on Author {
name
book {
title
year
}
}
|)

assert_equal @expected, result["data"]
end

def test_resolves_inline_fragment_for_parent_of_merged_child
result = plan_and_execute(@supergraph, %|
query {
author {
...on Author {
name
book {
title
year
}
}
}
}
|)

assert_equal @expected, result["data"]
end

def test_resolves_untyped_fragment_for_parent_of_merged_child
result = plan_and_execute(@supergraph, %|
query {
author {
... {
name
book {
title
year
}
}
}
}
|)

assert_equal @expected, result["data"]
end
end
75 changes: 75 additions & 0 deletions test/schemas/merged_child.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

module Schemas
module MergedChild
class StitchingResolver < GraphQL::Schema::Directive
graphql_name "stitch"
locations FIELD_DEFINITION
argument :key, String, required: true
argument :arguments, String, required: false
repeatable true
end

AUTHOR = {
id: "1",
name: "Frank Herbert",
book: {
id: "1",
title: "Dune",
year: 1965,
},
}.freeze

class ParentSchema < GraphQL::Schema
class Book < GraphQL::Schema::Object
field :id, ID, null: false
field :title, String, null: false
end

class Author < GraphQL::Schema::Object
field :id, ID, null: false
field :name, String, null: false
field :book, Book, null: true
end

class Query < GraphQL::Schema::Object
field :author, Author, null: false

def author
AUTHOR
end

field :book, Book, null: true do
directive StitchingResolver, key: "id"
argument :id, ID, required: true
end

def book(id:)
AUTHOR[:book] if AUTHOR[:book][:id] == id
end
end

query Query
end

class ChildSchema < GraphQL::Schema
class Book < GraphQL::Schema::Object
field :id, ID, null: false
field :year, Int, null: false
end

class Query < GraphQL::Schema::Object
field :book, Book, null: true do
directive StitchingResolver, key: "id"
argument :id, ID, required: true
end

def book(id:)
AUTHOR[:book] if AUTHOR[:book][:id] == id
end
end

query Query
end
end
end

0 comments on commit 31e997b

Please # to comment.