Commit e62756a2 authored by Mike Jang's avatar Mike Jang

Merge branch 'update-graphql-request-spec-docs' into 'master'

Improve GraphQL testing guide

See merge request gitlab-org/gitlab!46177
parents 7645d63d f7002fcf
......@@ -1392,6 +1392,62 @@ it 'returns a successful response' do
end
```
### Testing tips and tricks
- Avoid false positives:
Authenticating a user with the `current_user:` argument for `post_graphql`
generates more queries on the first request than on subsequent requests on that
same user. If you are testing for N+1 queries using
[QueryRecorder](query_recorder.md), use a **different** user for each request.
The below example shows how a test for avoiding N+1 queries should look:
```ruby
RSpec.describe 'Query.project(fullPath).pipelines' do
include GraphqlHelpers
let(:project) { create(:project) }
let(:query) do
%(
{
project(fullPath: "#{project.full_path}") {
pipelines {
nodes {
id
}
}
}
}
)
end
it 'avoids N+1 queries' do
first_user = create(:user)
second_user = create(:user)
create(:ci_pipeline, project: project)
control_count = ActiveRecord::QueryRecorder.new do
post_graphql(query, current_user: first_user)
end
create(:ci_pipeline, project: project)
expect do
post_graphql(query, current_user: second_user) # use a different user to avoid a false positive from authentication queries
end.not_to exceed_query_limit(control_count)
end
end
```
- Mimic the folder structure of `app/graphql/types`:
For example, tests for fields on `Types::Ci::PipelineType`
in `app/graphql/types/ci/pipeline_type.rb` should live in
`spec/requests/api/graphql/ci/pipeline_spec.rb` regardless of the query being
used to fetch the pipeline data.
## Notes about Query flow and GraphQL infrastructure
GitLab's GraphQL infrastructure can be found in `lib/gitlab/graphql`.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment