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-query-gen

v1.3.2

Published

Node.js module to generate queries with random input data from graphQL endpoint or schema

Downloads

6,686

Readme

graphql-query-gen

Node.js module to generate queries from graphQL endpoint or from schema text.

It reads graphql SDL using introspection query and then generates queries/mutations with random input data. Additionally lists down inputs, types as well. Also, there you get schema string and some statistics data as well.

Installation

npm install graphql-query-gen

Live Demo

Graphql Query Generator is a chrome extension built using this. You can install and try it to see what this node module can do for you.

Usage


const qGen = require('graphql-query-gen');

const options = {};

// Work with endpoint
qGen.processEndpoint("endpoint-url", options).then(result => console.log(result));

// work with schema
const s = `
type Character {
  id: ID
  name: String
}
type Jedi  {
  id: ID
  side: String
}
type Droid {
  id: ID
  model: Model
}

type Model {
    key: String
}

input TestInput {
    key1: String
    key2: Int
}
union People = Character | Jedi | Droid
type Query {
  allPeople(input: TestInput, input2: String): [People]
}
`;
const result  = graphqlQueryGen.processSchema(s, options);

// work with SDL
const fs = require('fs');
let rawdata = fs.readFileSync('sdl.json');
const sdl = JSON.parse(rawdata);
const result  = graphqlQueryGen.processSDL(sdl.data, options);


// or you can do like below to handle errors as well
const qGen = require('graphql-query-gen');
try {
    qGen.generateQueries(
        "http://graphql-endpoint-url/",
        {
            depth: 5,
            indentBy: 2
        }
    ).then(
        result => console.log(result)
        // or do what you want to do with it
    );
} catch (err) {
    console.log(err.message);
}

Options

{
    debug: false, // Boolean [Default is false] -> Would print additional log message to help in debugging if true
    filter: null, // String [Default is null ] -> You can give a query or mutation name or part of it
    responseDepth: 7, // Number [Default is 5] -> For query/mutation result the nesting level of fields
    inputDepth: 8, // Number [Default is 7] -> For query/muation input the nesting level of fields
    spacer: ' ', // String [Default is ''] -> To indent query/mutation the space character (e.g. to print on HTML page you can use   )
    indentBy: 2, // Number [Default is 4] -> The number of spacer to use for indentation.
    inputVariables: true, // Boolean [Default is false] -> In generated query input would be in form or variable if true, else inline input.
    duplicatePercentage: 75 // Number [Default is 75] -> for types and input check and list for duplicates based on this threshold value.
}

Output

Sample output looks like following


{
  operations: [
    { name: 'Query', options: [Array] },
    { name: 'Mutation', options: [] },
    { name: 'Subscription', options: [] }
  ],
  types: [
    { name: 'Character', definition: '{\n  id: ID\n  name: String\n}' },
    { name: 'Jedi', definition: '{\n  id: ID\n  side: String\n}' },
    { name: 'Droid', definition: '{\n  id: ID\n  model: Model\n}' },
    { name: 'Model', definition: '{\n  key: String\n}' }
  ],
  inputs: [
    {
      name: 'TestInput',
      definition: '{\n  key1: String\n  key2: Int\n}'
    }
  ],
  statistics: {
    counts: { queries: 1, mutations: 0, subscriptions: 0, types: 4, inputs: 1 },
    suggestions: { duplicates: [Array] }
  },
  schema: 'SCHEMA_AS_STRING'
}

TODO

  • Subscriptions are not processed and will resturn as empty as of now, planned for future release
  • Fragments support is limited for now, plan to enhance in future release
  • Better logging, current version has console.log and console.debug only