-
Notifications
You must be signed in to change notification settings - Fork 18
/
usage_with_inlined_dsl.rb
69 lines (60 loc) · 1.44 KB
/
usage_with_inlined_dsl.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
require_relative './lib/gqli'
class ContentfulClient
extend GQLi::DSL # Makes DSL available at a class level
include GQLi::DSL # Makes DSL available at an object level
SPACE_ID = 'cfexampleapi'
ACCESS_TOKEN = 'b4c0n73n7fu1'
CONTENTFUL_GQL = GQLi::Client.new(
"https://graphql.contentful.com/content/v1/spaces/#{SPACE_ID}",
headers: { "Authorization" => "Bearer #{ACCESS_TOKEN}" }
)
CatBase = fragment('CatBase', 'Cat') {
name
likes
lives
}
CatBestFriend = fragment('CatBestFriend', 'Cat') {
bestFriend {
__on('Cat') {
___ CatBase
}
}
}
CatImportantFields = fragment('CatImportantFields', 'Cat') {
___ CatBase
___ CatBestFriend
}
def cats
CONTENTFUL_GQL.execute(
query {
catCollection(limit: 10, locale: 'tlh') {
items {
___ CatImportantFields
image {
url
}
}
}
}
)
end
end
response = ContentfulClient.new.cats
puts "Query sent:"
puts response.query.to_gql
puts
puts "Response received"
response.data.catCollection.items.each do |c|
puts "Name: #{c.name}"
puts "Likes: #{c.likes.join(", ")}"
puts "Lives #: #{c.lives}"
puts "Image: #{c.image.url}" if c.image?
c.bestFriend.tap do |bf|
next if bf.nil?
puts "Best Friend:"
puts "\tName: #{bf.name}"
puts "\tLikes: #{bf.likes.join(", ")}"
puts "\tLives #: #{bf.lives}"
end
puts
end