Class: GraphQL::Language::VariableDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_valueObject

Returns the value of attribute default_value

Returns:

  • (Object)

    the current value of default_value



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

def default_value
  @default_value
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



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

def type
  @type
end

#variableObject

Returns the value of attribute variable

Returns:

  • (Object)

    the current value of variable



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

def variable
  @variable
end

Instance Method Details

#materialize(schema, param) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphql/language/variable_definition.rb', line 6

def materialize(schema, param)
  schema_type = type.materialize(schema)

  if schema_type.nil? || !schema_type.is_a?(GraphQLInputType)
    raise GraphQLError, "Variable '#{variable.name}' expected value " +
      "of type '#{schema_type}' which cannot be used as input type."
  end

  if Validator.valid_value?(param, schema_type)
    if !param && default_value
      return default_value.materialize(schema_type)
    end
    return Validator.coerce_value(param, schema_type)
  end

  if param
    raise GraphQLError, "Variable '#{variable.name}' expected value " +
      "of type '#{schema_type}' but got '#{param}'."
  else
    raise GraphQLError, "Variable '#{variable.name}' " +
      "of required type '#{type}' was not provided."
  end

end