Coder Social home page Coder Social logo

graphql-to-postman's Introduction

The Postman Logo

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

graphql-to-postman's People

Contributors

abhijitkane avatar akshay2402 avatar dependabot[bot] avatar jonathanhaviv avatar shreys7 avatar umeshp7 avatar vshingala avatar web-flow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

graphql-to-postman's Issues

AWS scalar types support

Hi,

I tried to import my schema from AWS AppSync into a GraphQL Postman collection but it is complaining about unknown type: AWSDateTime & AWSDate, weird thing is I also tried manually change the .graphql schema file where it mentions AWSDateTime & AWSDate to just use the scalar types of Date & DateTime (as per graphql's available scalar types) & that also returned unknown type errors.

Is this a known issue or am I understanding these errors incorrectly?

Thank you in advance

GraphQL introspection query variables

Hi,
When “auto fetch” triggers for my GraphQL schema, I need to pass some variables along with the authorization header.
Right now, I see that the introspection query for “auto fetch” passes variables collection as empty. How can I pass variables to the “auto fetch” introspection query when using postman desktop application?

Add support to auto-load graphql SDL

I have seen this implemented in other GraphQL environments (e.g GraphiQL), and it would be a nice-to-have feature that significantly improves user testing speed. Currently, I can manually add a GraphQL Schema to a Postman API and have it suggest keywords correctly, but making modifications to the GraphQL API itself means that I have to update the Postman API with the new SDL, then refresh from the request body tab. Ideally, clicking on the refresh button in the request body tab should autoload the SDL.

[Bug] Missing fields from generated collection.

I currently have a workflow that generates a GraphQL schema and then uses Postman to generate a collection to test our API.

I've come across an issue where certain nested fields don't get included in the generated collection. I have a reproducible schema below:

schema {
  query: Query
}

"Query root"
type Query {
  testQuery(
    parameter: String
  ): RootType
}

type RootType {
  elements: [ArrayElement]
}

type ArrayElement {
  a: NestedElement
  b: NestedElement
  c: NestedElement
}

type NestedElement {
  element: Element
}

type Element {
  text: String
  subElement: SubElement
  name: String
}

type SubElement {
  code: String
  text: String
  moreText: String
} 

This produces an incorrect query in the collection that looks like this:

query testQuery ($parameter: String) {
    testQuery (parameter: $parameter) {
        elements {
            a {
                element {
                    text
                    subElement {
                        code
                        text
                        moreText
                    }
                    name
                }
            }
            b {
                element {
                    text
                    name
                }
            }
            c {
                element {
                    text
                    name
                }
            }
        }
    }
}

Where elements b and c are missing the subElement. It is always the first element that has the subElement and the remaining elements are missing it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.