A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.npmjs.com/package/ibm-graphql-query-generator below:

ibm-graphql-query-generator - npm

GraphQL Query Generator

This library will generate randomized GraphQL queries from a given schema.

It can be used in a few ways:

Usage

Install the library using:

npm i ibm-graphql-query-generator

Usage typically relies on the generateRandomQuery function, which can be imported like this:

const { generateRandomQuery } = require("ibm-graphql-query-generator")
Minimal working example

All arguments are exposed as variables. Providers can be passed to provide values for these variables. For example:

const { generateRandomQuery } = require("ibm-graphql-query-generator")

const { buildSchema, print } = require("graphql")

const schema = `
  type Query {
    getUser(name: String!): User
    getCompany(companyName: String!): Company
  }

  type User {
    firstName: String
    lastName: String
    employer: Company
    friends: [User]
  }

  type Company {
    name: String
    CEO: User
    employees: [User]
  }
`

const configuration = {
  depthProbability: 0.5,
  breadthProbability: 0.5,
  providerMap: {
    "*__*__name": () => {
      const nameList = ["Alfred", "Barbara", "Charles", "Dorothy"]

      return nameList[Math.floor(Math.random() * nameList.length)]
    },
    "*__*__companyName": () => {
      const companyNameList = [
        "All Systems Go",
        "Business Brothers",
        "Corporate Comglomerate Company",
        "Data Defenders"
      ]

      return companyNameList[
        Math.floor(Math.random() * companyNameList.length)
      ]
    }
  }
}

const { queryDocument, variableValues, seed } = generateRandomQuery(
  buildSchema(schema),
  configuration
)

console.log(print(queryDocument))
console.log(variableValues)
Example configurations

We provide sample query generators for the following APIs:

Generating random queries

This library exposes two functions for generating random GraphQL queries:

Configuration

Functions of this library accept a configuration object with the following properties:

Example:

const cfg = {
  'depthProbability':        0.5,
  'breadthProbability':      0.5,
  'maxDepth':                5,
  'ignoreOptionalArguments': true,
  'argumentsToIgnore':       [],
  'argumentsToConsider':     [],
  'providerMap':             {'*__*__*': null},
  'considerInterfaces':      false,
  'considerUnions':          false,
  'seed':                    1,
  'pickNestedQueryField':    false
}
Provider map

Whenever a randomly generated query or mutation requires an argument, this library exposes that argument as a variable. The names of these variables reflect the type and field that the argument applies to, as well as the argument name like so:

<type>__<fieldName>__<argumentName>

Alternatively, you can match using:

<type>__<fieldName>

In this case, the provider function returns an object where multiple arguments are present.

The providerMap contains values or value producing functions for the variables in a query.

The keys of the providerMap are either the exact name of the variable or a wildcard where either the type, fieldName, and/or argumentName are replaced by a *. For example, the key *__*__limit matches all variables for arguments of name limit, no matter for what field the argument is used or in which type. If no providerMap is passed, a default map {'*__*__*': null} is used, which provides a null value to all variables (Note: this can be problematic if an argument defines a non-null value).

The values of the providerMap are either the concrete argument values, or a function that will be invoked to provide that value. A provider function will get passed a map of all already provided variable values, which allows to provide values based on previous ones. It will also get passed the argument type (as a GraphQLNamedType) as a second argument.

This library also exposes a function matchVarName(query: string, candidates: string[]): string that, from a given list of variable names and/or variable name queries, finds the one matching the given variable name or query.

Note that for variables with an enumeration type, this library automatically chooses one value at random.

Errors

Generating random queries or mutations may fail in some cases:

Citing this library

If you use this library in a scientific publication, please cite:

  1. The library, as: IBM, graphql-query-generator, 2020. https://github.com/IBM/graphql-query-generator.
  2. The work in which we introduced it, as: Cha, Wittern, Baudart, Davis, Mandel, and Laredo. A Principled Approach to GraphQL Query Cost Analysis. ESEC/FSE 2020.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4