Skip to content

Commit

Permalink
Misc cleanup (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmac authored Feb 15, 2023
1 parent 9c4f818 commit 4e0ae18
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 203 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ The [Executor](./docs/executor.md) component builds atop the Ruby fiber-based im

## Example

This repo includes a working example of several stitched schemas running across Rack servers. Try running it:
This repo includes a working example of several stitched schemas running across small Rack servers. Try running it:

```shell
bundle install
Expand Down
17 changes: 16 additions & 1 deletion docs/composer.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,19 @@ supergraph = GraphQL::Stitching::Composer.new(
).perform
```

Each merger accepts a `values_by_location` and `info` argument. These provide a `location => value` mapping of all possible values encountered, and information about where this data is used (type name, field name, argument name, etc.). The function should then select a value (or compute a new one) and return that for use in the combined schema.
Each merger accepts a `values_by_location` and an `info` argument; these provide the values found across locations and info about where in schema they were encountered:

```ruby
values_by_location = {
"storefronts" => "A fabulous data type.",
"products" => "An excellent data type.",
}

info = {
type_name: "Product",
# field_name: ...,
# argument_name: ...,
}
```

The function should then select a value (or compute a new one) and return that for use in the combined schema.
17 changes: 9 additions & 8 deletions lib/graphql/stitching/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,20 @@ def initialize(supergraph:, request:, plan:, nonblocking: false)

def perform(raw: false)
exec!

result = {}
result["data"] = @data if @data && @data.length > 0
result["errors"] = @errors if @errors.length > 0

if result["data"] && !raw
GraphQL::Stitching::Shaper.new(
if @data && @data.length > 0
result["data"] = raw ? @data : GraphQL::Stitching::Shaper.new(
schema: @supergraph.schema,
request: @request,
).perform!(result)
else
result
).perform!(@data)
end

if @errors.length > 0
result["errors"] = @errors
end

result
end

private
Expand Down
14 changes: 10 additions & 4 deletions lib/graphql/stitching/planner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ def add_operation(location:, parent_type:, selections: nil, insertion_path: [],
extract_locale_selections(location, parent_type, selections, insertion_path, parent_key)
end

grouping = [after_key, location, parent_type.graphql_name, *insertion_path].join("/")
grouping = String.new
grouping << after_key.to_s << "/" << location << "/" << parent_type.graphql_name
grouping = insertion_path.reduce(grouping) do |memo, segment|
memo << "/" << segment
end

if op = @operations_by_grouping[grouping]
op.selections += selection_set if selection_set
Expand Down Expand Up @@ -161,8 +165,10 @@ def extract_locale_selections(current_location, parent_type, input_selections, i
if Util.is_leaf_type?(field_type)
selections_result << node
else
expanded_path = [*insertion_path, node.alias || node.name]
selection_set, variables = extract_locale_selections(current_location, field_type, node.selections, expanded_path, after_key)
insertion_path.push(node.alias || node.name)
selection_set, variables = extract_locale_selections(current_location, field_type, node.selections, insertion_path, after_key)
insertion_path.pop

selections_result << node.merge(selections: selection_set)
variables_result.merge!(variables)
end
Expand Down Expand Up @@ -260,7 +266,7 @@ def build_child_operations(current_location, parent_type, input_selections, inse
location: location,
selections: selections_by_location[location],
parent_type: parent_type,
insertion_path: insertion_path,
insertion_path: insertion_path.dup,
boundary: boundary,
after_key: after_key,
)
Expand Down
5 changes: 5 additions & 0 deletions lib/graphql/stitching/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ def assess_argument_value(arg)
attr_reader :document, :variables, :operation_name, :context

def initialize(document, operation_name: nil, variables: nil, context: nil)
@may_contain_runtime_directives = true

@document = if document.is_a?(String)
@may_contain_runtime_directives = document.include?("@")
GraphQL.parse(document)
else
document
Expand Down Expand Up @@ -111,6 +114,8 @@ def prepare!
@variables[v.name] ||= v.default_value
end

return self unless @may_contain_runtime_directives

visitor = ApplyRuntimeDirectives.new(@document, @variables)
@document = visitor.visit

Expand Down
3 changes: 1 addition & 2 deletions lib/graphql/stitching/shaper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ def initialize(schema:, request:)

def perform!(raw)
root_type = @schema.public_send(@request.operation.operation_type)
raw["data"] = resolve_object_scope(raw["data"], root_type, @request.operation.selections)
raw
resolve_object_scope(raw, root_type, @request.operation.selections)
end

private
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 = "0.2.0"
VERSION = "0.2.1"
end
end
78 changes: 31 additions & 47 deletions test/graphql/stitching/shaper/grooming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,19 @@ def test_prunes_stitching_fields
request: GraphQL::Stitching::Request.new("{ test { __typename req opt } }"),
)
raw = {
"data" => {
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"__typename" => "Test",
"req" => "yes",
"opt" => nil,
}
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"__typename" => "Test",
"req" => "yes",
"opt" => nil,
}
}
expected = {
"data" => {
"test" => {
"__typename" => "Test",
"req" => "yes",
"opt" => nil,
}
"test" => {
"__typename" => "Test",
"req" => "yes",
"opt" => nil,
}
}

Expand All @@ -41,20 +37,16 @@ def test_adds_missing_fields
request: GraphQL::Stitching::Request.new("{ test { req opt } }"),
)
raw = {
"data" => {
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"req" => "yes",
}
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"req" => "yes",
}
}
expected = {
"data" => {
"test" => {
"req" => "yes",
"opt" => nil,
}
"test" => {
"req" => "yes",
"opt" => nil,
}
}

Expand All @@ -68,20 +60,16 @@ def test_grooms_through_inline_fragments
request: GraphQL::Stitching::Request.new("{ test { ... on Test { ... on Test { req opt } } } }"),
)
raw = {
"data" => {
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"req" => "yes",
}
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"req" => "yes",
}
}
expected = {
"data" => {
"test" => {
"req" => "yes",
"opt" => nil,
}
"test" => {
"req" => "yes",
"opt" => nil,
}
}

Expand All @@ -100,20 +88,16 @@ def test_grooms_through_fragment_spreads
request: GraphQL::Stitching::Request.new(query),
)
raw = {
"data" => {
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"req" => "yes",
}
"test" => {
"_STITCH_req" => "yes",
"_STITCH_typename" => "Test",
"req" => "yes",
}
}
expected = {
"data" => {
"test" => {
"req" => "yes",
"opt" => nil,
}
"test" => {
"req" => "yes",
"opt" => nil,
}
}

Expand Down
Loading

0 comments on commit 4e0ae18

Please sign in to comment.