Skip to content

Commit a4630b0

Browse files
authored
Merge pull request #5 from Shopify/support-url-positional-arg
Add support for positional URL argument
2 parents 896012c + 471ba48 commit a4630b0

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/help.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
export default `
2-
usage: graphql-js-schema-fetch --url my-api.com [--method=POST] [--header="...", --header="..."]
2+
usage: graphql-js-schema-fetch [URL | --url] [--method=POST] [--header="...", --header="..."]
33
44
Fetch the json representation of a GraphQL Schema from a live server.
55
6-
arguments:
6+
Arguments:
77
--url The URL where you GraphQL API resides.
88
--method The HTTP method to use during the fetch (default: 'POST')
99
--header[,--header] Any headers to send along with the request. Specify this
1010
option multiple times for multiple headers. (example:
1111
--header "Authorization: Basic abc123" --header "X-Version: 1")
12+
13+
Examples:
14+
Fetch schema with default headers:
15+
graphql-js-schema-fetch https://api.example.com > schema.json
16+
17+
Fetch schema with authorization header:
18+
graphql-js-schema-fetch https://api.example.com \
19+
--header "Authorization: Basic abc123" > schema.json
20+
21+
Fetch schema with multiple headers:
22+
graphql-js-schema-fetch https://api.example.com \
23+
--header "Authorization: Basic abc123" \
24+
--header "X-Version: 1" > schema.json
1225
`;

src/parse-args.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ export default function parseArgs(rawArgs) {
1313
}
1414
});
1515

16-
if (args.help || !args.url) {
16+
const url = args._[0] || args.url;
17+
18+
if (args.help || !url) {
1719
return {showHelp: true};
1820
}
1921

20-
const url = args.url;
2122
const method = args.method;
2223

2324
const headers = [].concat(args.header).reduce((headerAcc, header) => {

test/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ suite('This will test arg parsing', () => {
3939
assert.deepEqual(args.headers.Accept, 'application/json');
4040
assert.deepEqual(args.headers['Content-Type'], 'application/json');
4141
});
42+
43+
test('it supports positional url argument', () => {
44+
const args = parseArgs(['https://graphql.example.com']);
45+
46+
assert.deepEqual(args.url, 'https://graphql.example.com');
47+
});
48+
49+
test('it supports named url argument', () => {
50+
const args = parseArgs(['--url', 'https://graphql.example.com']);
51+
52+
assert.deepEqual(args.url, 'https://graphql.example.com');
53+
});
4254
});

0 commit comments

Comments
 (0)