class GraphQL::Schema::Printer


puts printer.print_type(post_type)
printer = GraphQL::Schema::Printer.new(MySchema)
MySchema = GraphQL::Schema.define(query: query_root)
end
field :body, !types.String
field :title, !types.String
field :id, !types.ID
description “A blog post”
name “Post”
post_type = GraphQL::ObjectType.define do
end
end
resolve ->(obj, args, ctx) { Post.find(args) }
type post_type
field :post do
description “The query root of this schema”
name “Query”
query_root = GraphQL::ObjectType.define do
@example print a single type to standard output
puts GraphQL::Schema::Printer.new(MySchema).print_schema
MySchema = GraphQL::Schema.define(query: QueryType)
@example print your schema to standard output
puts GraphQL::Schema::Printer.print_schema(MySchema)
MySchema = GraphQL::Schema.define(query: QueryType)
@example print your schema to standard output (via helper)
Used to convert your {GraphQL::Schema} to a GraphQL schema string

def self.print_introspection_schema

Return the GraphQL schema string for the introspection type system
def self.print_introspection_schema
  query_root = ObjectType.define(name: "Root")
  schema = GraphQL::Schema.define(query: query_root)
  blacklist = ->(m, ctx) { m == query_root }
  printer = new(schema, except: blacklist, introspection: true)
  printer.print_schema
end

def self.print_schema(schema, **args)

Parameters:
  • except (<#call(member, ctx)>) --
  • only (<#call(member, ctx)>) --
  • context (Hash) --
  • schema (GraphQL::Schema) --
def self.print_schema(schema, **args)
  printer = new(schema, **args)
  printer.print_schema
end

def build_blacklist(only, except, introspection:)

def build_blacklist(only, except, introspection:)
  if introspection
    if only
      ->(m, ctx) { !only.call(m, ctx) }
    elsif except
      except
    else
      ->(m, ctx) { false }
    end
  else
    if only
      ->(m, ctx) { !(IS_USER_DEFINED_MEMBER.call(m) && only.call(m, ctx)) }
    elsif except
      ->(m, ctx) { !IS_USER_DEFINED_MEMBER.call(m) || except.call(m, ctx) }
    else
      ->(m, ctx) { !IS_USER_DEFINED_MEMBER.call(m) }
    end
  end
end

def initialize(schema, context: nil, only: nil, except: nil, introspection: false)

Parameters:
  • introspection (Boolean) -- Should include the introspection types in the string?
  • except (<#call(member, ctx)>) --
  • only (<#call(member, ctx)>) --
  • context (Hash) --
  • schema (GraphQL::Schema) --
def initialize(schema, context: nil, only: nil, except: nil, introspection: false)
  @schema = schema
  @context = context
  blacklist = build_blacklist(only, except, introspection: introspection)
  @warden = GraphQL::Schema::Warden.new(blacklist, schema: @schema, context: @context)
end

def print_directive(directive)

def print_directive(directive)
  TypeKindPrinters::DirectivePrinter.print(warden, directive)
end

def print_schema

Return a GraphQL schema string for the defined types in the schema
def print_schema
  directive_definitions = warden.directives.map { |directive| print_directive(directive) }
  printable_types = warden.types.reject(&:default_scalar?)
  type_definitions = printable_types
    .sort_by(&:name)
    .map { |type| print_type(type) }
  [print_schema_definition].compact
                           .concat(directive_definitions)
                           .concat(type_definitions).join("\n\n")
end

def print_schema_definition

def print_schema_definition
  if (schema.query.nil? || schema.query.name == 'Query') &&
     (schema.mutation.nil? || schema.mutation.name == 'Mutation') &&
     (schema.subscription.nil? || schema.subscription.name == 'Subscription')
    return
  end
  operations = [:query, :mutation, :subscription].map do |operation_type|
    object_type = schema.public_send(operation_type)
    # Special treatment for the introspection schema, which prints `{ query: "Root" }`
    if object_type && (warden.get_type(object_type.name) || (object_type.name == "Root" && schema.query == object_type))
      "  #{operation_type}: #{object_type.name}\n"
    else
      nil
    end
  end.compact.join
  "schema {\n#{operations}}"
end

def print_type(type)

def print_type(type)
  TypeKindPrinters::STRATEGIES.fetch(type.kind).print(warden, type)
end