Class: GraphQL::Schema::Printer

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

Overview

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

Examples

Example: print your schema to standard output (via helper)

puts GraphQL::Schema::Printer.print_schema(MySchema)

Example: print your schema to standard output

puts GraphQL::Schema::Printer.new(MySchema).print_schema

Example: print a single type to standard output

class Types::Query < GraphQL::Schema::Object
  description "The query root of this schema"

  field :post, Types::Post, null: true
end

class Types::Post < GraphQL::Schema::Object
  description "A blog post"

  field :id, ID, null: false
  field :title, String, null: false
  field :body, String, null: false
end

class MySchema < GraphQL::Schema
  query(Types::Query)
end

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

Defined Under Namespace

Classes: IntrospectionPrinter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Parameters

  • schema (GraphQL::Schema)
  • context (Hash)
  • introspection (Boolean) — Should include the introspection types in the string?

:call-seq:

initialize(GraphQL::Schema schema, Hash context:, bool introspection:)


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

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

  @document = @document_from_schema.document
  @schema = schema
end

Instance Attribute Details

#schema ⇒ Object (readonly)

Returns the value of attribute schema.



47
48
49
# File 'lib/graphql/schema/printer.rb', line 47

def schema
  @schema
end

#warden ⇒ Object (readonly)

Returns the value of attribute warden.



47
48
49
# File 'lib/graphql/schema/printer.rb', line 47

def warden
  @warden
end

Class Method Details

Return the GraphQL schema string for the introspection type system



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graphql/schema/printer.rb', line 69

def self.print_introspection_schema
  query_root = Class.new(GraphQL::Schema::Object) do
    graphql_name "Root"
    field :throwaway_field, String
    # Not `def self.visible?`: Sorbet would treat it as a definition of `Printer.visible?`
    define_singleton_method(:visible?) { |ctx| false }
  end
  schema = Class.new(GraphQL::Schema) {
    query(query_root)
    use GraphQL::Schema::Visibility
    def self.visible?(member, _ctx)
      member.graphql_name != "Root"
    end
  }

  introspection_schema_ast = GraphQL::Language::DocumentFromSchemaDefinition.new(
    schema,
    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)>)

:call-seq:

print_schema(GraphQL::Schema schema, **args)


104
105
106
107
# File 'lib/graphql/schema/printer.rb', line 104

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



110
111
112
# File 'lib/graphql/schema/printer.rb', line 110

def print_schema
  print(@document) + "\n"
end


114
115
116
117
# File 'lib/graphql/schema/printer.rb', line 114

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