lib/graphql/schema/printer.rb in graphql-0.19.3 vs lib/graphql/schema/printer.rb in graphql-0.19.4

- old
+ new

@@ -16,11 +16,11 @@ end # Return the GraphQL schema string for the introspection type system def print_introspection_schema query_root = ObjectType.define do - name "Query" + name "Root" end schema = GraphQL::Schema.define(query: query_root) print_filtered_schema(schema, method(:is_spec_directive), method(:is_introspection_type)) end @@ -31,15 +31,22 @@ directive_definitions = directives.map{ |directive| print_directive(directive) } types = schema.types.values.select{ |type| type_filter.call(type) }.sort_by(&:name) type_definitions = types.map{ |type| print_type(type) } - [print_schema_definition(schema)].concat(directive_definitions) + [print_schema_definition(schema)].compact + .concat(directive_definitions) .concat(type_definitions).join("\n\n") end def print_schema_definition(schema) + 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) " #{operation_type}: #{object_type.name}\n" if object_type end.compact.join "schema {\n#{operations}}" @@ -82,14 +89,36 @@ " @deprecated(reason: #{field_or_enum_value.deprecation_reason.to_s.inspect})" end end end + module DescriptionPrinter + def print_description(definition, indentation='', first_in_block=true) + return '' unless definition.description + + description = indentation != '' && !first_in_block ? "\n" : "" + description << GraphQL::Language::Comments.commentize(definition.description, indent: indentation) + end + end + module ArgsPrinter - def print_args(field) + include DescriptionPrinter + def print_args(field, indentation = '') return if field.arguments.empty? - "(#{field.arguments.values.map{ |arg| print_input_value(arg) }.join(", ")})" + + field_arguments = field.arguments.values + + if field_arguments.all?{ |arg| !arg.description } + return "(#{field_arguments.map{ |arg| print_input_value(arg) }.join(", ")})" + end + + out = "(\n" + out << field_arguments.map.with_index{ |arg, i| + "#{print_description(arg, " #{indentation}", i == 0)} #{indentation}"\ + "#{print_input_value(arg)}" + }.join("\n") + out << "\n)" end def print_input_value(arg) if arg.default_value.nil? default_string = nil @@ -129,67 +158,93 @@ end module FieldPrinter include DeprecatedPrinter include ArgsPrinter + include DescriptionPrinter def print_fields(type) - type.all_fields.map{ |field| - " #{field.name}#{print_args(field)}: #{field.type}#{print_deprecated(field)}" + type.all_fields.map.with_index{ |field, i| + "#{print_description(field, ' ', i == 0)}"\ + " #{field.name}#{print_args(field, ' ')}: #{field.type}#{print_deprecated(field)}" }.join("\n") end end class DirectivePrinter extend ArgsPrinter + extend DescriptionPrinter def self.print(directive) + "#{print_description(directive)}"\ "directive @#{directive.name}#{print_args(directive)} "\ "on #{directive.locations.join(' | ')}" end end class ScalarPrinter + extend DescriptionPrinter def self.print(type) + "#{print_description(type)}"\ "scalar #{type.name}" end end class ObjectPrinter extend FieldPrinter + extend DescriptionPrinter def self.print(type) if type.interfaces.any? implementations = " implements #{type.interfaces.map(&:to_s).join(", ")}" else implementations = nil end - "type #{type.name}#{implementations} {\n#{print_fields(type)}\n}" + + "#{print_description(type)}"\ + "type #{type.name}#{implementations} {\n"\ + "#{print_fields(type)}\n"\ + "}" end end class InterfacePrinter extend FieldPrinter + extend DescriptionPrinter def self.print(type) + "#{print_description(type)}"\ "interface #{type.name} {\n#{print_fields(type)}\n}" end end class UnionPrinter + extend DescriptionPrinter def self.print(type) + "#{print_description(type)}"\ "union #{type.name} = #{type.possible_types.map(&:to_s).join(" | ")}" end end class EnumPrinter extend DeprecatedPrinter + extend DescriptionPrinter def self.print(type) values = type.values.values.map{ |v| " #{v.name}#{print_deprecated(v)}" }.join("\n") + values = type.values.values.map.with_index { |v, i| + "#{print_description(v, ' ', i == 0)}"\ + " #{v.name}#{print_deprecated(v)}" + }.join("\n") + "#{print_description(type)}"\ "enum #{type.name} {\n#{values}\n}" end end class InputObjectPrinter extend FieldPrinter + extend DescriptionPrinter def self.print(type) - fields = type.input_fields.values.map{ |field| " #{print_input_value(field)}" }.join("\n") + fields = type.input_fields.values.map.with_index{ |field, i| + "#{print_description(field, " ", i == 0)}"\ + " #{print_input_value(field)}" + }.join("\n") + "#{print_description(type)}"\ "input #{type.name} {\n#{fields}\n}" end end STRATEGIES = {