Module: GraphQL

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

Defined Under Namespace

Modules: DefinitionHelpers, Introspection, Nodes, StaticValidation, TypeKinds Classes: Argument, Directive, EnumType, Field, InputObjectType, InterfaceType, ListType, NonNullType, ObjectType, Parser, Query, Repl, ScalarType, Schema, Transform, UnionType, Visitor

Constant Summary collapse

PARSER =
GraphQL::Parser.new
ID_TYPE =
GraphQL::ScalarType.new do |t|
  t.name "ID"
  def t.coerce(value)
    value.to_s
  end
end
VERSION =
"0.4.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
TRANSFORM =
GraphQL::Transform.new
FLOAT_TYPE =
GraphQL::ScalarType.new do |t|
  t.name "Float"
  def t.coerce(value)
    value.respond_to?(:to_f) ? value.to_f : nil
  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

Class Method Summary collapse

Class Method Details

.parse(string, as: nil) ⇒ GraphQL::Nodes::Document

Turn a query string into an AST

Parameters:

  • string (String)

    a GraphQL query string

  • as (Symbol) (defaults to: nil)

    If you want to use this to parse some piece of a document, pass the rule name (from GraphQL::Parser::Parser)

Returns:



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

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