-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.rb
50 lines (40 loc) · 1.08 KB
/
index.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
require 'pp'
require 'active_record'
require 'kaminari'
require 'graphiti'
require './seeds'
class ApplicationResource < Graphiti::Resource
self.abstract_class = true
self.adapter = Graphiti::Adapters::ActiveRecord
self.autolink = false
end
class EmployeeResource < ApplicationResource
attribute :first_name, :string
attribute :last_name, :string
attribute :age, :integer
has_many :positions
end
class PositionResource < ApplicationResource
attribute :employee_id, :integer, only: [:filterable]
attribute :department_id, :integer, only: [:filterable]
attribute :title, :string
belongs_to :department
end
class DepartmentResource < ApplicationResource
attribute :name, :string
end
Graphiti.setup!
employees = EmployeeResource.all({
sort: '-id',
filter: { age: { gt: 30 } },
page: { size: 10, number: 1 },
include: 'positions.department'
})
employees.each do |e|
puts "#{e.first_name} | #{e.positions[0].title} | #{e.positions[0].department.name}"
end
pp JSON.parse(employees.to_jsonapi)
puts "\n\n"
pp JSON.parse(employees.to_json)
puts "\n\n"
puts employees.to_xml