Class: GraphQL::Client::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/client/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, operation_name: nil, variables: {}, context: {}) ⇒ Query

Internal: Construct Query.

Avoid creating queries with this constructor, perfer using Client#query.

document - A parsed GraphQL::Language::Nodes::Document of the query operation_name - String operation to execute variables - Hash of variables to execute with the operation context - Hash of metadata to pass to network adapter



12
13
14
15
16
17
# File 'lib/graphql/client/query.rb', line 12

def initialize(document, operation_name: nil, variables: {}, context: {})
  @document = document
  @operation_name = operation_name
  @variables = variables
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Public: Hash of contextual metadata.



29
30
31
# File 'lib/graphql/client/query.rb', line 29

def context
  @context
end

#documentObject (readonly)

Public: A parsed GraphQL::Language::Nodes::Document of the query.



20
21
22
# File 'lib/graphql/client/query.rb', line 20

def document
  @document
end

#operation_nameObject (readonly)

Public: String name of operation to execute.



23
24
25
# File 'lib/graphql/client/query.rb', line 23

def operation_name
  @operation_name
end

#variablesObject (readonly)

Public: Hash of variables to execute with the operation.



26
27
28
# File 'lib/graphql/client/query.rb', line 26

def variables
  @variables
end

Instance Method Details

#operationObject

Public: Get operation definition node.

Returns GraphQL::Language::Nodes::OperationDefinition.



41
42
43
# File 'lib/graphql/client/query.rb', line 41

def operation
  document.definitions.find { |node| node.name == operation_name }
end

#operation_typeObject

Public: Query operation type

Returns “query”, “mutation” or “subscription”.



48
49
50
# File 'lib/graphql/client/query.rb', line 48

def operation_type
  operation.operation_type
end

#payloadObject

Internal: Payload object to pass to ActiveSupport::Notifications.

Returns Hash.



55
56
57
58
59
60
61
62
# File 'lib/graphql/client/query.rb', line 55

def payload
  {
    document: document,
    operation_name: operation_name,
    operation_type: operation_type,
    variables: variables
  }
end

#to_sObject

Public: Serialized query string

Returns String.



34
35
36
# File 'lib/graphql/client/query.rb', line 34

def to_s
  document.to_query_string
end