Skip to content

GraphQL::Testing::Helpers are incompatible with named VisibilityProfiles #5430

@timlkelly

Description

@timlkelly

Describe the bug

When the Schema uses named Visibility Profiles, the visibility_profile kwarg is required; however, the GraphQL::Testing::Helpers do not support the visibility_profile kwarg. I looked for other examples or Github issues for how to use the test helpers with Visibility, but I could not find any. Hope I didn't miss something.

Versions

graphql: 2.5.11
rails: n/a
rspec: 3.13
ruby: 3.4.5

GraphQL schema

n/a; see below.

GraphQL query

n/a; see below.

Steps to reproduce

I was able to recreate a minimal scenario within RSpec below. Running the file as an rspec test shows the errors, rspec graphql_test_helpers_spec.rb.

# graphql_test_helpers_spec.rb

require 'bundler/inline'

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

RSpec.describe GraphQL::Testing::Helpers do
  include GraphQL::Testing::Helpers

  class Cake
    def kind = 'strawberry'
  end

  class CakeSchema < GraphQL::Schema
    use GraphQL::Schema::Visibility, profiles: { cake_viewer: {} }

    class CakeType < GraphQL::Schema::Object
      field :kind, String
    end

    class Query < GraphQL::Schema::Object
      field :cakes, [CakeType]
    end

    query(Query)
  end

  it 'run_graphql_field' do
    kind = run_graphql_field(CakeSchema, 'Cake.kind', Cake.new, context: {})
    expect(kind).to eq('strawberry')
  end

  it 'with_resolution_context' do
    with_resolution_context(CakeSchema, type: 'Cake', object: Cake.new, context: {}) do |rc|
      expect(rc.run_graphql_field('kind')).to eq('strawberry')
    end
  end
end

Expected behavior

The GraphQL::Testing::Helpers work with Schemas that use named Visibility Profiles.

Actual behavior

❯ rspec graphql_test_helpers_spec.rb
FF

Failures:

  1) GraphQL::Testing::Helpers run_graphql_field
     Failure/Error: kind = run_graphql_field(CakeSchema, 'Cake.kind', Cake.new, context: {})

     ArgumentError:
       CakeSchema expects a visibility profile, but `visibility_profile:` wasn't passed. Provide a `visibility_profile:` value or add `dynamic: true` to your visibility configuration.
     # ./graphql_test_helpers_spec.rb:33:in 'block (2 levels) in <top (required)>'

  2) GraphQL::Testing::Helpers with_resolution_context
     Failure/Error: expect(rc.run_graphql_field('kind')).to eq('strawberry')

     ArgumentError:
       CakeSchema expects a visibility profile, but `visibility_profile:` wasn't passed. Provide a `visibility_profile:` value or add `dynamic: true` to your visibility configuration.
     # ./graphql_test_helpers_spec.rb:39:in 'block (3 levels) in <top (required)>'
     # ./graphql_test_helpers_spec.rb:38:in 'block (2 levels) in <top (required)>'

Finished in 0.00349 seconds (files took 0.2879 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./graphql_test_helpers_spec.rb:32 # GraphQL::Testing::Helpers run_graphql_field
rspec ./graphql_test_helpers_spec.rb:37 # GraphQL::Testing::Helpers with_resolution_context

When I add the visibility_profile: :cake_viewer kwarg to the helpers, I get a ArgumentError: unknown keyword: :visibility_profile error.

kind = run_graphql_field(CakeSchema, 'Cake.kind', Cake.new, context: {}, visibility_profile: :cake_viewer)

with_resolution_context(CakeSchema, type: 'Cake', object: Cake.new, context: {}, visibility_profile: :cake_viewer) do |rc|
  # ...
end
~/Desktop  ❯ rspec graphql_test_helpers_spec.rb
FF

Failures:

  1) GraphQL::Testing::Helpers run_graphql_field
     Failure/Error: kind = run_graphql_field(CakeSchema, 'Cake.kind', Cake.new, context: {}, visibility_profile: :cake_viewer)

     ArgumentError:
       unknown keyword: :visibility_profile
     # ./graqphl_test_helpers_spec.rb:33:in 'block (2 levels) in <top (required)>'

  2) GraphQL::Testing::Helpers with_resolution_context
     Failure/Error:
       with_resolution_context(CakeSchema, type: 'Cake', object: Cake.new, context: {}, visibility_profile: :cake_viewer) do |rc|
         expect(rc.run_graphql_field('kind')).to eq('strawberry')
       end

     ArgumentError:
       unknown keyword: :visibility_profile
     # ./graphql_test_helpers_spec.rb:38:in 'block (2 levels) in <top (required)>'

Finished in 0.00311 seconds (files took 0.27879 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./graphql_test_helpers_spec.rb:32 # GraphQL::Testing::Helpers run_graphql_field
rspec ./graphql_test_helpers_spec.rb:37 # GraphQL::Testing::Helpers with_resolution_context

Additional context

I was able to get this to work locally with some minor changes to the Testing Helpers, I have a branch on my fork here.

I wanted to open an issue first to see if this is a valid problem before opening a pull request. Also, looking at the helpers_spec.rb, I wasn't entirely sure the best way to recreate the scenario there while also preserving the existing behavior, especially because I saw there's already an optional configuration for using Warden. If you have suggestions on the test setup you'd like to see, I'm happy to open a pull request to address this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions