Search code examples
rubygraphql-ruby

Redefine custom subscriptions on GraphQL Schema


We are facing an issue where we would like to replace a custom subscriptions object on the GraphQL Schema on the fly (mostly for testing). We currently have the following schema:

class MySchema < GraphQL::Schema
  query CustomQueryType
  mutation CustomMutationType
  subscription CustomSuscriptionType

  use CustomSubscriptions # We want to replace this on the fly
end

CustomSubscriptions itself derives from GraphQL::Subscriptions and uses some external dependencies to store/trigger subscriptions. While testing, we don't necessarily want to deal with these dependencies, so we would like to replace the subscriptions there with some mock or some implementation that does not require the same external dependencies. For example, I'd like to do:

class SubscriptionsTest < ActiveSupport::TestCase
 def setup
   mySchema.use(MyTestSubscriptions) # This doesn't work :(
 end

 ...
end

However, that will not remove our CustomSubscriptions from the schema. I have tried to update the schema to allow setting the subscriptions instance, but so far with no success.

Is there a way to accomplish what I'm after? Some setter/helper method that I missed?


Solution

  • Found a solution with some help from the maintainer. One can access the singlelton instance of the schema via graphql_definition. It is then possible to modify that instance as one wishes.

    For example, to set the subscriptions instance as desired one can use:

    @subscriptions  = MyTestSubscription.new(schema: MeistertaskSchema.graphql_definition)
    MeistertaskSchema.graphql_definition.subscriptions = @subscriptions