Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept blocks to reference schema root types #5055

Merged
merged 4 commits into from
Aug 8, 2024
Merged

Conversation

rmosolgo
Copy link
Owner

@rmosolgo rmosolgo commented Aug 6, 2024

With this, the schema could defer loading a root type until necessary (thus also deferring loading all types referenced by it, resolvers, etc).

In this implementation, calling schema.query returns the deferred definition which means that if, say, a plug-in references that root type, all the magic is lost.

To be really effective, we'd also need some plugin behavior like extend(:query) { ... } where callbacks could be registered by the plugin, then called by GraphQL-Ruby after the type is actually loaded. I think that can be added later.

Part of #5014

TODO:

  • Add a test that proves that Rails doesn't load types/query.rb until a query is run when using Schema::Subset.
  • Write a Rubocop rule for it

@rmosolgo rmosolgo added this to the 2.3.13 milestone Aug 8, 2024
@rmosolgo
Copy link
Owner Author

rmosolgo commented Aug 8, 2024

I confirmed this has the desired effect by making a Rails app and configuring the schema with:

puts ">>>> loading #{__FILE__}"

class GraphqlLazyLoadTestSchema < GraphQL::Schema
  self.use_schema_subset = true

  mutation { Types::MutationType }
  query { Types::QueryType }
  # ...
end 

Then, I ran two queries, and watched the Rails server logs:

query {
  __typename
}
# Processing by GraphqlController#execute as */*
#   Parameters: {"query"=>"query {\n  __typename\n}", "graphql"=>{"query"=>"query {\n  __typename\n}"}}
# >>>> loading /Users/rmosolgo/code/graphql_lazy_load_test/app/graphql/graphql_lazy_load_test_schema.rb
# >>>> loading /Users/rmosolgo/code/graphql_lazy_load_test/app/graphql/types/query_type.rb
# Completed 200 OK in 15ms (Views: 0.2ms | ActiveRecord: 0.0ms | Allocations: 6307)

Then:

mutation {
  __typename
}
# Started POST "/graphql" for 127.0.0.1 at 2024-08-08 10:57:45 -0400
# Processing by GraphqlController#execute as */*
#   Parameters: {"query"=>"mutation {\n  __typename\n}", "graphql"=>{"query"=>"mutation {\n  __typename\n}"}}
# >>>> loading /Users/rmosolgo/code/graphql_lazy_load_test/app/graphql/types/mutation_type.rb
# Completed 200 OK in 3ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 1025)

So, it works: running a query doesn't cause types/mutation.rb to be loaded 🎉

@rmosolgo rmosolgo merged commit 62a2df0 into master Aug 8, 2024
15 checks passed
@rmosolgo rmosolgo deleted the lazy-root-types branch August 8, 2024 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants