class GraphQL::REPL::Expression

Public Class Methods

new(schema, str) click to toggle source
# File lib/graphql/repl.rb, line 75
def initialize(schema, str)
  @schema = schema
  @str = str
end

Public Instance Methods

calls() click to toggle source
# File lib/graphql/repl.rb, line 80
def calls
  proxy = Proxy.new
  b = proxy.get_binding
  b.eval(@str).calls
end
completions() click to toggle source
# File lib/graphql/repl.rb, line 86
def completions
  first, partial = @str.scan(/^(.+)\.(.*)$/)[0]

  if first
    type = Expression.new(@schema, first).return_type
  else
    partial = @str
    type = @schema.query.to_s
  end

  if type
    @schema.types[type].all_fields.map(&:name).grep(/^#{partial}/).map { |name|
      [first, name].compact.join(".")
    }
  else
    []
  end
end
document() click to toggle source
# File lib/graphql/repl.rb, line 118
def document
  calls = self.calls

  operation_type = case calls[0][0]
  when :query
    calls.shift
    "query"
  when :mutation
    calls.shift
    "mutation"
  else
    "query"
  end

  selection = calls.reverse.reduce(nil) do |child, call|
    case call[0]
    when :[]
      child
    when :on
      type = GraphQL::Language::Nodes::TypeName.new(name: call[1].to_s)
      node = GraphQL::Language::Nodes::InlineFragment.new(type: type)
      node.selections = [child] if child
      node
    else
      arguments = []

      (call[1] || {}).each do |name, value|
        arguments << GraphQL::Language::Nodes::Argument.new(name: name.to_s, value: value)
      end

      node = GraphQL::Language::Nodes::Field.new(name: call[0].to_s, arguments: arguments)
      node.selections = [child] if child
      node
    end
  end

  operation = GraphQL::Language::Nodes::OperationDefinition.new(operation_type: operation_type, selections: [selection])
  GraphQL::Language::Nodes::Document.new(definitions: [operation])
end
error_message() click to toggle source
# File lib/graphql/repl.rb, line 194
def error_message
  validator = GraphQL::StaticValidation::Validator.new(schema: @schema)
  query = GraphQL::Query.new(@schema, document: document)
  errors = validator.validate(query)
  errors.fetch(:errors)[0]&.message
end
response_path() click to toggle source
# File lib/graphql/repl.rb, line 105
def response_path
  calls.reduce([]) do |path, (name, *args)|
    case name
    when :query, :mutation, :on
      path
    when :[]
      path << args[0]
    else
      path << name.to_s
    end
  end
end
return_type() click to toggle source
# File lib/graphql/repl.rb, line 158
def return_type
  visitor = GraphQL::Language::Visitor.new(document)
  type_stack = GraphQL::StaticValidation::TypeStack.new(@schema, visitor)

  stack = []

  visitor[GraphQL::Language::Nodes::OperationDefinition] << ->(node, _parent) do
    stack << type_stack.object_types.last
  end
  visitor[GraphQL::Language::Nodes::FragmentDefinition] << ->(node, _parent) do
    stack << type_stack.object_types.last
  end
  visitor[GraphQL::Language::Nodes::Field] << ->(node, _parent) do
    stack << type_stack.field_definitions.last&.type
  end
  visitor[GraphQL::Language::Nodes::InlineFragment] << ->(node, _parent) do
    stack << type_stack.object_types.last
  end
  visitor.visit

  type = stack.last
  type = type.of_type if type.is_a?(GraphQL::NonNullType)

  if type.is_a?(GraphQL::ListType)
    if response_path.last.is_a?(Integer)
      type.unwrap.name
    else
      "List"
    end
  elsif type
    type.name
  else
    nil
  end
end