├── .babelrc ├── .editorconfig ├── .eslintignore ├── .eslintrc.json ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bin └── fetch-graphql-schema.js ├── package.json └── src ├── __tests__ └── index.spec.js └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["latest"] 3 | } 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | indent_style = space 10 | indent_size = 2 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | lib 3 | .nyc_output 4 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb-base", 3 | "env": { 4 | "node": true, 5 | "jest": true 6 | }, 7 | "rules": { 8 | "arrow-parens": ["error", "as-needed"] 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | npm-debug.log 4 | node_modules 5 | lib 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "7" 5 | - "6" 6 | - "4" 7 | cache: 8 | directories: 9 | - node_modules 10 | script: "npm run lint && npm run test:cov" 11 | after_script: "npm install coveralls@2 && nyc report --reporter=text-lcov | coveralls" 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 0.2.1 / 2016-11-22 2 | ================== 3 | - [fix] should not ignore `bin/`, `lib/` 4 | 5 | 0.2.0 / 2016-11-22 6 | ================== 7 | - [new] cli support 8 | 9 | 0.1.0 / 2016-11-11 10 | ================== 11 | - initial implement 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yoctol (github.com/Yoctol/fetch-graphql-schema) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fetch-graphql-schema 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![Build Status][travis-image]][travis-url] 5 | [![Test coverage][coveralls-image]][coveralls-url] 6 | [![Dependency Status][david_img]][david_site] 7 | 8 | > fetch GraphQL schema via introspection query 9 | 10 | 11 | ## Install 12 | 13 | ``` 14 | $ npm install fetch-graphql-schema 15 | ``` 16 | 17 | 18 | ## Usage 19 | 20 | ```js 21 | const fetchSchema = require('fetch-graphql-schema'); 22 | 23 | fetchSchema('http://localhost:8080/graphql') 24 | .then(schemaJSON => { 25 | /** 26 | * { 27 | * "data": { 28 | * "__schema": { 29 | * "queryType": { 30 | * "name": "Query" 31 | * .... 32 | */ 33 | }); 34 | 35 | fetchSchema('http://localhost:8080/graphql', { readable: true }) 36 | .then(clientSchema => { 37 | /** 38 | * type User implements Node { 39 | * id: ID! 40 | * name: String! 41 | * } 42 | */ 43 | }); 44 | //=> 45 | ``` 46 | 47 | 48 | ## API 49 | 50 | ### fetchGraphqlSchema(url, [options]) 51 | 52 | #### url 53 | 54 | *Required* 55 | Type: `string` 56 | 57 | URL of GraphQL server. 58 | 59 | #### options 60 | 61 | ##### readable 62 | 63 | Type: `boolean` 64 | Default: `false` 65 | 66 | resolve `.graphql` instead of `.json`. 67 | 68 | ## CLI 69 | 70 | #### Usage 71 | ``` 72 | $ fetch-graphql-schema 73 | ``` 74 | 75 | #### Options 76 | ``` 77 | -o, --output Specify an output filename. 78 | -r, --readable Resolve .graphql instead of .json. 79 | ``` 80 | 81 | #### Examples 82 | ``` 83 | $ fetch-graphql-schema http://api.server/graphql -o schema.json 84 | $ fetch-graphql-schema http://api.server/graphql -o schema.graphql -r 85 | ``` 86 | 87 | ## License 88 | 89 | MIT © [C.T. Lin](https://github.com/Yoctol/fetch-graphql-schema) 90 | 91 | [npm-image]: https://badge.fury.io/js/fetch-graphql-schema.svg 92 | [npm-url]: https://npmjs.org/package/fetch-graphql-schema 93 | [travis-image]: https://travis-ci.org/Yoctol/fetch-graphql-schema.svg 94 | [travis-url]: https://travis-ci.org/Yoctol/fetch-graphql-schema 95 | [coveralls-image]: https://coveralls.io/repos/Yoctol/fetch-graphql-schema/badge.svg?branch=master&service=github 96 | [coveralls-url]: https://coveralls.io/r/Yoctol/fetch-graphql-schema?branch=master 97 | [david_img]: https://david-dm.org/Yoctol/fetch-graphql-schema.svg 98 | [david_site]: https://david-dm.org/Yoctol/fetch-graphql-schema 99 | 100 | -------------------------------------------------------------------------------- /bin/fetch-graphql-schema.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const chalk = require('chalk'); 6 | const meow = require('meow'); 7 | const mkdirp = require('mkdirp'); 8 | const fetchSchema = require('../lib'); // eslint-disable-line import/no-unresolved 9 | 10 | const cli = meow(` 11 | 12 | Usage 13 | $ fetch-graphql-schema 14 | 15 | Options 16 | -o, --output Specify an output filename. 17 | -r, --readable Resolve .graphql instead of .json. 18 | 19 | Examples 20 | $ fetch-graphql-schema http://api.server/graphql -o schema.json 21 | $ fetch-graphql-schema http://api.server/graphql -o schema.graphql -r 22 | 23 | `); 24 | 25 | if (cli.input.length === 0) { 26 | console.log(cli.help); 27 | process.exit(0); 28 | } 29 | 30 | const schemaUrl = cli.input[0]; 31 | const output = cli.flags.o || cli.flags.output; 32 | 33 | if (!output) { 34 | fetchSchema(schemaUrl, { readable: cli.flags.r || cli.flags.readable }) 35 | .then(console.log); 36 | } 37 | 38 | if (output && typeof output === 'string') { 39 | const outputPath = path.resolve(output); 40 | 41 | console.log(chalk.yellow(`Start fetching "${schemaUrl}"...`)); 42 | 43 | fetchSchema(schemaUrl, { readable: cli.flags.r || cli.flags.readable }).then(schemaJSON => { 44 | mkdirp.sync((path.dirname(outputPath))); 45 | fs.writeFileSync(outputPath, schemaJSON); 46 | 47 | console.log(chalk.green(`Successfully fetched "${schemaUrl}" and saved to "${outputPath}"`)); 48 | }); 49 | } 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fetch-graphql-schema", 3 | "version": "0.2.1", 4 | "description": "fetch GraphQL schema via introspection query", 5 | "main": "lib/index.js", 6 | "license": "MIT", 7 | "repository": "Yoctol/fetch-graphql-schema", 8 | "author": { 9 | "name": "C.T. Lin", 10 | "email": "chentsulin@gmail.com", 11 | "url": "github.com/chentsulin" 12 | }, 13 | "engines": { 14 | "node": ">=0.12.0" 15 | }, 16 | "bin": "bin/fetch-graphql-schema.js", 17 | "files": [ 18 | "bin/", 19 | "lib/", 20 | "src/" 21 | ], 22 | "keywords": [ 23 | "graphql", 24 | "schema", 25 | "json", 26 | "server", 27 | "fetch" 28 | ], 29 | "dependencies": { 30 | "chalk": "^1.1.3", 31 | "graphql": "^0.8.0", 32 | "meow": "^3.7.0", 33 | "mkdirp": "^0.5.1", 34 | "node-fetch": "^2.6.1" 35 | }, 36 | "devDependencies": { 37 | "babel-cli": "^6.18.0", 38 | "babel-jest": "^17.0.0", 39 | "babel-preset-latest": "^6.16.0", 40 | "eslint": "^3.5.0", 41 | "eslint-config-airbnb-base": "^10.0.1", 42 | "eslint-plugin-import": "^2.2.0", 43 | "fetch-mock": "^5.5.0", 44 | "jest": "^17.0.0", 45 | "rimraf": "^2.5.4" 46 | }, 47 | "scripts": { 48 | "build": "npm run clean && babel src -d lib --ignore __tests__", 49 | "clean": "rimraf lib", 50 | "test": "jest", 51 | "test:cov": "jest --coverage", 52 | "lint": "eslint .", 53 | "preversion": "npm test" 54 | }, 55 | "devEngines": { 56 | "node": "4.x || 5.x || 6.x || 7.x", 57 | "npm": "2.x || 3.x || 4.x" 58 | }, 59 | "jest": { 60 | "verbose": true, 61 | "collectCoverageFrom": [ 62 | "src/**/*.js" 63 | ], 64 | "testEnvironment": "node" 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/__tests__/index.spec.js: -------------------------------------------------------------------------------- 1 | import fetchSchema from '../'; 2 | 3 | it('be defined', () => { 4 | expect(fetchSchema).toBeDefined(); 5 | }); 6 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const fetch = require('node-fetch'); 2 | 3 | const { 4 | buildClientSchema, 5 | introspectionQuery, 6 | printSchema, 7 | } = require('graphql/utilities'); 8 | 9 | 10 | module.exports = function fetchGraphQLSchema(url, options) { 11 | options = options || {}; // eslint-disable-line no-param-reassign 12 | return fetch(url, { 13 | method: 'POST', 14 | headers: { 15 | Accept: 'application/json', 16 | 'Content-Type': 'application/json', 17 | }, 18 | body: JSON.stringify({ 19 | query: introspectionQuery, 20 | }), 21 | }) 22 | .then(res => res.json()) 23 | .then(schemaJSON => { 24 | if (options.readable) { 25 | return printSchema(buildClientSchema(schemaJSON.data)); 26 | } 27 | return JSON.stringify(schemaJSON, null, 2); 28 | }); 29 | }; 30 | --------------------------------------------------------------------------------