-
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
16
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 all commits
Commits
Show all changes
16 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 f84a511
Merge branch 'master' into partial-execution
rmosolgo 34870ea
Update tests
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,136 @@ | ||
# 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 | ||
selections = [@query.selected_operation] | ||
type = @query.schema.query # TODO could be other? | ||
parent_type = nil | ||
field_defn = nil | ||
@path.each do |name_in_doc| | ||
next_selections = [] | ||
selections.each do |selection| | ||
selections_to_check = [] | ||
selections_to_check.concat(selection.selections) | ||
while (sel = selections_to_check.shift) | ||
case sel | ||
when GraphQL::Language::Nodes::InlineFragment | ||
selections_to_check.concat(sel.selections) | ||
when GraphQL::Language::Nodes::FragmentSpread | ||
fragment = @query.fragments[sel.name] | ||
selections_to_check.concat(fragment.selections) | ||
when GraphQL::Language::Nodes::Field | ||
if sel.alias == name_in_doc || sel.name == name_in_doc | ||
next_selections << sel | ||
end | ||
else | ||
raise "Unexpected selection in partial path: #{sel.class}, #{sel.inspect}" | ||
end | ||
end | ||
end | ||
|
||
if next_selections.empty? | ||
raise ArgumentError, "Path `#{@path.inspect}` is not present in this query. `#{name_in_doc.inspect}` was not found. Try a different path or rewrite the query to include it." | ||
end | ||
field_name = next_selections.first.name | ||
field_defn = type.get_field(field_name, @query.context) || raise("Invariant: no field called #{field_name} on #{type.graphql_name}") | ||
parent_type = type | ||
type = field_defn.type | ||
if type.non_null? | ||
type = type.of_type | ||
end | ||
selections = next_selections | ||
end | ||
@parent_type = parent_type | ||
@ast_nodes = selections | ||
@root_type = type | ||
@field_definition = field_defn | ||
@leaf = @root_type.unwrap.kind.leaf? | ||
end | ||
|
||
def leaf? | ||
@leaf | ||
end | ||
|
||
attr_reader :context, :query, :ast_nodes, :root_type, :object, :field_definition, :path, :parent_type | ||
|
||
attr_accessor :multiplex, :result_values | ||
|
||
class Result < GraphQL::Query::Result | ||
def path | ||
@query.path | ||
end | ||
|
||
# @return [GraphQL::Query::Partial] | ||
def partial | ||
@query | ||
end | ||
end | ||
|
||
def result | ||
@result ||= Result.new(query: self, values: result_values) | ||
end | ||
|
||
def current_trace | ||
@query.current_trace | ||
end | ||
|
||
def schema | ||
@query.schema | ||
end | ||
|
||
def types | ||
@query.types | ||
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 | ||
|
||
# TODO dry with query | ||
def arguments_for(ast_node, definition, parent_object: nil) | ||
arguments_cache.fetch(ast_node, definition, parent_object) | ||
end | ||
|
||
# TODO dry with query | ||
def arguments_cache | ||
@arguments_cache ||= Execution::Interpreter::ArgumentsCache.new(self) | ||
end | ||
|
||
# TODO dry | ||
def handle_or_reraise(err) | ||
@query.schema.handle_or_reraise(context, err) | ||
end | ||
|
||
def resolve_type(...) | ||
@query.resolve_type(...) | ||
end | ||
|
||
def variables | ||
@query.variables | ||
end | ||
|
||
def fragments | ||
@query.fragments | ||
end | ||
end | ||
end | ||
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.