# frozen_string_literal: true require "spec_helper" describe GraphQL::Schema::Printer do let(:schema) { node_type = GraphQL::InterfaceType.define do name "Node" field :id, !types.ID end choice_type = GraphQL::EnumType.define do name "Choice" value "FOO", value: :foo value "BAR", value: :bar value "BAZ", deprecation_reason: 'Use "BAR".' value "WOZ", deprecation_reason: GraphQL::Directive::DEFAULT_DEPRECATION_REASON end sub_input_type = GraphQL::InputObjectType.define do name "Sub" description "Test" input_field :string, types.String, 'Something' input_field :int, types.Int, 'Something' end variant_input_type = GraphQL::InputObjectType.define do name "Varied" input_field :id, types.ID input_field :int, types.Int input_field :float, types.Float input_field :bool, types.Boolean input_field :enum, choice_type, default_value: :foo input_field :sub, types[sub_input_type] end comment_type = GraphQL::ObjectType.define do name "Comment" description "A blog comment" interfaces [node_type] field :id, !types.ID 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 field :comments, types[!comment_type] field :comments_count, !types.Int, deprecation_reason: 'Use "comments".' end audio_type = GraphQL::ObjectType.define do name "Audio" field :id, !types.ID field :name, !types.String field :duration, !types.Int end image_type = GraphQL::ObjectType.define do name "Image" field :id, !types.ID field :name, !types.String field :width, !types.Int field :height, !types.Int end media_union_type = GraphQL::UnionType.define do name "Media" description "Media objects" possible_types [image_type, audio_type] end query_root = GraphQL::ObjectType.define do name "Query" description "The query root of this schema" field :post do type post_type argument :id, !types.ID, 'Post ID' argument :varied, variant_input_type, default_value: { id: "123", int: 234, float: 2.3, enum: :foo, sub: [{ string: "str" }] } argument :variedWithNulls, variant_input_type, default_value: { id: nil, int: nil, float: nil, enum: nil, sub: nil } resolve ->(obj, args, ctx) { Post.find(args["id"]) } end end create_post_mutation = GraphQL::Relay::Mutation.define do name "CreatePost" description "Create a blog post" input_field :title, !types.String input_field :body, !types.String return_field :post, post_type resolve ->(_, _, _) { } end mutation_root = GraphQL::ObjectType.define do name "Mutation" field :createPost, field: create_post_mutation.field end subscription_root = GraphQL::ObjectType.define do name "Subscription" field :post do type post_type argument :id, !types.ID resolve ->(_, _, _) { } end end GraphQL::Schema.define( query: query_root, mutation: mutation_root, subscription: subscription_root, resolve_type: ->(a,b,c) { :pass }, orphan_types: [media_union_type] ) } describe ".print_introspection_schema" do it "returns the schema as a string for the introspection types" do # From https://github.com/graphql/graphql-js/blob/6a0e00fe46951767287f2cc62e1a10b167b2eaa6/src/utilities/__tests__/schemaPrinter-test.js#L599 expected = <(member, ctx) { case member when GraphQL::ScalarType true when GraphQL::BaseType ctx[:names].include?(member.name) when GraphQL::Argument member.name != "id" else member.deprecation_reason.nil? end } context = { names: ["Varied", "Choice", "Subscription"] } assert_equal expected.chomp, schema.to_definition(context: context, only: only_filter) end it "applies an `except` filter" do expected = <(member, ctx) { ctx[:names].include?(member.name) || (member.respond_to?(:deprecation_reason) && member.deprecation_reason) } context = { names: ["Varied", "Image", "Sub"] } assert_equal expected.chomp, schema.to_definition(context: context, except: except_filter) end describe "#print_type" do it "returns the type schema as a string" do expected = <