Module: GraphQL::Schema::Printer

Extended by:
Printer
Included in:
Printer
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

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

Instance Method Summary collapse

Instance Method Details

Return the GraphQL schema string for the introspection type system



33
34
35
36
37
38
39
40
41
# File 'lib/graphql/schema/printer.rb', line 33

def print_introspection_schema
  query_root = ObjectType.define(name: "Root")
  schema = GraphQL::Schema.define(query: query_root)
  blacklist = ->(m, ctx) { m == query_root }

  warden = GraphQL::Schema::Warden.new(blacklist, schema: schema, context: nil)

  print_filtered_schema(schema, warden: warden)
end

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

Parameters:

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


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphql/schema/printer.rb', line 18

def print_schema(schema, context: nil, only: nil, except: nil)
  blacklist = 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

  warden = GraphQL::Schema::Warden.new(blacklist, schema: schema, context: context)

  print_filtered_schema(schema, warden: warden)
end