Class: GraphQL::Schema::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/schema/printer.rb

Overview

Used to convert your GraphQL::Schema to a GraphQL schema string

Examples:

print your schema to standard output (via helper)

MySchema = GraphQL::Schema.define(query: QueryType)
puts GraphQL::Schema::Printer.print_schema(MySchema)

print your schema to standard output

MySchema = GraphQL::Schema.define(query: QueryType)
puts GraphQL::Schema::Printer.new(MySchema).print_schema

print a single type to standard output

query_root = GraphQL::ObjectType.define do
  name "Query"
  description "The query root of this schema"

  field :post do
    type post_type
    resolve ->(obj, args, ctx) { Post.find(args["id"]) }
  end
end

post_type = GraphQL::ObjectType.define do
  name "Post"
  description "A blog post"

  field :id, !types.ID
  field :title, !types.String
  field :body, !types.String
end

MySchema = GraphQL::Schema.define(query: query_root)

printer = GraphQL::Schema::Printer.new(MySchema)
puts printer.print_type(post_type)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, context: nil, only: nil, except: nil, introspection: false) ⇒ Printer

Returns a new instance of Printer.

Parameters:

  • schema (GraphQL::Schema)
  • context (Hash) (defaults to: nil)
  • only (<#call(member, ctx)>) (defaults to: nil)
  • except (<#call(member, ctx)>) (defaults to: nil)
  • introspection (Boolean) (defaults to: false)

    Should include the introspection types in the string?



47
48
49
50
51
52
53
54
# File 'lib/graphql/schema/printer.rb', line 47

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

  blacklist = build_blacklist(only, except, introspection: introspection)
  filter = GraphQL::Filter.new(except: blacklist)
  @warden = GraphQL::Schema::Warden.new(filter, schema: @schema, context: @context)
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

#wardenObject (readonly)

Returns the value of attribute warden.



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

def warden
  @warden
end

Class Method Details

Return the GraphQL schema string for the introspection type system



57
58
59
60
61
62
63
# File 'lib/graphql/schema/printer.rb', line 57

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

Return a GraphQL schema string for the defined types in the schema

Parameters:

  • schema (GraphQL::Schema)
  • context (Hash)
  • only (<#call(member, ctx)>)
  • except (<#call(member, ctx)>)


70
71
72
73
# File 'lib/graphql/schema/printer.rb', line 70

def self.print_schema(schema, **args)
  printer = new(schema, **args)
  printer.print_schema
end

Instance Method Details

Return a GraphQL schema string for the defined types in the schema



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphql/schema/printer.rb', line 76

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


90
91
92
# File 'lib/graphql/schema/printer.rb', line 90

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