Module: GraphQL::Client::DefinitionVariables

Defined in:
lib/graphql/client/definition_variables.rb

Overview

Internal: Detect variables used in a definition.

Defined Under Namespace

Classes: VariablesVisitor

Class Method Summary collapse

Class Method Details

.operation_variables(schema, document, definition_name = nil) ⇒ Object

Internal: Detect all variables used in a given operation or fragment definition.

schema - A GraphQL::Schema document - A GraphQL::Language::Nodes::Document to scan definition_name - A String definition name. Defaults to anonymous definition.

Returns a Hash to VariableDefinition objects.



64
65
66
67
68
# File 'lib/graphql/client/definition_variables.rb', line 64

def self.operation_variables(schema, document, definition_name = nil)
  variables(schema, document, definition_name).map { |name, type|
    GraphQL::Language::Nodes::VariableDefinition.new(name: name.to_s, type: variable_node(type))
  }
end

.variable_node(type) ⇒ Object

Internal: Get AST node for GraphQL type.

type - A GraphQL::Type

Returns GraphQL::Language::Nodes::Type.



75
76
77
78
79
80
81
82
83
84
# File 'lib/graphql/client/definition_variables.rb', line 75

def self.variable_node(type)
  case type.kind.name
  when "NON_NULL"
    GraphQL::Language::Nodes::NonNullType.new(of_type: variable_node(type.of_type))
  when "LIST"
    GraphQL::Language::Nodes::ListType.new(of_type: variable_node(type.of_type))
  else
    GraphQL::Language::Nodes::TypeName.new(name: type.graphql_name)
  end
end

.variables(schema, document, definition_name = nil) ⇒ Object

Internal: Detect all variables used in a given operation or fragment definition.

schema - A GraphQL::Schema document - A GraphQL::Language::Nodes::Document to scan definition_name - A String definition name. Defaults to anonymous definition.

Returns a Hash to GraphQL::Type objects.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql/client/definition_variables.rb', line 16

def self.variables(schema, document, definition_name = nil)
  unless schema.is_a?(GraphQL::Schema) || (schema.is_a?(Class) && schema < GraphQL::Schema)
    raise TypeError, "expected schema to be a GraphQL::Schema, but was #{schema.class}"
  end

  unless document.is_a?(GraphQL::Language::Nodes::Document)
    raise TypeError, "expected document to be a GraphQL::Language::Nodes::Document, but was #{document.class}"
  end

  sliced_document = GraphQL::Language::DefinitionSlice.slice(document, definition_name)

  visitor = VariablesVisitor.new(sliced_document, schema: schema)
  visitor.visit
  visitor.variables
end