File: graphql_schema.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 (40 lines) | stat: -rw-r--r-- 946 bytes parent folder | download
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
class UserType < GraphQL::Schema::Object
  field :id, ID, null: false
end

class PostType < GraphQL::Schema::Object
  field :user, UserType, null: false
  field :user_old, UserType, null: false

  def user
    BatchLoader::GraphQL.for(object.user_id).batch(default_value: nil) do |user_ids, loader|
      User.where(id: user_ids).each { |user| loader.call(user.id, user) }
    end
  end

  def user_old
    BatchLoader.for(object.user_id).batch(default_value: nil) do |user_ids, loader|
      User.where(id: user_ids).each { |user| loader.call(user.id, user) }
    end
  end
end

class QueryType < GraphQL::Schema::Object
  field :posts, [PostType], null: false

  def posts
    Post.all
  end
end

class GraphqlSchema < GraphQL::Schema
  query QueryType
  use BatchLoader::GraphQL
end

if defined?(GraphQL::Execution::Interpreter)
  class GraphqlSchemaWithInterpreter < GraphQL::Schema
    query QueryType
    use BatchLoader::GraphQL
  end
end