Module: FragmentClient::GraphqlAst

Extended by:
T::Sig
Defined in:
lib/fragment_client/graphql_ast.rb

Overview

Reading helpers for graphql-ruby's language AST.

Nothing here knows about Fragment. Kept separate so it can move to a gem of its own if a second consumer appears -- no such gem exists today.

Named GraphqlAst rather than GraphQL::Ast: a FragmentClient::GraphQL would shadow the real GraphQL for every constant lookup inside FragmentClient.

Class Method Summary collapse

Class Method Details

.inline_object_argument(field, argument_name) ⇒ Object



49
50
51
52
# File 'lib/fragment_client/graphql_ast.rb', line 49

def inline_object_argument(field, argument_name)
  value = field.arguments.find { |argument| argument.name == argument_name }&.value
  value.is_a?(GraphQL::Language::Nodes::InputObject) ? value : nil
end

.object_field(object, name) ⇒ Object



59
60
61
# File 'lib/fragment_client/graphql_ast.rb', line 59

def object_field(object, name)
  object.arguments.find { |argument| argument.name == name }&.value
end

.single_root_field(operation, field_name, operation_type: 'mutation') ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fragment_client/graphql_ast.rb', line 31

def single_root_field(operation, field_name, operation_type: 'mutation')
  return nil unless operation.operation_type == operation_type

  selections = operation.selections
  return nil unless selections.length == 1

  root = selections.first
  return nil unless root.is_a?(GraphQL::Language::Nodes::Field)

  root.name == field_name ? root : nil
end

.variable_types(operation) ⇒ Object



68
69
70
# File 'lib/fragment_client/graphql_ast.rb', line 68

def variable_types(operation)
  operation.variables.to_h { |definition| [definition.name, definition.type] }
end