This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.rb
65 lines (51 loc) · 1.49 KB
/
graphql.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
require 'graphql'
class ChildType < GraphQL::Schema::Object
field :id, ID, null: false
field :first_name, String, null: true
field :last_name, String, null: true
end
class DirectionEnum < GraphQL::Schema::Enum
value 'asc', value: 'ASC'
value 'desc', value: 'DESC'
end
class ChildOrder < GraphQL::Schema::InputObject
graphql_name 'ChildOrderTuple'
argument :direction, DirectionEnum, required: true
argument :key, 'ChildOrder::Values', required: true
class Values < GraphQL::Schema::Enum
graphql_name 'ChildOrder'
value 'firstName', value: 'first_name'
value 'lastName', value: 'last_name'
end
end
class ChildResolver < GraphQL::Schema::Resolver
type [ChildType], null: true
argument :order, ChildOrder, required: false
def resolve(order: nil)
Child.all
.then { |query| use_order(query, order) }
end
private
def use_order(query, order)
return query.order(id: :asc) unless order
case order.key
when 'first_name'
query
.reorder(first_name: order.direction.to_sym, last_name: order.direction.to_sym)
.order(id: order.direction.to_sym)
when 'last_name'
query
.reorder(last_name: order.direction.to_sym, first_name: order.direction.to_sym)
.order(id: order.direction.to_sym)
else
query
.order(id: order.direction.to_sym)
end
end
end
class QueryType < GraphQL::Schema::Object
field :children, resolver: ChildResolver
end
class MeetupSchema < GraphQL::Schema
query(QueryType)
end