File: graphql_spec.rb

package info (click to toggle)
ruby-batch-loader 2.0.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: ruby: 783; makefile: 6; sh: 4
file content (44 lines) | stat: -rw-r--r-- 961 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require "spec_helper"

RSpec.describe 'GraphQL integration' do
  after do
    User.destroy_all
    Post.destroy_all
  end

  it 'resolves BatchLoader fields lazily' do
    test(GraphqlSchema)
  end

  if defined?(GraphqlSchemaWithInterpreter)
    it 'resolves BatchLoader fields lazily with GraphQL Interpreter' do
      test(GraphqlSchemaWithInterpreter)
    end
  end

  def test(schema)
    user1 = User.save(id: "1")
    user2 = User.save(id: "2")
    Post.save(user_id: user1.id)
    Post.save(user_id: user2.id)
    query = <<~QUERY
      {
        posts {
          user { id }
          userOld { id }
        }
      }
    QUERY

    expect(User).to receive(:where).with(id: ["1", "2"]).twice.and_call_original

    result = schema.execute(query)

    expect(result['data']).to eq({
      'posts' => [
        {'user' => {'id' => "1"}, 'userOld' => {'id' => "1"}},
        {'user' => {'id' => "2"}, 'userOld' => {'id' => "2"}}
      ]
    })
  end
end