graphql-query-to-json

v2.0.1
Convert graphQL queries and mutations to easily readable JSON representations.

graphql-query-to-json

Statement coverage Function coverage Line coverage Branches badge

This is a simple module that takes a graphQL query string and converts it into a JSON object. Think of it as the reverse of the excellent module json-to-graphql-query.

Installation

npm install graphql-query-to-json
# or
yarn add graphql-query-to-json

Usage

const {graphQlQueryToJson} = require("graphql-query-to-json")

const query = `
query GetThisStuff($name: String, $lastName: String) {
    viewer {
        personal(criteria: {
            name: $name,
            lastName: $lastName
        }) {
            name
            address
        }
    }
}
`
const result = graphQlQueryToJson(query, {
    variables: {
        name: "PETER",
        lastName: "SCHMIDT",
    },
})
expect(result).toEqual({
    query: {
        viewer: {
            personal: {
                __args: {
                    criteria: {
                        name: "PETER",
                        lastName: "SCHMIDT",
                    },
                },
                name: true,
                address: true,
            },
        },
    },
})

Debugging

Run the VSCode configuration "Jest" and set a breakpoint in the code wherever you feel the need to inspect.

npm i graphql-query-to-json

Metadata

  • MIT
  • Whatever
  • Tray.io
  • released 11/22/2021

Downloads

Maintainers