rmosolgo / graphql-ruby

Ruby implementation of GraphQL
http://graphql-ruby.org
MIT License
5.38k stars 1.39k forks source link

Directives with `.visible?` -> `false` are not fully removed from the schema #5172

Open erikkessler1 opened 12 hours ago

erikkessler1 commented 12 hours ago

Describe the bug

I'm trying to make it so that certain directives of a schema are only present in certain contexts (e.g., federation directives only included in federated contexts). From playing around with the .visible? hook, it seems like a directive that isn't visible has its definition removed, but the usage of the directive still exists. For example, the following script:

require 'bundler'
require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'graphql', '2.4.4'
end

class MyDirective < GraphQL::Schema::Directive
  locations OBJECT

  def self.visible?(context)
    context.fetch(:show_directives)
  end
end

class Query < GraphQL::Schema::Object
  directive MyDirective
  field :my_string, String, null: false
end

class MySchema < GraphQL::Schema
  use GraphQL::Schema::Visibility
  query Query
end

puts "# show_directives: true"
puts MySchema.to_definition(context: { show_directives: true })
puts "\n# show_directives: false"
puts MySchema.to_definition(context: { show_directives: false })

Produces the following output:

# show_directives: true
directive @myDirective on OBJECT

type Query @myDirective {
  myString: String!
}

# show_directives: false
type Query @myDirective {
  myString: String!
}

The output I would expect is:

# show_directives: false
type Query {
  myString: String!
}

Is that the expected behavior of the .visible? hook on directives?