Skip to content

Commit 9ab6adc

Browse files
authored
Merge pull request #103 from github/add-graphql
Add GraphQL query examples
2 parents b14badc + 89d7a93 commit 9ab6adc

File tree

7 files changed

+81
-0
lines changed

7 files changed

+81
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ But here it is, broken down:
1111
* _api_: here's a bunch of sample code relating to the API. Subdirectories in this
1212
category are broken up by language. Do you have a language sample you'd like added?
1313
Make a pull request and we'll consider it.
14+
* _graphql_: here's a bunch of sample GraphQL queries that can be run against our [GitHub GraphQL API](https://developers.github.com/early-access/graphql).
1415
* _hooks_: wanna find out how to write a consumer for [our web hooks](https://developer.github.com/webhooks/)? The examples in this subdirectory show you how. We are open for more contributions via pull requests.
1516
* _pre-receive-hooks_: this one contains [pre-receive-hooks](https://help.github.com/enterprise/admin/guides/developer-workflow/about-pre-receive-hooks/) that can block commits on GitHub Enterprise that do not fit your requirements. Do you have more great examples? Create a pull request and we will check it out.

graphql/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://rubygems.org"
2+
3+
gem "httparty"

graphql/Gemfile.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
httparty (0.14.0)
5+
multi_xml (>= 0.5.2)
6+
multi_xml (0.5.5)
7+
8+
PLATFORMS
9+
ruby
10+
11+
DEPENDENCIES
12+
httparty
13+
14+
BUNDLED WITH
15+
1.11.2

graphql/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# GitHub GraphQL API: Query Samples
2+
3+
This repository holds query samples for the GitHub GraphQL API. It's an easy way to get started using the GraphQL API for common workflows. You can copy and paste these queries into [GraphQL Explorer](https://developer.github.com/early-access/graphql/explorer) or you can use the included script.
4+
5+
### How to use the included script
6+
7+
1. Generate a [personal access token](https://help.github.com/articles/creating-an-access-token-for-command-line-use/) for use with these queries.
8+
1. Run `bundle install`.
9+
1. Pick the name of one of the included queries like `viewer.graphql`.
10+
1. Run `TOKEN=<OAuth Token> bin/run-query viewer.graphql`. Replace `<OAuth Token>` with your personal access token.

graphql/bin/run-query

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env ruby
2+
3+
require "httparty"
4+
5+
query_file_name = ARGV[0]
6+
unless query_file_name
7+
print "Please provide a file name from 'queries'."
8+
exit
9+
end
10+
11+
query_file_path = File.expand_path(File.join("queries", query_file_name))
12+
query = File.read(query_file_path)
13+
14+
response = HTTParty.post("https://api.github.com/graphql",
15+
:headers => {
16+
"User-Agent" => "github/graphql-samples",
17+
"Authorization" => "token #{ENV["TOKEN"]}",
18+
"Content-Type" => "application/json"
19+
},
20+
:body => {:query => query}.to_json
21+
)
22+
23+
puts "=== Results from #{File.basename(query_file_name)}"
24+
puts JSON.pretty_generate(response)
25+
puts
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
query {
2+
viewer {
3+
repositories(last: 30) {
4+
edges {
5+
node {
6+
owner {
7+
login
8+
}
9+
name
10+
stargazers(first: 5) {
11+
totalCount
12+
edges {
13+
node {
14+
login
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}
22+
}

graphql/queries/viewer.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query {
2+
viewer {
3+
login
4+
}
5+
}

0 commit comments

Comments
 (0)