Module: GraphQL

Defined in:
lib/graphql.rb,
lib/graph_ql/parser.rb,
lib/graph_ql/version.rb

Defined Under Namespace

Modules: Definable, Forwardable, Introspection, Nodes, NonNullWithBang, StaticValidation, TypeKinds Classes: ArgumentDefiner, Directive, DirectiveChain, Enum, Field, FieldDefiner, InputObjectType, InputValue, Interface, ListType, NonNullType, ObjectType, Parser, Query, Repl, ScalarType, Schema, Transform, TypeDefiner, Union, Visitor

Constant Summary collapse

PARSER =
GraphQL::Parser.new
TRANSFORM =
GraphQL::Transform.new
VERSION =
"0.2.0"
INT_TYPE =
GraphQL::ScalarType.new do |t|
  t.name "Int"
  def t.coerce(value)
    value.is_a?(Numeric) ? value.to_i : nil
  end
end
FLOAT_TYPE =
GraphQL::ScalarType.new do |t|
  t.name "Float"
  def t.coerce(value)
    value.to_f
  end
end
STRING_TYPE =
GraphQL::ScalarType.new do |t|
  t.name "String"
  def t.coerce(value)
    value.to_s
  end
end
BOOLEAN_TYPE =
GraphQL::ScalarType.new do |t|
  t.name "Boolean"
  def t.coerce(value)
    !!value
  end
end
SkipDirective =
GraphQL::Directive.new do |d, type, field, arg|
  d.name "skip"
  d.description "Ignore this part of the query if `if` is true"
  d.on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
  d.arguments({
    if: arg.build({type: !GraphQL::BOOLEAN_TYPE})
  })
  d.resolve -> (arguments, proc) {
    if !arguments["if"]
      proc.call
    else
      nil
    end
  }
end
IncludeDirective =
GraphQL::Directive.new do |d, type, field, arg|
  d.name "include"
  d.description "Include this part of the query if `if` is true"
  d.on([GraphQL::Directive::ON_FIELD, GraphQL::Directive::ON_FRAGMENT])
  d.arguments({
    if: arg.build({type: !GraphQL::BOOLEAN_TYPE})
  })
  d.resolve -> (arguments, proc) {
    if arguments["if"]
      proc.call
    else
      nil
    end
  }
end

Class Method Summary collapse

Class Method Details

.parse(string, as: nil) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/graphql.rb', line 6

def self.parse(string, as: nil)
  parser = as ? GraphQL::PARSER.send(as) : GraphQL::PARSER
  tree = parser.parse(string)
  GraphQL::TRANSFORM.apply(tree)
rescue Parslet::ParseFailed => error
  line, col = error.cause.source.line_and_column
  raise [line, col, string].join(", ")
end