Class: GraphQL::Stitching::Request
- Inherits:
-
Object
- Object
- GraphQL::Stitching::Request
- Defined in:
- lib/graphql/stitching/request.rb
Defined Under Namespace
Classes: ApplyRuntimeDirectives
Constant Summary collapse
- SUPPORTED_OPERATIONS =
["query", "mutation"].freeze
- EMPTY_CONTEXT =
{}.freeze
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.
- #operation ⇒ 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/graphql/stitching/request.rb', line 59 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 end @operation_name = operation_name @variables = variables || {} @context = context || EMPTY_CONTEXT end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
57 58 59 |
# File 'lib/graphql/stitching/request.rb', line 57 def context @context end |
#document ⇒ Object (readonly)
Returns the value of attribute document.
57 58 59 |
# File 'lib/graphql/stitching/request.rb', line 57 def document @document end |
#operation_name ⇒ Object (readonly)
Returns the value of attribute operation_name.
57 58 59 |
# File 'lib/graphql/stitching/request.rb', line 57 def operation_name @operation_name end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
57 58 59 |
# File 'lib/graphql/stitching/request.rb', line 57 def variables @variables end |
Instance Method Details
#digest ⇒ Object
78 79 80 |
# File 'lib/graphql/stitching/request.rb', line 78 def digest @digest ||= Digest::SHA2.hexdigest(string) end |
#fragment_definitions ⇒ Object
106 107 108 109 110 |
# File 'lib/graphql/stitching/request.rb', line 106 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 |
#operation ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/graphql/stitching/request.rb', line 82 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." elsif operation_defs.length > 1 raise GraphQL::ExecutionError, "An operation name is required when sending multiple operations." end operation_defs.first end end |
#prepare! ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/graphql/stitching/request.rb', line 112 def prepare! operation.variables.each do |v| @variables[v.name] ||= v.default_value end return self unless @may_contain_runtime_directives visitor = ApplyRuntimeDirectives.new(@document, @variables) @document = visitor.visit if visitor.changed? @string = nil @digest = nil @operation = nil @variable_definitions = nil @fragment_definitions = nil end self end |
#string ⇒ Object
74 75 76 |
# File 'lib/graphql/stitching/request.rb', line 74 def string @string ||= @document.to_query_string end |
#variable_definitions ⇒ Object
100 101 102 103 104 |
# File 'lib/graphql/stitching/request.rb', line 100 def variable_definitions @variable_definitions ||= operation.variables.each_with_object({}) do |v, memo| memo[v.name] = v.type end end |