Module: GraphQL

Defined in:
lib/graphql.rb,
lib/graphql/type.rb,
lib/graphql/version.rb,
lib/graphql/executor.rb,
lib/graphql/language.rb,
lib/graphql/type/list.rb,
lib/graphql/validator.rb,
lib/graphql/type/field.rb,
lib/graphql/type/schema.rb,
lib/graphql/errors/error.rb,
lib/graphql/language/name.rb,
lib/graphql/type/argument.rb,
lib/graphql/type/non_null.rb,
lib/graphql/language/field.rb,
lib/graphql/language/value.rb,
lib/graphql/type/directive.rb,
lib/graphql/type/enum_type.rb,
lib/graphql/language/parser.rb,
lib/graphql/type/directives.rb,
lib/graphql/type/union_type.rb,
lib/graphql/type/object_type.rb,
lib/graphql/type/scalar_type.rb,
lib/graphql/language/argument.rb,
lib/graphql/language/document.rb,
lib/graphql/language/variable.rb,
lib/graphql/configuration/slot.rb,
lib/graphql/language/directive.rb,
lib/graphql/language/list_type.rb,
lib/graphql/language/transform.rb,
lib/graphql/introspection/query.rb,
lib/graphql/language/named_type.rb,
lib/graphql/type/interface_type.rb,
lib/graphql/introspection/schema.rb,
lib/graphql/language/non_null_type.rb,
lib/graphql/language/selection_set.rb,
lib/graphql/type/input_object_type.rb,
lib/graphql/language/fragment_spread.rb,
lib/graphql/language/inline_fragment.rb,
lib/graphql/introspection/meta_fields.rb,
lib/graphql/configuration/configurable.rb,
lib/graphql/configuration/configuration.rb,
lib/graphql/language/fragment_definition.rb,
lib/graphql/language/variable_definition.rb,
lib/graphql/language/operation_definition.rb

Defined Under Namespace

Modules: Configuration, GraphQLAbstractType, GraphQLCompositeType, GraphQLInputType, GraphQLLeafType, GraphQLNamedType, GraphQLNullableType, GraphQLOutputType, GraphQLType, Introspection, Language Classes: Executor, GraphQLArgument, GraphQLArgumentConfiguration, GraphQLDirective, GraphQLDirectiveConfiguration, GraphQLEnumType, GraphQLEnumTypeConfiguration, GraphQLEnumValue, GraphQLError, GraphQLField, GraphQLFieldConfiguration, GraphQLInputObjectField, GraphQLInputObjectType, GraphQLInputObjectTypeConfiguration, GraphQLInterfaceType, GraphQLInterfaceTypeConfiguration, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLObjectTypeConfiguration, GraphQLScalarType, GraphQLScalarTypeConfiguration, GraphQLSchema, GraphQLSchemaConfiguration, GraphQLUnionType, GraphQLUnionTypeConfiguration, Validator

Constant Summary collapse

TypeMapReducer =
lambda do |memo, type|
  return TypeMapReducer.call(memo, type.of_type) if type.is_a?(GraphQLNonNull) || type.is_a?(GraphQLList)

  return memo if type.nil? || memo.keys.include?(type.name)

  memo[type.name] = type

  if type.is_a?(GraphQLUnionType) || type.is_a?(GraphQLInterfaceType)
    memo = type.possible_types.reduce(memo, &TypeMapReducer)
  end

  if type.is_a?(GraphQLObjectType)
    memo = type.interfaces.reduce(memo, &TypeMapReducer)
  end

  if type.is_a?(GraphQLObjectType) || type.is_a?(GraphQLInterfaceType)
    type.field_map.each do |name, field|
      memo = field.args.map(&:type).reduce(memo, &TypeMapReducer)
      memo = TypeMapReducer.call(memo, field.type)
    end
  end

  memo
end
VERSION =
'0.0.2'
GraphQLIncludeDirective =
GraphQLDirective.new do
  name        :include
  description 'Directs the executor to include this field or fragment only when the `if` argument is true.'

  arg :if, ! GraphQLBoolean, description: 'Included when true'

  on_operation  false
  on_fragment   true
  on_field      true
end
GraphQLSkipDirective =
GraphQLDirective.new do
  name        :skip
  description 'Directs the executor to skip this field or fragment only when the `if` argument is true.'

  arg :if, ! GraphQLBoolean, description: 'Skipped when true'

  on_operation  false
  on_fragment   true
  on_field      true
end
GraphQLInt =

Int

GraphQLScalarType.new do
  name 'Int'

  serialize lambda { |value|
    value = value.to_s
    return nil unless value =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
    value.to_f.round
  }

  parse_value serialize #-> (value) { serialize(value) }

  parse_literal lambda { |ast|
    ast[:kind] == :int ? ast[:value].to_i : nil
  }
end
GraphQLFloat =

Float

GraphQLScalarType.new do
  name 'Float'

  serialize lambda { |value|
    value = value.to_s
    return nil unless value =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/
    value.to_f
  }

  parse_value serialize

  parse_literal lambda { |ast|
    ast[:kind] == :int || ast[:kind] == :float ? ast[:value].to_f : nil
  }
end
GraphQLString =

String

GraphQLScalarType.new do
  name 'String'

  serialize lambda { |value|
    value.to_s
  }

  parse_value serialize

  parse_literal lambda { |ast|
    ast[:kind] == :string ? ast[:value] : nil
  }
end
GraphQLBoolean =

Boolean

GraphQLScalarType.new do
  name 'Boolean'

  serialize lambda { |value|
    return false if value == 0
    !!value
  }

  parse_value serialize

  parse_literal lambda { |ast|
    ast[:kind] == :boolean ? ast[:value] : nil
  }
end
GraphQLID =

ID

GraphQLScalarType.new do
  name 'ID'

  serialize lambda { |value|
    value.to_s
  }

  parse_value serialize

  parse_literal lambda { |ast|
    ast[:kind] == :string || ast[:kind] == :int ? ast[:value] : nil
  }
end

Class Method Summary collapse

Class Method Details

.graphql(schema, query, root, params, operation = nil) ⇒ Object



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

def self.graphql(schema, query, root, params, operation = nil)
  document  = GraphQL::Language.parse(query)
  executor  = GraphQL::Executor.new(document, schema)
  result    = executor.execute(root, params, operation)
  { data: result }
rescue StandardError => e
  { errors: [e] }
end

.named_type(type) ⇒ Object



40
41
42
# File 'lib/graphql/type.rb', line 40

def self.named_type(type)
  type = type.of_type while type.is_a?(GraphQLList) || type.is_a?(GraphQLNonNull)
end