Skip to content

Commit b913789

Browse files
authored
Merge pull request #1 from Shopify/feature/initial-code
Adding all the code
2 parents 9830f76 + b9be24a commit b913789

File tree

8 files changed

+180
-38
lines changed

8 files changed

+180
-38
lines changed

README.md

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Circle CI](https://circleci.com/gh/Shopify/graphql-js-schema-fetch.png?circle-token=b91d60ffd2266c360cfc56697c847af8e95f715b)](https://circleci.com/gh/Shopify/graphql-js-schema-fetch)
2+
13
# graphql-js-schema-fetch
24

35
It fetches the JSON representation of the GraphQL schema from a live server
@@ -7,62 +9,49 @@ It fetches the JSON representation of the GraphQL schema from a live server
79
- [Installation](#installation)
810
- [Examples](#examples)
911
- [API](#api)
10-
+ [`const instance = graphqlJsSchemaFetch([options])`](#const-instance-graphqljsschemafetch-options)
11-
+ [`someFunction(variable, callback)`](#somefunction-variable-callback)
12-
+ [`anotherFunction()`](#anotherfunction)
1312
- [License](http://github.com/Shopify/graphql-js-schema-fetch/blob/master/LICENSE.md)
1413

1514
## Installation
15+
1616
```bash
17-
$ npm install graphql-js-schema-fetch
17+
git clone [email protected]:Shopify/graphql-js-schema-fetch.git
18+
cd graphql-js-schema-fetch
19+
npm install
20+
npm link
1821
```
1922

2023
## Examples
2124

22-
#### Example 1
23-
24-
Decribe Example 1. Stumptown selfies put a bird on it occupy, scenester ramps jean shorts next level kale chips seitan:
25+
To fetch the json representation of the graphql schema from a live server that
26+
implements the GraphQL (and optionally the Relay) spec, run:
2527

26-
```javascript
27-
import graphqlJsSchemaFetch from 'graphql-js-schema-fetch';
28-
29-
/********************************/
30-
/********************************/
31-
/* -- PUT AN EXAMPLE IN HERE -- */
32-
/********************************/
33-
/********************************/
28+
```bash
29+
graphql-js-schema-fetch --url https://www.my-server.com/api
3430
```
3531

36-
#### Example 2
32+
If your server requires additional credentials or headers, use the `--header`
33+
option:
3734

38-
Decribe Example 1. Stumptown selfies put a bird on it occupy, scenester ramps jean shorts next level kale chips seitan:
35+
```bash
36+
graphql-js-schema-fetch --url https://www.my-server.com/api --header "Authorization: Basic abc123" --header "X-API-Version: 1.1"
37+
```
3938

40-
```javascript
41-
import graphqlJsSchemaFetch from 'graphql-js-schema-fetch';
39+
If your server uses something other than the `POST` method for its API, use the
40+
`--method` option:
4241

43-
/********************************/
44-
/********************************/
45-
/* -- PUT AN EXAMPLE IN HERE -- */
46-
/********************************/
47-
/********************************/
42+
```bash
43+
graphql-js-schema-fetch --url https://www.my-server.com/api --method GET
4844
```
4945

5046
## API
5147

52-
#### `const instance = graphqlJsSchemaFetch([options]);`
53-
54-
Options you can pass `graphqlJsSchemaFetch`:
55-
- `option1` - Replace with a description of option 1
56-
- `option2` - Replace with a description of option 2
57-
- `option3` - Replace with a description of option 2
58-
59-
#### `instance.someFunction(variable, callback);`
60-
61-
Replace with `someFunction` description. Hoodie post-ironic polaroid salvia, microdosing vice ethical etsy bushwick roof party swag. Farm-to-table humblebrag etsy neutra synth.
62-
63-
#### `instance.anotherFunction();`
48+
#### `const instance = graphqlJsSchemaFetch(url, method, [headers]);`
6449

65-
Replace with `anotherFunction` description. Tacos polaroid cronut trust fund mumblecore biodiesel viral hella helvetica actually organic. Stumptown selfies put a bird on it occupy, scenester ramps jean shorts next level kale chips seitan.
50+
Params you can pass `graphqlJsSchemaFetch`:
51+
- `url` - The url where your GraphQL API resides
52+
- `method` - The HTTP method to use while making the request
53+
- `headers` - A hash representing the key value pairs of headers to pass to
54+
`node-fetch`
6655

6756
## License
6857

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
machine:
22
node:
3-
version: v4.5.0
3+
version: v6.7.0

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "graphql-js-schema-fetch",
3+
"version": "1.0.0",
4+
"description": "A simple command line tool for fetching the JSON representation of the GraphQL schema from a live server",
5+
"main": "lib/index.js",
6+
"bin": "lib/cli.js",
7+
"license": "MIT",
8+
"author": "Shopify Inc.",
9+
"dependencies": {
10+
"graphql": "0.7.0",
11+
"minimist": "1.2.0",
12+
"node-fetch": "1.6.3"
13+
},
14+
"devDependencies": {
15+
"babel": "6.5.2",
16+
"babel-cli": "6.11.4",
17+
"babel-core": "6.13.2",
18+
"babel-plugin-add-shopify-header": "1.0.5",
19+
"babel-preset-shopify": "13.0.0",
20+
"babel-register": "6.11.6",
21+
"eslint": "3.3.1",
22+
"eslint-plugin-mocha": "4.5.1",
23+
"eslint-plugin-shopify": "14.0.0",
24+
"mocha": "3.0.2"
25+
},
26+
"scripts": {
27+
"test": "npm run lint-allow-warning && mocha --compilers js:babel-register -u tdd test/index.js",
28+
"prepublish": "babel -d lib/ src/",
29+
"lint": "eslint --max-warnings 0 -c .eslintrc.json src/ test/",
30+
"lint-allow-warning": "eslint -c .eslintrc.json src/ test/"
31+
},
32+
"keywords": [
33+
"graphql,schema"
34+
],
35+
"repository": {
36+
"type": "git",
37+
"url": "git://github.com/Shopify/graphql-js-schema-fetch.git"
38+
},
39+
"homepage": "https://github.com/Shopify/graphql-js-schema-fetch",
40+
"bugs": {
41+
"url": "https://github.com/Shopify/graphql-js-schema-fetch/issues"
42+
}
43+
}

src/cli.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env node
2+
3+
import parseArgs from './parse-args';
4+
import fetchSchema from './index';
5+
import help from './help';
6+
7+
const args = parseArgs(process.argv.slice(2));
8+
9+
if (args.showHelp) {
10+
console.log(help);
11+
process.exit(0);
12+
}
13+
14+
fetchSchema(args.url, args.method, args.headers).then((schema) => {
15+
console.log(JSON.stringify(schema));
16+
}).catch((error) => {
17+
console.error(error);
18+
});

src/help.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default `
2+
usage: graphql-js-schema-fetch --url my-api.com [--method=POST] [--header="...", --header="..."]
3+
4+
Fetch the json representation of a GraphQL Schema from a live server.
5+
6+
arguments:
7+
--url The URL where you GraphQL API resides.
8+
--method The HTTP method to use during the fetch (default: 'POST')
9+
--header[,--header] Any headers to send along with the request. Specify this
10+
option multiple times for multiple headers. (example:
11+
--header "Authorization: Basic abc123" --header "X-Version: 1")
12+
`;

src/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fetch from 'node-fetch';
2+
import {introspectionQuery} from 'graphql/utilities/introspectionQuery';
3+
4+
export default function fetchSchema(url, method = 'POST', headers = {}) {
5+
const body = `query=${introspectionQuery}`;
6+
7+
return fetch(url, {
8+
method,
9+
headers,
10+
body
11+
}).then((response) => {
12+
return response.json();
13+
});
14+
}

src/parse-args.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import minimist from 'minimist';
2+
3+
export default function parseArgs(rawArgs) {
4+
const args = minimist(rawArgs, {default: {method: 'POST'}});
5+
6+
if (args.help || !args.url) {
7+
return {showHelp: true};
8+
}
9+
10+
const url = args.url;
11+
const method = args.method;
12+
const headers = [].concat(args.header).reduce((headerAcc, header) => {
13+
const pair = header.split(':');
14+
15+
headerAcc[pair[0].trim()] = pair[1].trim();
16+
17+
return headerAcc;
18+
}, {
19+
Accept: 'application/json',
20+
'Content-Type': 'application/x-www-form-urlencoded'
21+
});
22+
23+
return {url, method, headers};
24+
}

test/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import assert from 'assert';
2+
import parseArgs from '../src/parse-args';
3+
4+
suite('This will test arg parsing', () => {
5+
test('it indicates `showHelp` and skips all other args when the help flag is passed', () => {
6+
const args = parseArgs(['--header', 'some-header: data', '--help', '--url', 'www.com']);
7+
8+
assert.deepEqual(args, {showHelp: true});
9+
});
10+
11+
test('it indicates `showHelp` if required args aren\'t present', () => {
12+
const args = parseArgs(['--header', 'Authorization: Basic abc123', '--method', 'post']);
13+
14+
assert.deepEqual(args, {showHelp: true});
15+
});
16+
17+
test('it transforms single header args into a hash', () => {
18+
const args = parseArgs(['--header', 'Authorization: Basic abc123', '--url', 'www.com']);
19+
20+
assert.equal(args.headers.Authorization, 'Basic abc123');
21+
});
22+
23+
test('it transforms multi-header args into a hash', () => {
24+
const args = parseArgs(['--header', 'Authorization: Basic abc123', '--header', 'X-API-Key: token123', '--url', 'www.com']);
25+
26+
assert.equal(args.headers.Authorization, 'Basic abc123');
27+
assert.equal(args.headers['X-API-Key'], 'token123');
28+
});
29+
30+
test('it supplies sane default headers', () => {
31+
const args = parseArgs(['--header', 'Authorization: Basic abc123', '--header', 'X-API-Key: token123', '--url', 'www.com']);
32+
33+
assert.deepEqual(args.headers.Accept, 'application/json');
34+
assert.deepEqual(args.headers['Content-Type'], 'application/x-www-form-urlencoded');
35+
});
36+
37+
test('it cleans up arbitrary whitespace in headers', () => {
38+
const args = parseArgs(['--header', ' Authorization: Basic abc123 ', '--url', 'www.com']);
39+
40+
assert.equal(args.headers.Authorization, 'Basic abc123');
41+
});
42+
});

0 commit comments

Comments
 (0)