Class: GraphQL::Schema::Printer

Inherits:
Language::Printer 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)

Defined Under Namespace

Classes: IntrospectionPrinter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Language::Printer

#print

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
55
56
57
58
# File 'lib/graphql/schema/printer.rb', line 47

def initialize(schema, context: nil, only: nil, except: nil, introspection: false)
  @document_from_schema = GraphQL::Language::DocumentFromSchemaDefinition.new(
    schema,
    context: context,
    only: only,
    except: except,
    include_introspection_types: introspection,
  )

  @document = @document_from_schema.document
  @schema = schema
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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/graphql/schema/printer.rb', line 61

def self.print_introspection_schema
  query_root = Class.new(GraphQL::Schema::Object) do
    graphql_name "Root"
    field :throwaway_field, String, null: true
  end
  schema = Class.new(GraphQL::Schema) { query(query_root) }

  introspection_schema_ast = GraphQL::Language::DocumentFromSchemaDefinition.new(
    schema,
    except: ->(member, _) { member.graphql_name == "Root" },
    include_introspection_types: true,
    include_built_in_directives: true,
  ).document

  introspection_schema_ast.to_query_string(printer: IntrospectionPrinter.new)
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)>)


83
84
85
86
# File 'lib/graphql/schema/printer.rb', line 83

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



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

def print_schema
  print(@document)
end


93
94
95
96
# File 'lib/graphql/schema/printer.rb', line 93

def print_type(type)
  node = @document_from_schema.build_type_definition_node(type)
  print(node)
end