diff --git a/lib/rspec/graphql_response/helpers.rb b/lib/rspec/graphql_response/helpers.rb index f9bcafa..33d7561 100644 --- a/lib/rspec/graphql_response/helpers.rb +++ b/lib/rspec/graphql_response/helpers.rb @@ -43,6 +43,7 @@ def self.add_helper(name, scope: :spec, &helper) require_relative "helpers/graphql_context" require_relative "helpers/graphql_operation" require_relative "helpers/graphql_variables" +require_relative "helpers/with_schema" # spec level helpers require_relative "helpers/execute_graphql" diff --git a/lib/rspec/graphql_response/helpers/with_schema.rb b/lib/rspec/graphql_response/helpers/with_schema.rb new file mode 100644 index 0000000..b40ba91 --- /dev/null +++ b/lib/rspec/graphql_response/helpers/with_schema.rb @@ -0,0 +1,4 @@ +RSpec::GraphQLResponse.add_context_helper :with_schema do |schema| + # return unless schema.is_a? == GraphQL::Schema + RSpec::GraphQLResponse.configuration.graphql_schema = schema +end diff --git a/spec/graphql/alternate_schema.rb b/spec/graphql/alternate_schema.rb new file mode 100644 index 0000000..b998bed --- /dev/null +++ b/spec/graphql/alternate_schema.rb @@ -0,0 +1,6 @@ +require "graphql/alternate_schema/alternate_types" +require "graphql/alternate_schema/alternate_queries" + +class AlternateSchema < GraphQL::Schema + query AlternateQueries +end diff --git a/spec/graphql/alternate_schema/alternate_queries.rb b/spec/graphql/alternate_schema/alternate_queries.rb new file mode 100644 index 0000000..84701eb --- /dev/null +++ b/spec/graphql/alternate_schema/alternate_queries.rb @@ -0,0 +1,7 @@ +require "graphql/queries/characters" + +class AlternateQueries < GraphQL::Schema::Object + graphql_name "Query" + + field :employees, resolver: Queries::Characters +end diff --git a/spec/graphql/example_types.rb b/spec/graphql/alternate_schema/alternate_types.rb similarity index 100% rename from spec/graphql/example_types.rb rename to spec/graphql/alternate_schema/alternate_types.rb diff --git a/spec/graphql/example_schema.rb b/spec/graphql/example_schema.rb index 7bbf1f1..4e212c3 100644 --- a/spec/graphql/example_schema.rb +++ b/spec/graphql/example_schema.rb @@ -1,5 +1,5 @@ -require "graphql/example_types" -require "graphql/example_queries" +require "graphql/example_schema/example_types" +require "graphql/example_schema/example_queries" class ExampleSchema < GraphQL::Schema query ExampleQueries diff --git a/spec/graphql/example_queries.rb b/spec/graphql/example_schema/example_queries.rb similarity index 100% rename from spec/graphql/example_queries.rb rename to spec/graphql/example_schema/example_queries.rb diff --git a/spec/graphql/example_schema/example_types.rb b/spec/graphql/example_schema/example_types.rb new file mode 100644 index 0000000..e535a3e --- /dev/null +++ b/spec/graphql/example_schema/example_types.rb @@ -0,0 +1 @@ +require "graphql/types/response/character" diff --git a/spec/graphql_response/helpers/with_schema_spec.rb b/spec/graphql_response/helpers/with_schema_spec.rb new file mode 100644 index 0000000..1b19f6d --- /dev/null +++ b/spec/graphql_response/helpers/with_schema_spec.rb @@ -0,0 +1,44 @@ +require "graphql/alternate_schema" + +RSpec.describe RSpec::GraphQLResponse, "helper#with_schema", type: :graphql do + with_schema AlternateSchema + + graphql_operation <<-GQL + query { + employees { + id, + name + } + } + GQL + + it "queries the correct schema with employees" do + expect(response).to_not be_nil + expect(response_data).to include( + "employees" => [ + { + "id" => "1", + "name" => "Jam", + "friends" => [ + { "id" => "2", "name" => "Redemption" } + ] + }, + { + "id" => "2", + "name" => "Redemption", + "friends" => [ + { "id" => "1", "name" => "Jam" }, + { "id" => "3", "name" => "Pet" } + ] + }, + { + "id" => "3", + "name" => "Pet", + "friends" => [ + { "id" => "2", "name" => "Redemption" } + ] + } + ] + ) + end +end