Class: GraphQL::Stitching::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/stitching/request.rb

Constant Summary collapse

SUPPORTED_OPERATIONS =
["query", "mutation"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, operation_name: nil, variables: nil, context: nil) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/graphql/stitching/request.rb', line 10

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 || GraphQL::Stitching::EMPTY_OBJECT
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/graphql/stitching/request.rb', line 8

def context
  @context
end

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/graphql/stitching/request.rb', line 8

def document
  @document
end

#operation_nameObject (readonly)

Returns the value of attribute operation_name.



8
9
10
# File 'lib/graphql/stitching/request.rb', line 8

def operation_name
  @operation_name
end

#variablesObject (readonly)

Returns the value of attribute variables.



8
9
10
# File 'lib/graphql/stitching/request.rb', line 8

def variables
  @variables
end

Instance Method Details

#digestObject



29
30
31
# File 'lib/graphql/stitching/request.rb', line 29

def digest
  @digest ||= Digest::SHA2.hexdigest(string)
end

#fragment_definitionsObject



57
58
59
60
61
# File 'lib/graphql/stitching/request.rb', line 57

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

#operationObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/stitching/request.rb', line 33

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

#prepare!Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/graphql/stitching/request.rb', line 63

def prepare!
  operation.variables.each do |v|
    @variables[v.name] ||= v.default_value
  end

  if @may_contain_runtime_directives
    @document, modified = SkipInclude.render(@document, @variables)

    if modified
      @string = nil
      @digest = nil
      @operation = nil
      @variable_definitions = nil
      @fragment_definitions = nil
    end
  end

  self
end

#stringObject



25
26
27
# File 'lib/graphql/stitching/request.rb', line 25

def string
  @string ||= @document.to_query_string
end

#variable_definitionsObject



51
52
53
54
55
# File 'lib/graphql/stitching/request.rb', line 51

def variable_definitions
  @variable_definitions ||= operation.variables.each_with_object({}) do |v, memo|
    memo[v.name] = v.type
  end
end