Class: GraphQL::Stitching::Request
- Inherits:
-
Object
- Object
- GraphQL::Stitching::Request
- Defined in:
- lib/graphql/stitching/request.rb
Constant Summary collapse
- SUPPORTED_OPERATIONS =
["query", "mutation"].freeze
- SKIP_INCLUDE_DIRECTIVE =
/@(?:skip|include)/
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#operation_name ⇒ Object
readonly
Returns the value of attribute operation_name.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #digest ⇒ Object
- #fragment_definitions ⇒ Object
-
#initialize(document, operation_name: nil, variables: nil, context: nil) ⇒ Request
constructor
A new instance of Request.
- #normalized_digest ⇒ Object
- #normalized_string ⇒ Object
- #operation ⇒ Object
- #operation_directives ⇒ Object
- #prepare! ⇒ Object
- #string ⇒ Object
- #variable_definitions ⇒ Object
Constructor Details
#initialize(document, operation_name: nil, variables: nil, context: nil) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/graphql/stitching/request.rb', line 11 def initialize(document, operation_name: nil, variables: nil, context: nil) @document = if document.is_a?(String) @string = document GraphQL.parse(document) else document end @operation_name = operation_name @variables = variables || {} @context = context || GraphQL::Stitching::EMPTY_OBJECT end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/graphql/stitching/request.rb', line 9 def context @context end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
9 10 11 |
# File 'lib/graphql/stitching/request.rb', line 9 def document @document end |
#operation_name ⇒ Object (readonly)
Returns the value of attribute operation_name.
9 10 11 |
# File 'lib/graphql/stitching/request.rb', line 9 def operation_name @operation_name end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
9 10 11 |
# File 'lib/graphql/stitching/request.rb', line 9 def variables @variables end |
Instance Method Details
#digest ⇒ Object
32 33 34 |
# File 'lib/graphql/stitching/request.rb', line 32 def digest @digest ||= Digest::SHA2.hexdigest(string) end |
#fragment_definitions ⇒ Object
71 72 73 74 75 |
# File 'lib/graphql/stitching/request.rb', line 71 def fragment_definitions @fragment_definitions ||= @document.definitions.each_with_object({}) do |d, memo| memo[d.name] = d if d.is_a?(GraphQL::Language::Nodes::FragmentDefinition) end end |
#normalized_digest ⇒ Object
36 37 38 |
# File 'lib/graphql/stitching/request.rb', line 36 def normalized_digest @normalized_digest ||= Digest::SHA2.hexdigest(normalized_string) end |
#normalized_string ⇒ Object
28 29 30 |
# File 'lib/graphql/stitching/request.rb', line 28 def normalized_string @normalized_string ||= @document.to_query_string end |
#operation ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/graphql/stitching/request.rb', line 40 def operation @operation ||= begin operation_defs = @document.definitions.select do |d| next unless d.is_a?(GraphQL::Language::Nodes::OperationDefinition) next unless SUPPORTED_OPERATIONS.include?(d.operation_type) @operation_name ? d.name == @operation_name : true end if operation_defs.length < 1 raise GraphQL::ExecutionError, "Invalid root operation for given name and operation type." elsif operation_defs.length > 1 raise GraphQL::ExecutionError, "An operation name is required when sending multiple operations." end operation_defs.first end end |
#operation_directives ⇒ Object
58 59 60 61 62 63 |
# File 'lib/graphql/stitching/request.rb', line 58 def operation_directives @operation_directives ||= if operation.directives.any? printer = GraphQL::Language::Printer.new operation.directives.map { printer.print(_1) }.join(" ") end end |
#prepare! ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/graphql/stitching/request.rb', line 77 def prepare! operation.variables.each do |v| @variables[v.name] = v.default_value if @variables[v.name].nil? && !v.default_value.nil? end if @string.nil? || @string.match?(SKIP_INCLUDE_DIRECTIVE) SkipInclude.render(@document, @variables) do |modified_ast| @document = modified_ast @string = @normalized_string = nil @digest = @normalized_digest = nil @operation = @operation_directives = @variable_definitions = nil end end self end |
#string ⇒ Object
24 25 26 |
# File 'lib/graphql/stitching/request.rb', line 24 def string @string || normalized_string end |
#variable_definitions ⇒ Object
65 66 67 68 69 |
# File 'lib/graphql/stitching/request.rb', line 65 def variable_definitions @variable_definitions ||= operation.variables.each_with_object({}) do |v, memo| memo[v.name] = v.type end end |