|
| 1 | +<img src="https://voyager.postman.com/logo/postman-logo-orange.svg" width="320" alt="The Postman Logo"> |
1 | 2 |
|
2 |
| -<a href="https://www.getpostman.com/"><img src="https://assets.getpostman.com/common-share/postman-logo-horizontal-320x132.png" /></a><br /> |
3 |
| -_Manage all of your organization's APIs in Postman, with the industry's most complete API development environment._ |
4 |
| - |
5 |
| -*Supercharge your API workflow.* |
| 3 | +*Supercharge your API workflow.* |
6 | 4 | *Modern software is built on APIs. Postman helps you develop APIs faster.*
|
7 | 5 |
|
8 |
| -# graphql-to-postman [](https://travis-ci.com/postmanlabs/graphql-to-postman) |
| 6 | +# GraphQL to Postman Collection |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +<a href="https://www.npmjs.com/package/graphql-to-postman" alt="Latest Stable Version"></a> |
| 11 | +<a href="https://www.npmjs.com/package/graphql-to-postman" alt="Total Downloads"></a> |
| 12 | + |
| 13 | +#### Contents |
| 14 | + |
| 15 | +1. [Getting Started](#getting-started) |
| 16 | +2. [Command Line Interface](#command-line-interface) |
| 17 | + 1. [Options](#options) |
| 18 | + 2. [Usage](#usage) |
| 19 | +3. [Using the converter as a NodeJS module](#using-the-converter-as-a-nodejs-module) |
| 20 | + 1. [Convert Function](#convert) |
| 21 | + 2. [Options](#options) |
| 22 | + 3. [ConversionResult](#conversionresult) |
| 23 | + 4. [Sample usage](#sample-usage) |
| 24 | + 5. [Validate function](#validate-function) |
| 25 | +4. [Conversion Schema](#conversion-schema) |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | + |
| 32 | +## 💭 Getting Started |
| 33 | + |
| 34 | +To use the converter as a Node module, you need to have a copy of the NodeJS runtime. The easiest way to do this is through npm. If you have NodeJS installed you have npm installed as well. |
| 35 | + |
| 36 | +```terminal |
| 37 | +$ npm install graphql-to-postman |
| 38 | +``` |
| 39 | + |
| 40 | +If you want to use the converter in the CLI, install it globally with NPM: |
| 41 | + |
| 42 | +```terminal |
| 43 | +$ npm i -g graphql-to-postman |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +## 📖 Command Line Interface |
| 48 | + |
| 49 | +The converter can be used as a CLI tool as well. The following [command line options](#options) are available. |
| 50 | + |
| 51 | +`gql2postman [options]` |
| 52 | + |
| 53 | +### Options |
| 54 | + |
| 55 | +- `-s <source>`, `--spec <source>` |
| 56 | + Used to specify the GraphQL specification (file path) which is to be converted |
| 57 | + |
| 58 | +- `-o <destination>`, `--output <destination>` |
| 59 | + Used to specify the destination file in which the collection is to be written |
| 60 | + |
| 61 | +- `-p`, `--pretty` |
| 62 | + Used to pretty print the collection object while writing to a file |
| 63 | + |
| 64 | +- `-i`, `--interface-version` |
| 65 | + Specifies the interface version of the converter to be used. Value can be 'v2' or 'v1'. Default is 'v2'. |
| 66 | + |
| 67 | +- `-O`, `--options` |
| 68 | + Used to supply options to the converter, for complete options details see [here](/OPTIONS.md) |
| 69 | + |
| 70 | +- `-c`, `--options-config` |
| 71 | + Used to supply options to the converter through config file, for complete options details see [here](/OPTIONS.md) |
| 72 | + |
| 73 | +- `-t`, `--test` |
| 74 | + Used to test the collection with an in-built sample specification |
| 75 | + |
| 76 | +- `-v`, `--version` |
| 77 | + Specifies the version of the converter |
| 78 | + |
| 79 | +- `-h`, `--help` |
| 80 | + Specifies all the options along with a few usage examples on the terminal |
| 81 | + |
| 82 | + |
| 83 | +### Usage |
| 84 | + |
| 85 | +- Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing and using provided options |
| 86 | +```terminal |
| 87 | +$ gql2postman -s spec.yaml -o collection.json -p -O depth=3,includeDeprecatedFields=true |
| 88 | +``` |
| 89 | + |
| 90 | +- Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing and using provided options via config file |
| 91 | +```terminal |
| 92 | +$ gql2postman -s spec.yaml -o collection.json -p -c ./examples/cli-options-config.json |
| 93 | +``` |
| 94 | + |
| 95 | +- Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing and using provided options with larger depth limit |
| 96 | + to make sure more detailed and nested data is generated. |
| 97 | +```terminal |
| 98 | +$ gql2postman -s spec.yaml -o collection.json -p -O depth=7,includeDeprecatedFields=true,optimizeConversion=false |
| 99 | +``` |
| 100 | + |
| 101 | +- Testing the converter |
| 102 | +```terminal |
| 103 | +$ gql2postman --test |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | +## 🛠 Using the converter as a NodeJS module |
| 108 | + |
| 109 | +In order to use the convert in your node application, you need to import the package using `require`. |
| 110 | + |
| 111 | +```javascript |
| 112 | +var Converter = require('graphql-to-postman') |
| 113 | +``` |
| 114 | + |
| 115 | +The converter provides the following functions: |
| 116 | + |
| 117 | +### Convert |
| 118 | + |
| 119 | +The convert function takes in your GraphQL schema or SDL and converts it to a Postman collection. |
| 120 | + |
| 121 | +Signature: `convert (data, options, callback);` |
| 122 | + |
| 123 | +**data:** |
| 124 | + |
| 125 | +```javascript |
| 126 | +{ type: 'file', data: 'filepath' } |
| 127 | +OR |
| 128 | +{ type: 'string', data: '<entire GraphQL string - schema or SDL>' } |
| 129 | +``` |
| 130 | + |
| 131 | +**options:** |
| 132 | +```javascript |
| 133 | +{ |
| 134 | + depth: 4, |
| 135 | + includeDeprecatedFields: false, |
| 136 | + optimizeConversion: false |
| 137 | +} |
| 138 | +/* |
| 139 | +All three properties are optional. Check the options section below for possible values for each option. |
| 140 | +*/ |
| 141 | +``` |
| 142 | + |
| 143 | +**callback:** |
| 144 | +```javascript |
| 145 | +function (err, result) { |
| 146 | + /* |
| 147 | + result = { |
| 148 | + result: true, |
| 149 | + output: [ |
| 150 | + { |
| 151 | + type: 'collection', |
| 152 | + data: {..collection object..} |
| 153 | + } |
| 154 | + ] |
| 155 | + } |
| 156 | + */ |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +### Options |
| 161 | + |
| 162 | +- `depth` - The number of levels of information that should be returned. (A depth level of “1” returns that object and |
| 163 | + its properties. A depth of “2” will return all the nodes connected to the level 1 node, etc.) |
| 164 | + |
| 165 | +- `includeDeprecatedFields` - Generated queries will include deprecated fields or not. |
| 166 | + |
| 167 | +- `optimizeConversion` - Optimizes conversion for schemas with complex and nested input objects by reducing the depth to |
| 168 | + which input objects are resolved in GraphQL variables. |
| 169 | + |
| 170 | +### ConversionResult |
| 171 | + |
| 172 | +- `result` - Flag responsible for providing a status whether the conversion was successful or not. |
| 173 | + |
| 174 | +- `reason` - Provides the reason for an unsuccessful conversion, defined only if result if `false`. |
| 175 | + |
| 176 | +- `output` - Contains an array of Postman objects, each one with a `type` and `data`. The only type currently supported is `collection`. |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +### Sample Usage |
| 181 | +```javascript |
| 182 | +const fs = require('fs'), |
| 183 | + Converter = require('graphql-to-postman'), |
| 184 | + gqlData = fs.readFileSync('sample-spec.yaml', {encoding: 'UTF8'}); |
| 185 | + |
| 186 | +Converter.convert({ type: 'string', data: gqlData }, |
| 187 | + {}, (err, conversionResult) => { |
| 188 | + if (!conversionResult.result) { |
| 189 | + console.log('Could not convert', conversionResult.reason); |
| 190 | + } |
| 191 | + else { |
| 192 | + console.log('The collection object is: ', conversionResult.output[0].data); |
| 193 | + } |
| 194 | + } |
| 195 | +); |
| 196 | +``` |
| 197 | + |
| 198 | +### Validate Function |
| 199 | + |
| 200 | +The validate function is meant to ensure that the data that is being passed to the [convert function](#convert-function) is a valid JSON object or a valid (YAML/JSON) string. |
| 201 | + |
| 202 | +The validate function is synchronous and returns a status object which conforms to the following schema |
| 203 | + |
| 204 | +#### Validation object schema |
| 205 | + |
| 206 | +```javascript |
| 207 | +{ |
| 208 | + type: 'object', |
| 209 | + properties: { |
| 210 | + result: { type: 'boolean'}, |
| 211 | + reason: { type: 'string' } |
| 212 | + }, |
| 213 | + required: ['result'] |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +##### Validation object explanation |
| 218 | +- `result` - true if the data is valid GraphQL and can be passed to the convert function |
9 | 219 |
|
10 |
| -This module can be used to convert a GraphQL schema into a [Postman Collection V2](https://github.com/postmanlabs/postman-collection) format. |
| 220 | +- `reason` - Provides a reason for an unsuccessful validation of the specification |
0 commit comments