rmosolgo/graphql-ruby

Improve visiblity / type filtering

Closed this issue · 4 comments

In #4998, I started on a new schema visibility implementation. I have a few goals here:

  • Make it possible to load GraphQL types lazily -- only loading types needed by a given query.
  • Make it possible to use cached schema subsets (don't have to recalculate the visible schema on every query -- when possible)
  • Improve logging and debugging of visibility (especially when types are hidden)
  • Make a very smooth migration process:
    • Use the Query#types interface to support logging when the two systems would diverge
    • Clearly document the expectations of the new system (Warden was basically never documented, and arguably tried to do too much, but couldn't do everything)

I'm opening this issue for my own purposes but also in case anyone wants to share any thoughts or questions on the work.

TODO:

  • Build a Subset cache-by-name system
  • Make field return types lazy-loadable #5054
  • Make root types lazy-loadable from the schema #5055
  • Update the install generator to add Rubocop rules to rails (when the file is present)
  • Make it easier to start using Subset than self.use_schema_subset = true
  • Make a way to preload everything in Production
  • Warn when Warden is used without being explicitly included

I'm having an issue with the latest version of the gem related to this work in the tests for my mutations.

In my tests I call this

context = { current_user: user }
Mutations::AcceptDiscussionEmailInvite.new(object: nil, field: nil, context: context).resolve(**args)

And with the latest gem version I am getting this error:

NoMethodError: undefined method `types' for { :current_user=> <User> }
test/graphql/mutations/accept_discussion_email_invite_test.rb:8:in `new'

Any thoughts on what I should be passing into the mutation as the context for my tests? Looks like a hash doesn't work anymore.

Figured it out,

context = { current_user: user }
context = GraphQL::Query::Context.new(query: GraphQL::Query.new(MapleSchema), values: context)
Mutations::AcceptDiscussionEmailInvite.new(object: nil, field: nil, context: context).resolve(**args)

Hey, sorry for the disruption there. Initializing Mutations outside of query execution isn't a formally supported API, but I'm glad you found something that works. In a pinch, another way to get a context is:

query = MySchema.query_class.new(self, "{ __typename }", context: { ... } )
context = query.context

Also, you could try the run_graphql_field helper to get nearly-full runtime context: https://graphql-ruby.org/testing/helpers.html

This is all released in 2.4.0