graphql-ruby icon indicating copy to clipboard operation
graphql-ruby copied to clipboard

`GraphQL::Schema::Visibility` preload crashes if `query` type is not set first

Open cocoahero opened this issue 2 months ago • 2 comments

If the GraphQL::Schema::Visibility plugin is "used" with preload: true prior to configuring the schema's root types, an exception will be raised.

NoMethodError: undefined method 'kind' for nil

gems/graphql-2.5.14/lib/graphql/schema/visibility/profile.rb:177:in 'GraphQL::Schema::Visibility::Profile#field'
gems/graphql-2.5.14/lib/graphql/schema/visibility/profile.rb:332:in 'block in GraphQL::Schema::Visibility::Profile#preload'
gems/graphql-2.5.14/lib/graphql/schema/visibility/profile.rb:328:in 'Array#each'
gems/graphql-2.5.14/lib/graphql/schema/visibility/profile.rb:328:in 'GraphQL::Schema::Visibility::Profile#preload'
gems/graphql-2.5.14/lib/graphql/schema/visibility.rb:99:in 'block in GraphQL::Schema::Visibility#preload'
gems/graphql-2.5.14/lib/graphql/schema/visibility.rb:97:in 'Hash#each'
gems/graphql-2.5.14/lib/graphql/schema/visibility.rb:97:in 'GraphQL::Schema::Visibility#preload'
gems/graphql-2.5.14/lib/graphql/schema/visibility.rb:44:in 'GraphQL::Schema::Visibility#initialize'
gems/graphql-2.5.14/lib/graphql/schema/visibility.rb:20:in 'Class#new'
gems/graphql-2.5.14/lib/graphql/schema/visibility.rb:20:in 'GraphQL::Schema::Visibility.use'
gems/graphql-2.5.14/lib/graphql/schema.rb:322:in 'GraphQL::Schema.use'

This is likely a programmer error, however a sanity check with proper error message might be more helpful. It is not immediately obvious that certain plugins need to be configured in a certain order.

cocoahero avatar Oct 28 '25 18:10 cocoahero

Hey, thanks for this report! I agree that a decent error message would be a big improvement. Like you said, there's no way to indicate or require a sequence dependency within the schema definition. It's possible to make these plugins handle other configurations which are added later, and that's certainly the nicest experience, but sometimes it's hard to do ... (And since the schema class is technically never closed, it's probably the rightest thing to do.)

I'll take a look soon and add a decent error to start with!

rmosolgo avatar Oct 29 '25 10:10 rmosolgo

@rmosolgo We ran into this as well

I think this could be part of the documentation? Would probably also be good to mention that subscriptions have to include the visibility profile when broadcasting, and maybe clarify what effects that has? For example, if you do Schema.subscriptions.trigger(:shared_subscription, {}, {}, scope: 123, context: { visibility_profile: :admin }) will this be broadcast to everyone subscribing to this subscription? or only those that are currently using this visibility profile? or is the visibility profile only used to actually find the subscription in the schema?

We had to update all our subscription broadcasts but it wasn't clear if the need to pass the visibility profile was to just find the subscription at all or if it would become like a second scope

Amnesthesia avatar Nov 23 '25 23:11 Amnesthesia