graphql-client 0.0.22 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +118 -10
  3. data/lib/graphql/client.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35d033dd34fc07b0321711ab80afe22a67d6bc24
4
- data.tar.gz: fdc1351584a59b2fd907cb9231d3385f65c9ff7c
3
+ metadata.gz: 5601d2f7aa039d200df20fa117df18f06a5473f1
4
+ data.tar.gz: 73b673e1a522ade77581115a22e2e3c5d5cfdd9e
5
5
  SHA512:
6
- metadata.gz: d7f52e410a8929897c9b22f14b19a1bce944a0133d41eb1fc1f6947124c7e9770415115fd9563af11b3b6cb80e4f33fe6a87e104a41dc4bcfc15e65448186f42
7
- data.tar.gz: 535bc0149c5447b2004428bf92cc9e4806a5e84aa044e23e62c232fc9e0b3e5e0b66d8dba2b2a9eab8ed76e00925e7fca99ca4b1aed4057313a16a038806ea72
6
+ metadata.gz: af9d1074b12a2d82b45d4f90472a7d7f97061eda325787e4a43c726bda1041254e3e4db9fd91f0a25b4677c03851c4bbf9d660cff3aa11ffa1964951deb9552e
7
+ data.tar.gz: 5720e2af73259abec92963c3746c787821f547813282cf4dcac9ba32ee224b395684048690edb203c80cbb6513c045f47733588b0db15271a4f6a096ce441eed
data/README.md CHANGED
@@ -1,25 +1,29 @@
1
1
  # graphql-client
2
2
 
3
- ## Usage
3
+ GraphQL Client is a Ruby library for declaring, composing and executing GraphQL queries.
4
4
 
5
- To work with the client, you'll need to pass two variables to the initializer:
5
+ ## Usage
6
6
 
7
- * You'll need to have access to a GraphQL schema. This can either be a JSON blob or simply a JSON file. Usually, you can generate this by executing an introspection query to a GraphQL server.
8
- * You can optionally define a method that executes your schema. Usually, this is going to be some kind of HTTP adapter.
7
+ ### Configuration
9
8
 
10
- Once you've got that, you can set up the client like this:
9
+ Sample configuration for a GraphQL Client to query from the [SWAPI GraphQL Wrapper](https://github.com/graphql/swapi-graphql).
11
10
 
12
11
  ``` ruby
13
12
  require "graphql/client"
13
+ require "graphql/client/http"
14
14
 
15
+ # Star Wars API example wrapper
15
16
  module SWAPI
17
+ # Configure GraphQL endpoint using the basic HTTP network adapter.
16
18
  HTTP = GraphQL::Client::HTTP.new("http://graphql-swapi.parseapp.com/")
17
19
 
18
- # Fetch latest schema on boot,
20
+ # Fetch latest schema on init, this will make a network request
19
21
  Schema = GraphQL::Client.load_schema(HTTP)
22
+
20
23
  # However, its smart to dump this to a JSON file and load from disk
21
24
  #
22
- # GraphQL::Client.dump_schema(HTTP, "path/to/schema.json")
25
+ # Run in from a script or rake task
26
+ # GraphQL::Client.dump_schema(SWAPI::HTTP, "path/to/schema.json")
23
27
  #
24
28
  # Schema = GraphQL::Client.load_schema("path/to/schema.json")
25
29
 
@@ -27,9 +31,111 @@ module SWAPI
27
31
  end
28
32
  ```
29
33
 
30
- Then, all you'll need to do is pass your GraphQL query to `SWAPI::Client.query` to fetch the response.
34
+ ### Defining Queries
35
+
36
+ If you haven't already, [familiarize yourself with the GraphQL query syntax](http://graphql.org/docs/queries/). Queries are declared with the same syntax inside of a `<<-'GRAPHQL'` heredoc. There isn't any special query builder Ruby DSL.
37
+
38
+ This client library encourages all GraphQL queries to be declared statically and assigned to a Ruby constant.
39
+
40
+ ``` ruby
41
+ HeroNameQuery = SWAPI::Client.parse <<-'GRAPHQL'
42
+ query {
43
+ hero {
44
+ name
45
+ }
46
+ }
47
+ GRAPHQL
48
+ ```
49
+
50
+ Fragments are declared similarly.
51
+
52
+ ``` ruby
53
+ HumanFragment = SWAPI::Client.parse <<-'GRAPHQL'
54
+ fragment on Human {
55
+ name
56
+ homePlanet
57
+ }
58
+ GRAPHQL
59
+ ```
60
+
61
+ To include a fragment in a query, reference the fragment by constant.
62
+
63
+ ``` ruby
64
+ HeroNameQuery = SWAPI::Client.parse <<-'GRAPHQL'
65
+ {
66
+ luke: human(id: "1000") {
67
+ ...HumanFragment
68
+ }
69
+ leia: human(id: "1003") {
70
+ ...HumanFragment
71
+ }
72
+ }
73
+ GRAPHQL
74
+ ```
75
+
76
+ This works for namespaced constants.
77
+
78
+ ``` ruby
79
+ module Hero
80
+ Query = SWAPI::Client.parse <<-'GRAPHQL'
81
+ {
82
+ luke: human(id: "1000") {
83
+ ...Human::Fragment
84
+ }
85
+ leia: human(id: "1003") {
86
+ ...Human::Fragment
87
+ }
88
+ }
89
+ GRAPHQL
90
+ end
91
+ ```
92
+
93
+ `::` is invalid in regular GraphQL syntax, but `#parse` makes an initial pass on the query string and resolves all the fragment spreads with [`constantize`](http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize).
94
+
95
+ ### Executing queries
96
+
97
+ Pass the reference of a parsed query definition to `GraphQL::Client#query`. Data is returned back in a wrapped `GraphQL::QueryResult` struct that provides Ruby-ish accessors.
98
+
99
+ ``` ruby
100
+ result = SWAPI::Client.query(Hero::Query)
101
+
102
+ # The raw data is Hash of JSON values
103
+ # result["data"]["luke"]["homePlanet"]
104
+
105
+ # The wrapped result allows to you access data with Ruby methods
106
+ result.data.luke.home_planet
107
+ ```
108
+
109
+ ### Rails ERB integration
110
+
111
+ If you're using Ruby on Rails ERB templates, theres a ERB extension that allows static queries to be defined in the template itself.
112
+
113
+ In standard Ruby you can simply assign queries and fragments to constants and they'll be available throughout the app. However, the contents of an ERB template is compiled into a Ruby method, and methods can't assign constants. So a new ERB tag was extended to declare static sections that include a GraphQL query.
114
+
115
+ ``` erb
116
+ <%# app/views/humans/human.html.erb %>
117
+ <%graphql
118
+ fragment HumanFragment on Human {
119
+ name
120
+ homePlanet
121
+ }
122
+ %>
123
+
124
+ <p><%= human.name %> lives on <%= human.home_planet %>.</p>
125
+ ```
126
+
127
+ These `<%graphql` sections are simply ignored at runtime but make their definitions available through constants. The module namespacing is derived from the `.erb`'s path plus the definition name.
128
+
129
+ ```
130
+ >> "views/humans/human".camelize
131
+ => "Views::Humans::Human"
132
+ >> Views::Humans::Human::HumanFragment
133
+ => #<GraphQL::Client::FragmentDefinition>
134
+ ```
135
+
136
+ ## Examples
31
137
 
32
- You can also call `SWAPI::Client.parse` on a query to generate a validation against the GraphQL query.
138
+ [github/github-graphql-rails-example](https://github.com/github/github-graphql-rails-example) is an example application using this library to implement views on the GitHub GraphQL API.
33
139
 
34
140
  ## Installation
35
141
 
@@ -41,4 +147,6 @@ gem 'graphql-client'
41
147
 
42
148
  ## See Also
43
149
 
44
- * [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) gem which supports 90% of the features this library provides. ❤️ @rmosolgo
150
+ * [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) gem which implements 80% of what this library provides. ❤️ [@rmosolgo](https://github.com/rmosolgo)
151
+ * [Facebook's GraphQL homepage](http://graphql.org/)
152
+ * [Facebook's Relay homepage](https://facebook.github.io/relay/)
@@ -54,7 +54,7 @@ module GraphQL
54
54
  )
55
55
 
56
56
  if io
57
- io = IO.new(io, "w") if io.is_a?(String)
57
+ io = File.open(io, "w") if io.is_a?(String)
58
58
  io.write(JSON.pretty_generate(result))
59
59
  io.close_write
60
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub
@@ -106,7 +106,7 @@ dependencies:
106
106
  - - "~>"
107
107
  - !ruby/object:Gem::Version
108
108
  version: '0.42'
109
- description: "???"
109
+ description: A Ruby library for declaring, composing and executing GraphQL queries
110
110
  email: engineering@github.com
111
111
  executables: []
112
112
  extensions: []