From 30c69d0417be1b580056e193034e8b93ab04cc0c Mon Sep 17 00:00:00 2001 From: Daniel Flynn Date: Wed, 14 Apr 2021 16:43:16 -0400 Subject: [PATCH 1/2] update: migrate have_operation to have_field - Migrate the have_operation matcher to have_field to communicate its matching on field entities instead of operation --- README.md | 2 +- RELEASE_NOTES.md | 5 +++++ docs/{have_operation.md => have_field.md} | 6 +++--- lib/rspec/graphql_response/matchers.rb | 2 +- .../matchers/{have_operation.rb => have_field.rb} | 6 +++--- lib/rspec/graphql_response/validators.rb | 2 +- .../validators/{have_operation.rb => have_field.rb} | 2 +- .../{have_operation_spec.rb => have_field_spec.rb} | 10 +++++----- ...tion_negated_spec.rb => have_field_negated_spec.rb} | 4 ++-- .../{have_operation_spec.rb => have_field_spec.rb} | 4 ++-- 10 files changed, 24 insertions(+), 19 deletions(-) rename docs/{have_operation.md => have_field.md} (80%) rename lib/rspec/graphql_response/matchers/{have_operation.rb => have_field.rb} (65%) rename lib/rspec/graphql_response/validators/{have_operation.rb => have_field.rb} (93%) rename spec/graphql_response/matchers/{have_operation_spec.rb => have_field_spec.rb} (68%) rename spec/graphql_response/validators/{have_operation_negated_spec.rb => have_field_negated_spec.rb} (88%) rename spec/graphql_response/validators/{have_operation_spec.rb => have_field_spec.rb} (87%) diff --git a/README.md b/README.md index 31bfd61..102d4d7 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Configuration: Custom Matchers: - [have_errors](/docs/have_errors.md) - validates errors, or lack of, on the GraphQL response -- [have_operation](/docs/have_operation.md) - validates the presence of a specified graphql operation in the graphql response +- [have_field](/docs/have_field.md) - validates the presence of a specified graphql operation in the graphql response Context / Describe Helper Methods: diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 533abe2..45e7df4 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -4,6 +4,11 @@ Release notes for various versions of RSpec::GraphQLResponse See [the upgrade guide](/UPGRADE.md) for details on changes between versions and how to upgrade. +## v0.5.0 - Helper API change + +- Fully deprecates `operation`. +- Renames `have_operation` to `have_field` to clarify its use. + ## v0.4.0 - Helper API change ### Breaking Changes diff --git a/docs/have_operation.md b/docs/have_field.md similarity index 80% rename from docs/have_operation.md rename to docs/have_field.md index 857a44a..bfba606 100644 --- a/docs/have_operation.md +++ b/docs/have_field.md @@ -1,4 +1,4 @@ -# Validate a response operation with `have_operation(name)` +# Validate a response operation with `have_field(name)` Check for the presence of an operation's results. Useful when you want to ensure an operation exists before retrieving the operation's results. @@ -18,7 +18,7 @@ RSpec.describe My::Characters, type: :graphql do it "has the characters" do - expect(response).to have_operation(:characters) + expect(response).to have_field(:characters) end end @@ -39,7 +39,7 @@ RSpec.describe My::Characters, type: :graphql do it "does not have books" do - expect(response).to_not have_operation(:books) + expect(response).to_not have_field(:books) end end diff --git a/lib/rspec/graphql_response/matchers.rb b/lib/rspec/graphql_response/matchers.rb index dfcdcb2..bed26e1 100644 --- a/lib/rspec/graphql_response/matchers.rb +++ b/lib/rspec/graphql_response/matchers.rb @@ -12,4 +12,4 @@ def self.add_matcher(name, &matcher) end require_relative "matchers/have_errors" -require_relative "matchers/have_operation" +require_relative "matchers/have_field" diff --git a/lib/rspec/graphql_response/matchers/have_operation.rb b/lib/rspec/graphql_response/matchers/have_field.rb similarity index 65% rename from lib/rspec/graphql_response/matchers/have_operation.rb rename to lib/rspec/graphql_response/matchers/have_field.rb index 784ac84..ae61db6 100644 --- a/lib/rspec/graphql_response/matchers/have_operation.rb +++ b/lib/rspec/graphql_response/matchers/have_field.rb @@ -1,6 +1,6 @@ -RSpec::GraphQLResponse.add_matcher :have_operation do |operation_name| +RSpec::GraphQLResponse.add_matcher :have_field do |operation_name| match do |response| - validator = RSpec::GraphQLResponse.validator(:have_operation) + validator = RSpec::GraphQLResponse.validator(:have_field) @result = validator.validate(response, operation_name: operation_name) @result.valid? @@ -11,7 +11,7 @@ end match_when_negated do |response| - validator = RSpec::GraphQLResponse.validator(:have_operation) + validator = RSpec::GraphQLResponse.validator(:have_field) @result = validator.validate_negated(response, operation_name: operation_name) @result.valid? diff --git a/lib/rspec/graphql_response/validators.rb b/lib/rspec/graphql_response/validators.rb index 6103328..1ce3972 100644 --- a/lib/rspec/graphql_response/validators.rb +++ b/lib/rspec/graphql_response/validators.rb @@ -23,4 +23,4 @@ def self.validator(name) end require_relative "validators/have_errors" -require_relative "validators/have_operation" +require_relative "validators/have_field" diff --git a/lib/rspec/graphql_response/validators/have_operation.rb b/lib/rspec/graphql_response/validators/have_field.rb similarity index 93% rename from lib/rspec/graphql_response/validators/have_operation.rb rename to lib/rspec/graphql_response/validators/have_field.rb index 6c108e8..c2f1caa 100644 --- a/lib/rspec/graphql_response/validators/have_operation.rb +++ b/lib/rspec/graphql_response/validators/have_field.rb @@ -1,4 +1,4 @@ -RSpec::GraphQLResponse.add_validator :have_operation do +RSpec::GraphQLResponse.add_validator :have_field do failure_message :nil, "Cannot evaluate operations on nil" failure_message :not_found, ->(expected, actual) { "Expected to find operation result named #{expected}, but did not find it\n\t#{actual}" } diff --git a/spec/graphql_response/matchers/have_operation_spec.rb b/spec/graphql_response/matchers/have_field_spec.rb similarity index 68% rename from spec/graphql_response/matchers/have_operation_spec.rb rename to spec/graphql_response/matchers/have_field_spec.rb index 97e51db..41beee8 100644 --- a/spec/graphql_response/matchers/have_operation_spec.rb +++ b/spec/graphql_response/matchers/have_field_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe RSpec::GraphQLResponse, "matcher#have_operation", type: :graphql do +RSpec.describe RSpec::GraphQLResponse, "matcher#have_field", type: :graphql do graphql_operation <<-GQL query CharacterList { characters { @@ -12,14 +12,14 @@ it "is valid" do expect(response).to_not have_errors - expect(response).to have_operation(:characters) + expect(response).to have_field(:characters) end it "is invalid when negated" do expect(response).to_not have_errors expect { - expect(response).to_not have_operation(:characters) + expect(response).to_not have_field(:characters) }.to raise_error(/Expected not to find operation result named characters/) end end @@ -29,14 +29,14 @@ expect(response).to_not have_errors expect { - expect(response).to have_operation(:operation_not_present) + expect(response).to have_field(:operation_not_present) }.to raise_error(/Expected to find operation result named operation_not_present/) end it "is valid when negated" do expect(response).to_not have_errors - expect(response).to_not have_operation(:operation_not_present) + expect(response).to_not have_field(:operation_not_present) end end end diff --git a/spec/graphql_response/validators/have_operation_negated_spec.rb b/spec/graphql_response/validators/have_field_negated_spec.rb similarity index 88% rename from spec/graphql_response/validators/have_operation_negated_spec.rb rename to spec/graphql_response/validators/have_field_negated_spec.rb index 0c109f8..8dc64df 100644 --- a/spec/graphql_response/validators/have_operation_negated_spec.rb +++ b/spec/graphql_response/validators/have_field_negated_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe RSpec::GraphQLResponse, "#have_operation validator", type: :graphql do +RSpec.describe RSpec::GraphQLResponse, "#have_field validator", type: :graphql do graphql_operation <<-GQL query CharacterList { characters { @@ -11,7 +11,7 @@ let(:operation_name) { nil } subject(:result) do - validator = RSpec::GraphQLResponse.validator :have_operation + validator = RSpec::GraphQLResponse.validator :have_field validator.validate_negated(response, operation_name: operation_name) end diff --git a/spec/graphql_response/validators/have_operation_spec.rb b/spec/graphql_response/validators/have_field_spec.rb similarity index 87% rename from spec/graphql_response/validators/have_operation_spec.rb rename to spec/graphql_response/validators/have_field_spec.rb index 9099c1c..3a0303d 100644 --- a/spec/graphql_response/validators/have_operation_spec.rb +++ b/spec/graphql_response/validators/have_field_spec.rb @@ -1,4 +1,4 @@ -RSpec.describe RSpec::GraphQLResponse, "#have_operation validator", type: :graphql do +RSpec.describe RSpec::GraphQLResponse, "#have_field validator", type: :graphql do graphql_operation <<-GQL query CharacterList { characters { @@ -11,7 +11,7 @@ let(:operation_name) { nil } subject(:result) do - validator = RSpec::GraphQLResponse.validator :have_operation + validator = RSpec::GraphQLResponse.validator :have_field validator.validate(response, operation_name: operation_name) end From 724202c7cb2cd155708f4d963614b7d533ec442e Mon Sep 17 00:00:00 2001 From: Daniel Flynn Date: Wed, 14 Apr 2021 16:51:52 -0400 Subject: [PATCH 2/2] cleanup: remove old operation references and docs --- Gemfile.lock | 2 +- README.md | 13 ++++---- lib/rspec/graphql_response/helpers.rb | 1 - .../graphql_response/helpers/operation.rb | 6 ---- lib/rspec/graphql_response/version.rb | 2 +- .../helpers/operation_spec.rb | 31 ------------------- 6 files changed, 8 insertions(+), 47 deletions(-) delete mode 100644 lib/rspec/graphql_response/helpers/operation.rb delete mode 100644 spec/graphql_response/helpers/operation_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1e3b444..83228be 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rspec-graphql_response (0.4.1) + rspec-graphql_response (0.5.0) graphql (>= 1.0) rspec (>= 3.0) diff --git a/README.md b/README.md index 102d4d7..f282a60 100644 --- a/README.md +++ b/README.md @@ -162,11 +162,11 @@ RSpec.describe Some::Thing, type: :graphql do end ``` -#### Retrieve operation results with `operation` +#### Retrieve response results with `response_data` Now that the GraphQL query has been executed and a response has been obtained, it's time to check for the results of a GraphQL operation. In the previous example, the spec is expecting to find `data` with `characters` in the response hash. To reduce the -nested hash checking, use the built-in `operation` method to retrieve the `characters`: +nested hash checking, use the built-in `response_data` method to retrieve the `characters`: ```ruby RSpec.describe Some::Thing, type: :graphql do @@ -180,18 +180,17 @@ RSpec.describe Some::Thing, type: :graphql do GQL it "executes the query" do - characters = operation(:characters) - - expect(characters).to include( + expect(response_data :characters).to include( # ... ) end end ``` -Note the lack of `response` use here. Internally, the `operation` method uses the `response` to obtain the data requested. This +Note the lack of `response` use here. Internally, the `response_data` method uses the `response` to obtain the data requested. This means the entire chain of operations from executing the GraphQL request, to converting the response into a hash, and digging -through the results to find the correction operation, has been handled behind the scenes. +through the results to find the correction operation, has been handled behind the scenes. To see more examples of how to use +`response_data` dig through your response check out it's full documenation [here.](/docs/response_data.md) ## Development diff --git a/lib/rspec/graphql_response/helpers.rb b/lib/rspec/graphql_response/helpers.rb index ac295f2..f9bcafa 100644 --- a/lib/rspec/graphql_response/helpers.rb +++ b/lib/rspec/graphql_response/helpers.rb @@ -46,6 +46,5 @@ def self.add_helper(name, scope: :spec, &helper) # spec level helpers require_relative "helpers/execute_graphql" -require_relative "helpers/operation" require_relative "helpers/response" require_relative "helpers/response_data" diff --git a/lib/rspec/graphql_response/helpers/operation.rb b/lib/rspec/graphql_response/helpers/operation.rb deleted file mode 100644 index d320687..0000000 --- a/lib/rspec/graphql_response/helpers/operation.rb +++ /dev/null @@ -1,6 +0,0 @@ -RSpec::GraphQLResponse.add_helper :operation do |name| - warn 'WARNING: operation has been deprecated in favor of response_data. This helper will be removed in v0.5' - return nil unless response.is_a? Hash - - response.dig("data", name.to_s) -end diff --git a/lib/rspec/graphql_response/version.rb b/lib/rspec/graphql_response/version.rb index 06e5b21..ec3f580 100644 --- a/lib/rspec/graphql_response/version.rb +++ b/lib/rspec/graphql_response/version.rb @@ -1,5 +1,5 @@ module RSpec module GraphQLResponse - VERSION = "0.4.1" + VERSION = "0.5.0" end end diff --git a/spec/graphql_response/helpers/operation_spec.rb b/spec/graphql_response/helpers/operation_spec.rb deleted file mode 100644 index 5bb93a1..0000000 --- a/spec/graphql_response/helpers/operation_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -RSpec.describe RSpec::GraphQLResponse, "helper#operation", type: :graphql do - context "graphql response with no data" do - it "returns nil" do - characters = operation(:characters) - - expect(characters).to be_nil - end - end - - context "graphql response with data" do - graphql_operation <<-GQL - query { - characters { - id, - name - } - } - GQL - - it "retrieves the named operation from the graphql response" do - characters = operation(:characters) - - expect(characters).to_not be_nil - expect(characters).to include( - { "id" => "1", "name" => "Jam" }, - { "id" => "2", "name" => "Redemption" }, - { "id" => "3", "name" => "Pet" } - ) - end - end -end