-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathusage.rb
100 lines (85 loc) · 1.65 KB
/
usage.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
require_relative './lib/gqli'
puts GQLi::DSL.query {
__on('Foo') {
bar
}
}.to_gql
puts GQLi::DSL.query {
viewer {
login
repositories(first: 10) {
edges {
node {
nameWithOwner
watchers(first: 10) {
edges {
node {
login
}
}
}
}
}
}
}
}.to_gql
FragmentFoo = GQLi::DSL.fragment('Foo', 'Bar') {
foo
bar(b: 10)
baz {
qux(c: 3)
}
}
puts FragmentFoo.to_gql
puts GQLi::DSL.query {
___ FragmentFoo
}.to_gql
puts "-------------"
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 = GQLi::DSL.fragment('CatBase', 'Cat') {
name
likes
lives
}
CatBestFriend = GQLi::DSL.fragment('CatBestFriend', 'Cat') {
bestFriend {
__on('Cat') {
___ CatBase
}
}
}
CatImportantFields = GQLi::DSL.fragment('CatImportantFields', 'Cat') {
___ CatBase
___ CatBestFriend
}
Query = GQLi::DSL.query {
catCollection(limit: 1) {
items {
___ CatImportantFields
image {
url
}
}
}
}
response = CONTENTFUL_GQL.execute(Query)
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}"
c.bestFriend.tap do |bf|
puts "Best Friend:"
puts "\tName: #{bf.name}"
puts "\tLikes: #{bf.likes.join(", ")}"
puts "\tLives #: #{bf.lives}"
end
end