npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-to-postman

v0.3.0

Published

Generates a Postman Collection from a GraphQL schema.

Downloads

9,666

Readme

Supercharge your API workflow. Modern software is built on APIs. Postman helps you develop APIs faster.

GraphQL to Postman Collection

Build Status

npm npm

Contents

  1. Getting Started
  2. Command Line Interface
    1. Options
    2. Usage
  3. Using the converter as a NodeJS module
    1. Convert Function
    2. Options
    3. ConversionResult
    4. Sample usage
    5. Validate function
  4. Conversion Schema


💭 Getting Started

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.

$ npm install graphql-to-postman

If you want to use the converter in the CLI, install it globally with NPM:

$ npm i -g graphql-to-postman

📖 Command Line Interface

The converter can be used as a CLI tool as well. The following command line options are available.

gql2postman [options]

Options

  • -s <source>, --spec <source> Used to specify the GraphQL specification (file path) which is to be converted

  • -o <destination>, --output <destination> Used to specify the destination file in which the collection is to be written

  • -p, --pretty Used to pretty print the collection object while writing to a file

  • -i, --interface-version Specifies the interface version of the converter to be used. Value can be 'v2' or 'v1'. Default is 'v2'.

  • -O, --options Used to supply options to the converter, for complete options details see here

  • -c, --options-config Used to supply options to the converter through config file, for complete options details see here

  • -t, --test Used to test the collection with an in-built sample specification

  • -v, --version Specifies the version of the converter

  • -h, --help Specifies all the options along with a few usage examples on the terminal

Usage

  • Takes a specification (spec.yaml) as an input and writes to a file (collection.json) with pretty printing and using provided options
$ gql2postman -s spec.yaml -o collection.json -p -O depth=3,includeDeprecatedFields=true
  • 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
$ gql2postman -s spec.yaml -o collection.json -p  -c ./examples/cli-options-config.json
  • 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 to make sure more detailed and nested data is generated.
$ gql2postman -s spec.yaml -o collection.json -p -O depth=7,includeDeprecatedFields=true,optimizeConversion=false
  • Testing the converter
$ gql2postman --test

🛠 Using the converter as a NodeJS module

In order to use the convert in your node application, you need to import the package using require.

var Converter = require('graphql-to-postman')

The converter provides the following functions:

Convert

The convert function takes in your GraphQL schema or SDL and converts it to a Postman collection.

Signature: convert (data, options, callback);

data:

{ type: 'file', data: 'filepath' }
OR
{ type: 'string', data: '<entire GraphQL string - schema or SDL>' }

options:

{
  depth: 4,
  includeDeprecatedFields: false,
  optimizeConversion: false
}
/*
All three properties are optional. Check the options section below for possible values for each option.
*/

callback:

function (err, result) {
  /*
  result = {
    result: true,
    output: [
      {
        type: 'collection',
        data: {..collection object..}
      }
    ]
  }
  */
}

Options

  • depth - The number of levels of information that should be returned. (A depth level of “1” returns that object and its properties. A depth of “2” will return all the nodes connected to the level 1 node, etc.)

  • includeDeprecatedFields - Generated queries will include deprecated fields or not.

  • optimizeConversion - Optimizes conversion for schemas with complex and nested input objects by reducing the depth to which input objects are resolved in GraphQL variables.

ConversionResult

  • result - Flag responsible for providing a status whether the conversion was successful or not.

  • reason - Provides the reason for an unsuccessful conversion, defined only if result if false.

  • output - Contains an array of Postman objects, each one with a type and data. The only type currently supported is collection.

Sample Usage

const fs = require('fs'),
  Converter = require('graphql-to-postman'),
  gqlData = fs.readFileSync('sample-spec.yaml', {encoding: 'UTF8'});

Converter.convert({ type: 'string', data: gqlData },
  {}, (err, conversionResult) => {
    if (!conversionResult.result) {
      console.log('Could not convert', conversionResult.reason);
    }
    else {
      console.log('The collection object is: ', conversionResult.output[0].data);
    }
  }
);

Validate Function

The validate function is meant to ensure that the data that is being passed to the convert function is a valid JSON object or a valid (YAML/JSON) string.

The validate function is synchronous and returns a status object which conforms to the following schema

Validation object schema

{
  type: 'object',
  properties: {
    result: { type: 'boolean'},
    reason: { type: 'string' }
  },
  required: ['result']
}
Validation object explanation
  • result - true if the data is valid GraphQL and can be passed to the convert function

  • reason - Provides a reason for an unsuccessful validation of the specification