-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_schema.rb
87 lines (63 loc) · 1.6 KB
/
test_schema.rb
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
require 'graphql'
require 'graphql/groups'
require 'groupdate'
require_relative 'db'
require_relative 'models'
class BaseField < GraphQL::Schema::Field; end
class AuthorGroupResultType < GraphQL::Groups::Schema::GroupResultType
aggregate :average do
attribute :age
end
def average(scope:, attribute:)
scope.average(attribute)
end
end
class AuthorGroupType < GraphQL::Groups::Schema::GroupType
scope { Author.all }
result_type { AuthorGroupResultType }
by :name
by :age
by :nationality do
argument :upcase, String, required: false
end
def age(scope:)
scope.group("(cast(age/10 as int) * 10) || '-' || ((cast(age/10 as int) + 1) * 10)")
end
def nationality(scope:, upcase: false)
if upcase
scope.group('UPPER(nationality)')
else
scope.group(:nationality)
end
end
end
class BookGroupType < GraphQL::Groups::Schema::GroupType
scope { object }
by :name
by :published_at do
argument :interval, String, required: false
end
def published_at(scope:, interval: nil)
interval ||= context[:default_interval]
scope.group_by_period(interval.to_sym, :published_at)
end
end
class StatisticsType < GraphQL::Schema::Object
include GraphQL::Groups
group :books, BookGroupType
end
class QueryType < GraphQL::Schema::Object
include GraphQL::Groups
group :author_groups, AuthorGroupType
field :statistics, StatisticsType, null: false
def statistics
Book.all
end
end
class GroupsSchema < GraphQL::Schema
query QueryType
def self.resolve_type(_type, obj, _ctx)
"#{obj.class.name}Type"
end
end