-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add GraphQL::Query::Partial #5183
Open
rmosolgo
wants to merge
14
commits into
master
Choose a base branch
from
partial-execution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
236cbe9
Add GraphQL::Query::Partial
rmosolgo 5b73d6e
Add tests with args, dataloader, errorsg
rmosolgo f1a8fb2
Merge branch 'master' into partial-execution
rmosolgo 20fd744
Add test for running on Arrays
rmosolgo 38e19d7
Add dedicated run_partials method
rmosolgo 4090291
Start on run_partial_eager
rmosolgo 93952e7
Build out partial execution with list types
rmosolgo 87c244e
Fix typos
rmosolgo babf68f
Implement partial execution for list types
rmosolgo 9cc47f5
Support AST branches that match the same path
rmosolgo 385ee3b
Add multiple errors test
rmosolgo dcd9541
Support isolated execution of scalar fields
rmosolgo 2f9b1db
Add some resolve_type
rmosolgo 6f81134
Support inline fragments and fragment spreads
rmosolgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
module GraphQL | ||
class Query | ||
# This class is _like_ a {GraphQL::Query}, except | ||
# @see Query#run_partials | ||
class Partial | ||
def initialize(path:, object:, query:) | ||
@path = path | ||
@object = object | ||
@query = query | ||
@context = GraphQL::Query::Context.new(query: self, schema: @query.schema, values: @query.context.to_h) | ||
@multiplex = nil | ||
@result_values = nil | ||
@result = nil | ||
end | ||
|
||
attr_reader :context | ||
|
||
attr_accessor :multiplex, :result_values | ||
|
||
def result | ||
@result ||= GraphQL::Query::Result.new(query: self, values: result_values) | ||
end | ||
|
||
def valid? | ||
true | ||
end | ||
|
||
def analyzers | ||
EmptyObjects::EMPTY_ARRAY | ||
end | ||
|
||
def current_trace | ||
@query.current_trace | ||
end | ||
|
||
def analysis_errors=(_errs) | ||
end | ||
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def subscription? | ||
false | ||
end | ||
|
||
def selected_operation | ||
selection = @query.selected_operation | ||
@path.each do |name_in_doc| | ||
selection = selection.selections.find { |sel| sel.alias == name_in_doc || sel.name == name_in_doc } | ||
end | ||
selection | ||
rmosolgo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def schema | ||
@query.schema | ||
end | ||
|
||
def types | ||
@query.types | ||
end | ||
|
||
def root_value | ||
@object | ||
end | ||
|
||
def root_type | ||
# Eventually do the traversal upstream of here, processing the group of partials together. | ||
selection = @query.selected_operation | ||
type = @query.schema.query # TODO could be other? | ||
@path.each do |name_in_doc| | ||
selection = selection.selections.find { |sel| sel.alias == name_in_doc || sel.name == name_in_doc } | ||
field_defn = type.get_field(selection.name, @query.context) || raise("Invariant: no field called #{selection.name.inspect} on #{type.graphql_name}") | ||
type = field_defn.type.unwrap | ||
end | ||
type | ||
end | ||
|
||
# TODO dry with query | ||
def after_lazy(value, &block) | ||
if !defined?(@runtime_instance) | ||
@runtime_instance = context.namespace(:interpreter_runtime)[:runtime] | ||
end | ||
|
||
if @runtime_instance | ||
@runtime_instance.minimal_after_lazy(value, &block) | ||
else | ||
@schema.after_lazy(value, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
require "spec_helper" | ||
|
||
describe GraphQL::Query::Partial do | ||
class PartialSchema < GraphQL::Schema | ||
FARMS = { | ||
"1" => OpenStruct.new(name: "Bellair Farm", products: ["VEGETABLES", "MEAT", "EGGS"]), | ||
"2" => OpenStruct.new(name: "Henley's Orchard", products: ["FRUIT", "MEAT", "EGGS"]), | ||
"3" => OpenStruct.new(name: "Wenger Grapes", products: ["FRUIT"]), | ||
} | ||
|
||
class FarmProduct < GraphQL::Schema::Enum | ||
value :FRUIT | ||
value :VEGETABLES | ||
value :MEAT | ||
value :EGGS | ||
value :DAIRY | ||
end | ||
|
||
class Farm < GraphQL::Schema::Object | ||
field :name, String | ||
field :products, [FarmProduct] | ||
end | ||
|
||
class Query < GraphQL::Schema::Object | ||
field :farms, [Farm], fallback_value: FARMS.values | ||
|
||
field :farm, Farm do | ||
argument :id, ID, loads: Farm, as: :farm | ||
end | ||
|
||
def farm(farm:) | ||
farm | ||
end | ||
end | ||
|
||
query(Query) | ||
|
||
def self.object_from_id(id, ctx) | ||
FARMS[id] | ||
end | ||
|
||
def self.resolve_type(abs_type, object, ctx) | ||
Farm | ||
end | ||
end | ||
|
||
focus | ||
it "returns results for the named part" do | ||
str = "{ | ||
farms { name, products } | ||
farm1: farm(id: \"1\") { name } | ||
farm2: farm(id: \"2\") { name } | ||
}" | ||
query = GraphQL::Query.new(PartialSchema, str) | ||
results = query.run_partials( | ||
["farm1"] => PartialSchema::FARMS["1"], | ||
["farm2"] => OpenStruct.new(name: "Injected Farm"), | ||
) | ||
|
||
assert_equal [ | ||
{ "data" => { "name" => "Bellair Farm" } }, | ||
{ "data" => { "name" => "Injected Farm" } }, | ||
], results | ||
end | ||
|
||
it "returns errors if they occur" | ||
it "raises errors when given bad paths" | ||
it "runs multiple partials concurrently" | ||
it "returns multiple errors concurrently" | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were leftover from a previous
GraphQL::Query::Context
implementation. I rediscovered them while working on this feature because at first, I thought I was going to need to usequery.context.dup
. I didn't end up doing that (instead,Query::Context.new
insidePartial#initialize
) but I'm going to keep these clean-ups.