{"id":14991372,"url":"https://github.com/peterfication/rspec-graphql-integration","last_synced_at":"2025-04-12T03:42:03.898Z","repository":{"id":65147035,"uuid":"583875544","full_name":"peterfication/rspec-graphql-integration","owner":"peterfication","description":"This RSpec plugin simplifies integration tests for GraphQL.","archived":false,"fork":false,"pushed_at":"2025-04-07T22:30:41.000Z","size":267,"stargazers_count":16,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T19:07:47.730Z","etag":null,"topics":["graphql","rspec","ruby"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/rspec-graphql-integration","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/peterfication.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-31T09:09:21.000Z","updated_at":"2025-04-09T10:23:34.000Z","dependencies_parsed_at":"2023-02-10T06:15:57.952Z","dependency_job_id":"eab4f149-a8c4-45a7-b02b-db89901f1466","html_url":"https://github.com/peterfication/rspec-graphql-integration","commit_stats":{"total_commits":138,"total_committers":2,"mean_commits":69.0,"dds":0.4057971014492754,"last_synced_commit":"0446a7bfbe01f7b9a8c775d874b07a356d97d416"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterfication%2Frspec-graphql-integration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterfication%2Frspec-graphql-integration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterfication%2Frspec-graphql-integration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peterfication%2Frspec-graphql-integration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peterfication","download_url":"https://codeload.github.com/peterfication/rspec-graphql-integration/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281415,"owners_count":21077423,"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":["graphql","rspec","ruby"],"created_at":"2024-09-24T14:27:34.251Z","updated_at":"2025-04-12T03:42:03.876Z","avatar_url":"https://github.com/peterfication.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"logo.png\" width=\"200\" \\\u003e\n\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003eRSpec GraphQL Integration Testing\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/peterfication/rspec-graphql-integration/actions?query=branch%3Amain+\"\u003e\n    \u003cimg alt=\"CI\" src=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/ci.yml/badge.svg\" \\\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/peterfication/rspec-graphql-integration\"\u003e\n    \u003cimg alt=\"CodeCov\" src=\"https://codecov.io/gh/peterfication/rspec-graphql-integration/branch/main/graph/badge.svg?token=V5HKH4C2BA\" \\\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://sonarcloud.io/summary/new_code?id=peterfication_rspec-graphql-integration\"\u003e\n    \u003cimg alt=\"Maintainability Rating\" src=\"https://sonarcloud.io/api/project_badges/measure?project=peterfication_rspec-graphql-integration\u0026metric=sqale_rating\" \\\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codeatlas.dev/github/peterfication/rspec-graphql-integration/main\"\u003e\n    \u003cimg alt=\"Codeatlas Codebase visualized\" src=\"https://img.shields.io/badge/Codeatlas-Codebase_visualized-323b4f?link=https://codeatlas.dev/github/peterfication/rspec-graphql-integration/main\" \\\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://rubygems.org/gems/rspec-graphql-integration\"\u003e\n    \u003cimg src=\"https://badge.fury.io/rb/rspec-graphql-integration.svg\" alt=\"Gem Version\" height=\"18\"\u003e\n  \u003c/a\u003e\n\n  \u003cbr/\u003e\n\n  \u003ca href=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/ruby-nightly.yml\"\u003e\n    \u003cimg alt=\"CI\" src=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/ruby-nightly.yml/badge.svg\" \\\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/truffleruby-nightly.yml\"\u003e\n    \u003cimg alt=\"CI\" src=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/truffleruby-nightly.yml/badge.svg\" \\\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/jruby-nightly.yml\"\u003e\n    \u003cimg alt=\"CI\" src=\"https://github.com/peterfication/rspec-graphql-integration/actions/workflows/jruby-nightly.yml/badge.svg\" \\\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  This \u003ca href=\"https://rspec.info/\"\u003eRSpec\u003c/a\u003e plugin simplifies integration tests for \u003ca href=\"https://graphql-ruby.org/\"\u003eGraphQL\u003c/a\u003e.\n\u003c/p\u003e\n\n## Introduction\n\nThis plugin mainly consists of a matcher called `match_graphql_response` that executes a query or mutation against the defined schema and checks the response against a JSON response.\n\n### Idea behind this plugin\n\n#### The problem\n\nWhen writing GraphQL integration tests, you write your query in a multiline string, get the response, parse it (probably with a helper) and write some expectations, maybe even expecting a whole multi-dimensional `Hash`. This could then look something like this:\n\n```ruby\nRSpec.describe \"Query.currentUser\" do\n  subject(:query_result) { MySchema.execute(query, context: context).as_json }\n\n  let(:user) { create(:user) }\n  let(:context) { { current_user: user } }\n  let(:query) { \u003c\u003c~GRAPHQL }\n      query {\n        currentUser {\n          id\n          email\n        }\n      }\n    GRAPHQL\n  let(:expected_result) do\n    { \"data\" =\u003e { \"currentUser\" =\u003e { \"id\" =\u003e user.id.to_s, \"email\" =\u003e user.email } } }.as_json\n  end\n\n  it \"returns the current user\" do\n    expect(query_result).to eq(expected_result)\n  end\nend\n```\n\nFor small queries, this is fine. But for big queries (and hence, big responses) this gets unhandy very fast. This is subjective of course ;)\n\nAnother issue is that we can't leverage the GraphQL language server while writing/maintaining these integration tests.\n\n#### The solution provided by this gem\n\nThis gem tries to improve this situation by moving the query and the response in their own files with a proper file type. This way, the integration test files are smaller and can focus on mocking data/instances. Also, the GraphQL language server will give you autocompletion/linting in your GraphQL files (if you've set up your editor for it).\n\nThe simple integration test from above then looks like this:\n\n_`current_user_spec.rb`_\n\n```ruby\nRSpec.describe \"Query.currentUser\" do\n  let(:user) { create(:user) }\n  let(:context) { { current_user: user } }\n  let(:response_variables) { { user_id: user.id, user_email: user.email } }\n\n  it { is_expected.to match_graphql_response }\nend\n```\n\n_`current_user.graphql`_\n\n```graphql\nquery {\n  currentUser {\n    id\n    email\n  }\n}\n```\n\n_`current_user.json`_\n\n```json\n{\n  \"data\": {\n    \"currentUser\": {\n      \"id\": \"{{user_id}}\",\n      \"email\": \"{{user_email}}\"\n    }\n  }\n}\n```\n\n\u003e TODO: Think about using a templating language for the response\n\n## Configuration\n\nYou can have a look at the [RSpec GraphQL integration configuration](spec/support/graphql_integration.rb) of this gem to get started.\n\n### Schema class\n\nYou need to define the schema class that is used to execute the queries against. You can also define a main schema, but overwrite this for specific tests with `let(:schema_class_overwrite) { MyOtherSchema }`.\n\n```ruby\nrequire \"rspec/graphql_integration\"\n\nRSpec.configure do |config|\n  # ...\n  config.graphql_schema_class = MySchema\n  # ...\nend\n```\n\n### Files in folder\n\nThe default behavior for the request and response files is that they are named like the test file but instead of `_spec.rb` they end with `.graphql` and `.json`. If you want the files to be in a folder named like the test file, activate this option. Then the matcher will look for `\u003ctest_file_without_spec.rb\u003e/(request.graphql|response.json)` instead of `\u003ctest_file_without_spec.rb\u003e.(graphql|json)`.\n\n```ruby\nrequire \"rspec/graphql_integration\"\n\nRSpec.configure do |config|\n  # ...\n  config.graphql_put_files_in_folder = true\n  # ...\nend\n```\n\n### Spec type from file location\n\nLike with the [`RSpec::Rails`](https://github.com/rspec/rspec-rails), this gem can infer the spec type (`graphql`) from the spec file location. If you want this to happen, you have to enable this explicitly:\n\n```ruby\nrequire \"rspec/graphql_integration\"\n\nRSpec.configure do |config|\n  # ...\n  config.infer_graphql_spec_type_from_file_location!\n  # ...\nend\n```\n\n## Special variables\n\nA lot of things can be adjusted per test by setting special variables. You need to set them with `let`:\n\n```ruby\nlet(:context) { { current_user: user } }\n```\n\nHere is an overview of what can be set:\n\n| Variable                  | Description                                                                                                                                 | Example test                                                                 |\n| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |\n| `test_file_overwrite`     | Overwrites the location of the test file, if the automatic test file discovery is not working. This should normally not be necessary.       | [link](spec/graphql/queries/test_file_overwrite/test_file_overwrite_spec.rb) |\n| `schema_class_overwrite`  | Overwrites the schema class that is used for the test. This is only necessary if you have more than one GraphQL schema in your codebase.    | [link](spec/graphql/queries/schema_spec.rb)                                  |\n| `request_file_overwrite`  | Overwrites the filename of the request file, in case you want to reuse request files or put them inside a folder.                           | [link](spec/graphql/queries/request_file_overwrite_spec.rb)                  |\n| `response_file_overwrite` | Overwrites the filename of the response file, in case you want to reuse response files or put them inside a folder.                         | [link](spec/graphql/queries/response_file_overwrite_spec.rb)                 |\n| `context`                 | The GraphQL context that is passed to the GraphQL schema.                                                                                   | [link](spec/graphql/queries/context_spec.rb)                                 |\n| `request_variables`       | Sets the variables that are passed into the GraphQL schema execution. This is necessary if you use variables inside your queries/mutations. | [link](spec/graphql/queries/request_variables_spec.rb)                       |\n| `response_variables`      | Sets the variables that are used by the simple response template engine.                                                                    | [link](spec/graphql/queries/response_variables_spec.rb)                      |\n\n## Development\n\nUseful commands are defined in the [`Justfile`](Justfile) and can be listed with [`just`](https://github.com/casey/just).\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeterfication%2Frspec-graphql-integration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeterfication%2Frspec-graphql-integration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeterfication%2Frspec-graphql-integration/lists"}