{"id":13879020,"url":"https://github.com/testdouble/rspec-graphql_response","last_synced_at":"2025-07-16T15:30:29.677Z","repository":{"id":44638115,"uuid":"340453752","full_name":"testdouble/rspec-graphql_response","owner":"testdouble","description":"Verify ruby-graphql responses with a :graphql spec type","archived":false,"fork":false,"pushed_at":"2024-05-16T15:02:01.000Z","size":137,"stargazers_count":25,"open_issues_count":5,"forks_count":9,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-11-20T15:26:28.166Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/testdouble.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-02-19T18:18:28.000Z","updated_at":"2024-05-16T15:02:06.000Z","dependencies_parsed_at":"2024-01-13T21:10:26.024Z","dependency_job_id":null,"html_url":"https://github.com/testdouble/rspec-graphql_response","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Frspec-graphql_response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Frspec-graphql_response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Frspec-graphql_response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/testdouble%2Frspec-graphql_response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/testdouble","download_url":"https://codeload.github.com/testdouble/rspec-graphql_response/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-06T08:02:07.113Z","updated_at":"2024-11-24T07:31:29.202Z","avatar_url":"https://github.com/testdouble.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# Rspec::GraphQLResponse\n\nRSpec::GraphQLResponse provides a series of RSpec matchers, helper methods, and other configuration to help simplify\nthe testing of responses from the `graphql-ruby` gem and the `\u003cGraphQLSchemaName\u003e.execute` method.\n\n## \"Why Should I Use This?\"\n\nThere are a number of built-in helper methods and matchers that will allow you to skip the copy \u0026 paste work of executing\na GraphQL Schema `.execute`. Additionally, there are custom matchers and other bits that will help simplify your work in\nvalidating common peices of a graphql response.\n\nLastly, the work in this gem is geared toward customization for your own application's needs. Every API call used for building\nthe pieces of this gem are available to you, directly, in the `API / Development` documentation, below.\n\n## Installation\n\nYour app must have `graphql-ruby` and `rspec`. With that done, add this line to your application's Gemfile:\n\n```ruby\ngem 'rspec-graphql_response'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rspec-graphql_response\n\n## Full Documentation\n\n- [Release Notes](/RELEASE_NOTES.md)\n- [Upgrade Guide](/UPGRADE.md)\n\nThe full documentation for RSpec::GraphQLResponse can be found in the `/docs` folder.\n\nConfiguration:\n\n- [RSpec::GraphQLResponse.configure](/docs/configuration.md)\n- [Spec Type :graphql](/docs/graphql_spec_type.md)\n\nCustom Matchers:\n\n- [have_errors](/docs/have_errors.md) - validates errors, or lack of, on the GraphQL response\n- [have_field](/docs/have_field.md) - validates the presence of a specified graphql operation in the graphql response\n\nContext / Describe Helper Methods:\n\n- [graphql_operation](/docs/execute_graphql.md) - the operation to execute (i.e query or mutation)\n- [graphql_variables](/docs/execute_graphql.md) - a hash of variables the query expects\n- [graphql_context](/docs/execute_graphql.md) - the `context` of a query or mutation's resolver\n\nSpec Helper Methods:\n\n- [execute_graphql](/docs/execute_graphql.md) - executes a graphql call with the registered schema, query, variables and context\n- [response](/docs/response.md) - the response, as JSON, of the executed graphql query\n- [response_data](/docs/response_data.md) - digs through the graphql response to return data from the specified node(s)\n\nAPI / Development\n\n- [.add_matcher](/docs/add_matcher.md) - add a custom RSpec matcher to the GraphQLResponse matchers\n- [.add_validator](/docs/add_validator.md) - add a custom validator to be used by the custom matchers\n- [.add_helper](/docs/add_helper.md) - add helper methods to your specs, made avialable in `it` or `describe` / `context` blocks\n\n## Getting Started\n\nThere are only a couple of bits you need to get started:\n\n- configuration of a GraphQL Schema in [`RSpec::GraphQLResponse.configure`](/docs/configuration.md)\n- the inclusion of [`type: :graphql`](/docs/graphql_spec_type.md) in your `RSpec.describe` call\n\n```ruby\nRSpec::GraphQLResponse.configure do |config|\n  config.graphql_schema = MyGraphQLSchema\nend\n\nRSpec.describe My::Cool::Thing, type: :graphql do\n  # ...\nend\n```\n\nBeyond these two basic needs, understanding the reason for this gem's existence can be useful in figuring out what the gem\ndoes, and what methods and options are available.\n\n## How We Got Here\n\nExecuting a GraphQL call from RSpec is not the most challenging code to write:\n\n```ruby\nlet(:query) do\n  \u003c\u003c-GQL\n    query ListCharacters{\n      characters {\n        id\n        name\n      }\n    }\n  GQL\nend\n\nsubject do\n  MySchema.execute(query)\nend\n\nit \"does something\" do\n  response = subject.to_h\n\n  # expect(response) ...\nend\n```\n\nBut copy \u0026 paste is often considered a design error, and this code is likely going to be littered throughout your spec files.\n\n#### Use the Built-In `execute_graphql`\n\nTo help reduce the copy \u0026 paste, `RSpec::GraphQLResponse` has a built-in `execute_graphql` method that looks for a `query` variable\nin your specs.\n\n```ruby\nRSpec.describe Some::Thing, type: :graphql do\n  graphql_operation \u003c\u003c-GQL\n    query ListCharacters{\n      characters {\n        id\n        name\n      }\n    }\n  GQL\n\n  it \"executes the query\" do\n    response = execute_graphql.to_h\n\n    # expect(response) ...\n  end\nend\n```\n\n#### Use the Built-In `response`\n\nThe reduction in code is good, but the copy \u0026 paste of `response = execute_graphql.to_h` will quickly become an issue in the same\nway. The reduce this, `RSpec::GraphQLResponse` provides a built-in `response` helper.\n\n```ruby\nRSpec.describe Some::Thing, type: :graphql do\n  graphql_operation \u003c\u003c-GQL\n    query ListCharacters{\n      characters {\n        id\n        name\n      }\n    }\n  GQL\n\n  it \"executes the query\" do\n    expect(response).to include(\n      \"data\" =\u003e {\n        \"characters\" =\u003e { ... }\n      }\n    )\n  end\nend\n```\n\n#### Retrieve response results with `response_data`\n\nNow that the GraphQL query has been executed and a response has been obtained, it's time to check for the results of a GraphQL\noperation. In the previous example, the spec is expecting to find `data` with `characters` in the response hash. To reduce the\nnested hash checking, use the built-in `response_data` method to retrieve the `characters`:\n\n```ruby\nRSpec.describe Some::Thing, type: :graphql do\n  graphql_operation \u003c\u003c-GQL\n    query ListCharacters{\n      characters {\n        id\n        name\n      }\n    }\n  GQL\n\n  it \"executes the query\" do\n    expect(response_data :characters).to include(\n      # ...\n    )\n  end\nend\n```\n\nNote the lack of `response` use here. Internally, the `response_data` method uses the `response` to obtain the data requested. This\nmeans the entire chain of operations from executing the GraphQL request, to converting the response into a hash, and digging\nthrough the results to find the correction operation, has been handled behind the scenes. To see more examples of how to use\n`response_data` dig through your response check out it's full documenation [here.](/docs/response_data.md)\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/testdouble/rspec-graphql_response. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Rspec::GraphQLResponse project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/testdouble/rspec-graphql_response/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestdouble%2Frspec-graphql_response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftestdouble%2Frspec-graphql_response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftestdouble%2Frspec-graphql_response/lists"}