-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
song.rb
executable file
·156 lines (130 loc) · 4.01 KB
/
song.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'date'
require 'agoo'
Agoo::Log.configure(dir: '',
console: true,
classic: true,
colorize: true,
states: {
INFO: true,
DEBUG: true,
connect: true,
request: true,
response: true,
eval: true,
push: false,
})
# Next implement the Ruby classes to support the API. The type and class names
# are the same in this example to make it easier to follow.
class Artist
attr_reader :name
attr_reader :songs
attr_reader :origin
def initialize(name, origin)
@name = name
@songs = []
@origin = origin
end
def song(args, req)
n = args['name']
@songs.each { |s| return s if n == s.name }
nil
end
end
class Song
attr_reader :name # string
attr_reader :artist # reference
attr_reader :duration # integer
attr_reader :release # date
attr_accessor :likes # integer
def initialize(name, artist, duration, release)
@name = name
@artist = artist
@duration = duration
@release = release
@likes = 0
artist.songs << self
end
end
# This is the class that implements the root query operation.
class Query
attr_reader :artists
def initialize(artists)
@artists = artists
end
def artist(args={})
n = args['name']
@artists.each { |a| return a if n == a.name }
nil
end
end
class Mutation
def initialize(artists)
@artists = artists
@lock = Mutex.new
end
def like(args={})
an = args['artist']
sn = args['song']
@lock.synchronize {
@artists.each {|a|
if an == a.name
a.songs.each { |s| if s.name == sn; s.likes += 1; return s; end }
end
}
}
nil
end
end
class Schema
#attr_reader :query
attr_reader :mutation
attr_reader :subscription
def initialize(q)
@query = q
@mutation = Mutation.new(@query.artists)
end
def query(_, req)
# req will contain the request parameters which can be used to determine
# if the query should continue. For example the req["QUERY_STRING"] could
# be checked for an occurance of "__" and rejected by raising an
# exception. If the exception responds to :code then that code will be
# used as the HTTP status code.
# puts "*** query #{req}"
@query
end
end
# Populate the library.
fazerdaze = Artist.new('Fazerdaze', ['Morningside', 'Auckland', 'New Zealand'])
Song.new('Jennifer', fazerdaze, 240, Date.new(2017, 5, 5))
Song.new('Lucky Girl', fazerdaze, 170, Date.new(2017, 5, 5))
Song.new('Friends', fazerdaze, 194, Date.new(2017, 5, 5))
Song.new('Reel', fazerdaze, 193, Date.new(2015, 11, 2))
boys = Artist.new('Viagra Boys', ['Stockholm', 'Sweden'])
Song.new('Down In The Basement', boys, 216, Date.new(2018, 9, 28))
Song.new('Frogstrap', boys, 195, Date.new(2018, 9, 28))
Song.new('Worms', boys, 208, Date.new(2018, 9, 28))
Song.new('Amphetanarchy', boys, 346, Date.new(2018, 9, 28))
$schema = Schema.new(Query.new([fazerdaze, boys]))
puts %^\nopen 'localhost:6464/graphql?query={artist(name:"Fazerdaze"){name,songs{name,duration}}}&indent=2' in a browser.\n\n^
#http://localhost:6464/graphql?query={artists{name,origin,songs{name,duration,likes}},__schema{types{name,kind,fields{name}}}}
Agoo::Server.init(6464, 'root', thread_count: 1, graphql: '/graphql')
Agoo::Server.start
Agoo::GraphQL.schema($schema) {Agoo::GraphQL.load_file('song.graphql')}
# Use CORS to allow GraphiQL to connect.
Agoo::GraphQL.build_headers = proc{ |req|
origin = req.headers['HTTP_ORIGIN'] || '*'
{
'Access-Control-Allow-Origin' => origin,
'Access-Control-Allow-Headers' => '*',
'Access-Control-Allow-Credentials' => true,
'Access-Control-Max-Age' => 3600,
}
}
# When starting with a thread_count over 0 just sleep until a ^C is
# signalled. Agoo must be running to load the SDL.
sleep
# To run this example type the following then go to a browser and enter a URL of
# localhost:6464/graphql?query={artist(name:"Fazerdaze"){name}}&indent=2. For
# a more complex query try
# localhost:6464/graphql?query={artist(name:"Fazerdaze"){name,songs{name,duration}}}&indent=2.
# ruby song.rb