graphtown 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c54e2275afc874fe0761dba661b16b746a9c3716f15eb74ee50d986ac442b04b
4
- data.tar.gz: c113a90a48131d55dcf0b4d47ce1f17d6b383c5cbfd1fb765c5171a1ddb01499
3
+ metadata.gz: 7ab78a149acc477d7cf5dc2d10a37e6a9855b7b146465f1e18bd40d4ad5fe9f8
4
+ data.tar.gz: d36029de500ce26ad396e4ed0111d8203e84e9e04eae2595b8624feb0b728eb7
5
5
  SHA512:
6
- metadata.gz: 16912ef133cdd7ae2ae3bfbef980839b6eb8e17a320eb8df113d6aef9bd3d3cbdab1e69bb73ecf4e20add8053a826a623b29cf955a51bab38de8352aa4620e3b
7
- data.tar.gz: a66847460dbd420e44a20f2f4ed98040d4ea72b8b22a86179eb94eff25169f86d0d46463281ee9a22d2730269b48fce64d0d18cf364765636eaff92bfc6c3695
6
+ metadata.gz: 2584c1981f3de679c846164c12df1b9fe82b14c639ed308a01c022b85e9bb33acd8e80374ea7fdd2b619e37d6f122d5a158c94d08b3f8ff24e1550f98d3a2b55
7
+ data.tar.gz: fdb4af743a3cb3eaad5bf71cbbf67cdfc43db1b47e3b33bb554b8095515fb62339f5cd85bd74d6bd58745fcf5833de877c05f606b875eddebe90a7da6ba8fe77
@@ -0,0 +1 @@
1
+ github: jaredcwhite
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # main
2
2
 
3
+ # 1.1.0 / 2021-04-06
4
+
5
+ * Add support for string-based queries in addition to Ruby DSL [[#2](https://github.com/whitefusionhq/graphtown/pull/2)] ([pusewicz](https://github.com/pusewicz))
6
+
3
7
  # 1.0.1 / 2020-08-20
4
8
 
5
9
  * Add better detection of matching/not-matching data roots
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Graphtown
2
2
 
3
- Easily consume GraphQL APIs for your Bridgetown website using a tidy Builder DSL on top of the [Graphlient](http://github.com/ashkan18/graphlient) gem.
3
+ Easily consume GraphQL APIs for your [Bridgetown](https://www.bridgetownrb.com) website using a tidy Builder DSL on top of the [Graphlient](http://github.com/ashkan18/graphlient) gem.
4
4
 
5
5
  ## Installation
6
6
 
@@ -36,19 +36,102 @@ def graphql_endpoint
36
36
  end
37
37
  ```
38
38
 
39
- ---
39
+ ## Usage
40
40
 
41
- # Documentation coming soon…
41
+ You'll start by creating a builder plugin which defines a GraphQL query using the DSL provided by the Graphlient gem. Then, in the `build` method of the plugin, you can execute the query and use that data to add content to your site.
42
42
 
43
- ## Usage
43
+ Here's an example of using the GraphQL API provided by [Strapi](https://strapi.io) (a headless CMS) to turn blog posts authored in the CMS into Bridgetown posts:
44
+
45
+ ```ruby
46
+ # plugins/builders/strapi_posts.rb
47
+
48
+ class StrapiPosts < SiteBuilder
49
+ graphql :posts do
50
+ query {
51
+ posts {
52
+ id
53
+ title
54
+ description
55
+ body
56
+ createdAt
57
+ }
58
+ }
59
+ end
60
+
61
+ def build
62
+ queries.posts.each do |post|
63
+ slug = Bridgetown::Utils.slugify(post.title)
64
+ doc "#{slug}.md" do
65
+ layout "post"
66
+ date post.created_at
67
+ front_matter post.to_h
68
+ content post.body
69
+ end
70
+ end
71
+ end
72
+ end
73
+ ```
44
74
 
45
- The plugin will
75
+ The `queries` object will contain the same graph names as what you define using the `graphql` class method. If the "data root" of the query is the same as the graph name, you don't have to access the root specifically. In other words, you don't have to write `queries.posts.posts.each do |post|`. However, if your data root is different, you'll need to access it specifically (see below where it's written as `queries.github.viewer…`).
46
76
 
47
- ### Optional configuration options
77
+ Here's an example of using an authenticated GitHub API to access a list of repositories owned by the user associated with the API key. It includes configuring the Graphlient client to provide the API key in the request header, as well as utilizing query variables which get resolved at runtime.
48
78
 
49
- The plugin will automatically use any of the following metadata variables if they are present in your site's `_data/site_metadata.yml` file.
79
+ ```ruby
80
+ # plugins/builders/github_graphql.rb
81
+
82
+ class GitHubGraphql < SiteBuilder
83
+ graphql :github do
84
+ query(number_of_repos: :int) do
85
+ viewer do
86
+ repositories(first: :number_of_repos) do
87
+ edges do
88
+ node do
89
+ name
90
+ description
91
+ createdAt
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ def variables_for_github
100
+ {
101
+ # pull this out of the bridgetown.config.yaml, if present:
102
+ number_of_repos: config[:github_repo_limit] || 10
103
+ }
104
+ end
105
+
106
+ def build
107
+ queries.github.viewer.repositories.edges.each do |item|
108
+ repo = item.node
109
+ slug = Bridgetown::Utils.slugify(repo.name)
110
+
111
+ doc "#{slug}.md" do
112
+ layout "repository"
113
+ date repo.created_at
114
+ title repo.name
115
+ content repo.description
116
+ end
117
+ end
118
+ end
119
+
120
+ def graphql_endpoint
121
+ "https://api.github.com/graphql"
122
+ end
123
+
124
+ def configure_graphql_client(client)
125
+ client.options[:headers] = {
126
+ "Authorization" => "bearer #{ENV["GITHUB_API_TOKEN"]}"
127
+ }
128
+ end
129
+ end
130
+ ```
50
131
 
51
-
132
+ Note that these examples show just one GraphQL query defined in the plugin, but you can call the `graphql` class method multiple times with different graph names/queries, and access any or all of them in the `build` method.
133
+
134
+ If you run into any issues or need further assistance using GraphQL in your Bridgetown project, [please reach out to the Bridgetown community](https://www.bridgetownrb.com/docs/community) via chat or other means. If you think you've encountered a bug, please file an issue here in the GitHub repo.
52
135
 
53
136
  ## Testing
54
137
 
@@ -62,4 +145,9 @@ The plugin will automatically use any of the following metadata variables if the
62
145
  3. Create your feature branch (`git checkout -b my-new-feature`)
63
146
  4. Commit your changes (`git commit -am 'Add some feature'`)
64
147
  5. Push to the branch (`git push origin my-new-feature`)
65
- 6. Create a new Pull Request
148
+ 6. Create a new Pull Request
149
+
150
+ ## Releasing
151
+
152
+ To release a new version of the plugin, simply bump up the version number in
153
+ `version.rb` and then run `script/release`.
@@ -12,9 +12,9 @@ module Graphtown
12
12
  class_methods do
13
13
  attr_accessor :graphql_queries
14
14
 
15
- def graphql(graph_name, &block)
15
+ def graphql(graph_name, query = nil, &block)
16
16
  self.graphql_queries ||= Queries.new
17
- graphql_queries.send("#{graph_name}=", block)
17
+ graphql_queries.send("#{graph_name}=", query || block)
18
18
  end
19
19
  end
20
20
 
@@ -22,16 +22,15 @@ module Graphtown
22
22
  return self.class.graphql_queries if @_executed_queries
23
23
 
24
24
  client = graphql_client
25
- self.class.graphql_queries.each_pair do |graph_name, block|
26
- graph_dsl = Graphlient::Query.new(&block)
27
-
25
+ self.class.graphql_queries.each_pair do |graph_name, value|
26
+ query = parse_graphql_query(client, value)
28
27
  query_variables = if respond_to?("variables_for_#{graph_name}")
29
28
  send("variables_for_#{graph_name}")
30
29
  end
31
30
 
32
31
  self.class.graphql_queries.send(
33
32
  "#{graph_name}=",
34
- client.execute(graph_dsl.to_s, query_variables).data.yield_self do |data|
33
+ client.execute(query, query_variables).data.yield_self do |data|
35
34
  # avoid having to do query.foo.foo if possible
36
35
  data.send(graph_name)
37
36
  rescue NoMethodError
@@ -44,6 +43,10 @@ module Graphtown
44
43
  self.class.graphql_queries
45
44
  end
46
45
 
46
+ def parse_graphql_query(client, value)
47
+ value.is_a?(Proc) ? Graphlient::Query.new(&value).to_s : client.parse(value)
48
+ end
49
+
47
50
  def graphql_client
48
51
  Graphlient::Client.new(graphql_endpoint) do |client|
49
52
  configure_graphql_client(client)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Graphtown
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphtown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared White
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-20 00:00:00.000000000 Z
11
+ date: 2021-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bridgetown
@@ -134,6 +134,7 @@ executables: []
134
134
  extensions: []
135
135
  extra_rdoc_files: []
136
136
  files:
137
+ - ".github/FUNDING.yml"
137
138
  - ".gitignore"
138
139
  - ".rspec"
139
140
  - ".rubocop.yml"
@@ -165,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
166
  - !ruby/object:Gem::Version
166
167
  version: '0'
167
168
  requirements: []
168
- rubygems_version: 3.0.6
169
+ rubygems_version: 3.1.4
169
170
  signing_key:
170
171
  specification_version: 4
171
172
  summary: Easily consume GraphQL APIs for your Bridgetown website using a tidy Builder