Class: GraphQL::Language::Field

Inherits:
Struct
  • Object
show all
Defined in:
lib/graphql/language/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aliasObject

Returns the value of attribute alias

Returns:

  • (Object)

    the current value of alias



3
4
5
# File 'lib/graphql/language/field.rb', line 3

def alias
  @alias
end

#argumentsObject

Returns the value of attribute arguments

Returns:

  • (Object)

    the current value of arguments



3
4
5
# File 'lib/graphql/language/field.rb', line 3

def arguments
  @arguments
end

#directivesObject

Returns the value of attribute directives

Returns:

  • (Object)

    the current value of directives



3
4
5
# File 'lib/graphql/language/field.rb', line 3

def directives
  @directives
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/graphql/language/field.rb', line 3

def name
  @name
end

#selection_setObject

Returns the value of attribute selection_set

Returns:

  • (Object)

    the current value of selection_set



3
4
5
# File 'lib/graphql/language/field.rb', line 3

def selection_set
  @selection_set
end

Instance Method Details

#keyObject

GraphQL Specification

6.3 Evaluate selection sets
  CollectFields
    responseKey implementation


10
11
12
# File 'lib/graphql/language/field.rb', line 10

def key
  self.alias || self.name
end

#materialize_arguments(object_type, variables) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/graphql/language/field.rb', line 36

def materialize_arguments(object_type, variables)
  schema_field(object_type).args.reduce({}) do |memo, field_argument|
    argument = arguments.find { |argument| argument.name == field_argument.name }
    memo[argument.name.to_sym] = argument.materialize(field_argument.type, variables) unless argument.nil?
    memo
  end
end

#resolve(context, object_type, object) ⇒ Object

GraphQL Specification

6.4.1 Field entries
  ResolveFieldOnObject implementation
    objectType, object, firstField = self
      + context[document, schema, root]

TODO: think of should or shouldn’t we pass self as fourth parameter



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql/language/field.rb', line 22

def resolve(context, object_type, object)
  arguments = [
      object,
      materialize_arguments(object_type, context[:variables]),
      context
  ]

  resolve   = schema_field(object_type).resolve
  arguments = arguments.slice(0, resolve.arity) if resolve.arity >= 0

  resolve.call(*arguments)
end

#schema_field(object_type) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/graphql/language/field.rb', line 44

def schema_field(object_type)
  case
  when GraphQL::Introspection.meta_field?(name)
    GraphQL::Introspection.meta_field(name)
  else
    object_type.field(name)
  end
end