|
4 | 4 | RSpec.describe GraphQL::Preload do |
5 | 5 | subject do |
6 | 6 | @result ||= |
7 | | - PreloadSchema.execute(query: query_string, context: {}, variables: {}) |
| 7 | + schema.execute(query: query_string, context: {}, variables: {}) |
8 | 8 | end |
9 | 9 |
|
10 | | - context "without associations" do |
11 | | - let(:query_string) { "query { products { title } }" } |
| 10 | + shared_examples "test suite" do |
| 11 | + context "without associations" do |
| 12 | + let(:query_string) { "query { products { title } }" } |
12 | 13 |
|
13 | | - it "doesn't load associations at all" do |
14 | | - expect { subject }.not_to exceed_query_limit 1 |
| 14 | + it "doesn't load associations at all" do |
| 15 | + expect { subject }.not_to exceed_query_limit 1 |
| 16 | + end |
15 | 17 | end |
16 | | - end |
17 | 18 |
|
18 | | - context "with associations" do |
19 | | - let(:query_string) do |
20 | | - <<~QRAPHQL |
| 19 | + context "with associations" do |
| 20 | + let(:query_string) do |
| 21 | + <<~QRAPHQL |
21 | 22 | query { posts { title comments { text } } } |
22 | | - QRAPHQL |
23 | | - end |
| 23 | + QRAPHQL |
| 24 | + end |
24 | 25 |
|
25 | | - it "preloads associations by single query" do |
26 | | - expect { subject }.to exceed_query_limit 1 # to ensure that GraphQL query at least works |
27 | | - expect { subject }.not_to exceed_query_limit 2 |
28 | | - posts = subject.dig("data", "posts") |
29 | | - expect(posts.size).to eq(4) |
30 | | - expect(posts.flat_map { |p| p["comments"] }.size).to eq(8) |
| 26 | + it "preloads associations by single query" do |
| 27 | + expect { subject }.to exceed_query_limit 1 # to ensure that GraphQL query at least works |
| 28 | + expect { subject }.not_to exceed_query_limit 2 |
| 29 | + posts = subject.dig("data", "posts") |
| 30 | + expect(posts.size).to eq(4) |
| 31 | + expect(posts.flat_map { |p| p["comments"] }.size).to eq(8) |
| 32 | + end |
31 | 33 | end |
32 | | - end |
33 | 34 |
|
34 | | - context "with associations with custom scopes" do |
35 | | - let(:query_string) do |
36 | | - <<~QRAPHQL |
| 35 | + context "with associations with custom scopes" do |
| 36 | + let(:query_string) do |
| 37 | + <<~QRAPHQL |
37 | 38 | query { users { name posts { title } } } |
38 | | - QRAPHQL |
39 | | - end |
| 39 | + QRAPHQL |
| 40 | + end |
40 | 41 |
|
41 | | - it "preloads associations by single query and given order" do |
42 | | - expect { subject }.not_to exceed_query_limit 2 |
43 | | - posts = subject.dig("data", "users").flat_map { |p| p["posts"] } |
44 | | - # Posts for every user should be from greater rating to lower |
45 | | - expect(posts.map { |p| p["title"] }).to eq(%w[Bar Foo Baz Huh]) |
| 42 | + it "preloads associations by single query and given order" do |
| 43 | + expect { subject }.not_to exceed_query_limit 2 |
| 44 | + posts = subject.dig("data", "users").flat_map { |p| p["posts"] } |
| 45 | + # Posts for every user should be from greater rating to lower |
| 46 | + expect(posts.map { |p| p["title"] }).to eq(%w[Bar Foo Baz Huh]) |
| 47 | + end |
46 | 48 | end |
47 | 49 | end |
| 50 | + |
| 51 | + context "modern class-based GraphQL schema" do |
| 52 | + let(:schema) { PreloadSchema } |
| 53 | + |
| 54 | + include_examples "test suite" |
| 55 | + end |
| 56 | + |
| 57 | + context "legacy define-based GraphQL schema" do |
| 58 | + next if TESTING_GRAPHQL_RUBY_INTERPRETER # No interpreter runtime these days |
| 59 | + |
| 60 | + let(:schema) { Legacy::PreloadSchema } |
| 61 | + |
| 62 | + include_examples "test suite" |
| 63 | + end |
48 | 64 | end |
0 commit comments