forked from hschne/graphql-groups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_schema.rb
78 lines (57 loc) · 1.48 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
# frozen_string_literal: true
require 'graphql'
require 'graphql/groups'
require_relative 'db'
require_relative 'models'
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
def age(scope:)
scope.group("(cast(age/10 as int) * 10) || '-' || ((cast(age/10 as int) + 1) * 10)")
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:)
case interval
when 'month'
scope.group("strftime('%Y-%m-01 00:00:00 UTC', published_at)")
when 'year'
scope.group("strftime('%Y-01-01 00:00:00 UTC', published_at)")
else
scope.group("strftime('%Y-%m-%d 00:00:00 UTC', published_at)")
end
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