🚀 Socket Launch Week Day 4:Socket MCP Adds Org Alerts, Threat Feed Review, and Package Inspection.Learn more
Sign In

generate-gql

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

generate-gql

Generating GraphQL code from JavaScript object.

unpublished
latest
Source
npmnpm
Version
0.0.1-alpha.9
Version published
Maintainers
1
Created
Source

generate-gql

npm version install size npm downloads

Generating GraphQL code from JavaScript object.

Installation

# with pnpm
pnpm i generate-gql

# or with npm
npm i generate-gql

Example

import generateGQL from 'generate-gql'

const gql = generateGQL({
  /**
   * Operation type, can be 'query', 'mutation' or 'subscription'.
   *
   * Default: 'query'.
   */
  type: 'query',

  /**
   * The operation body.
   */
  body: {
    users: {
      /**
       * Optional alias of the field.
       */
      alias: 'results',

      /**
       * Optional arguments. If not set, no arguments will be generated.
       */
      args: {
        first: 10,

        filter: {
          /**
           * String values will be enclosed in double quotes.
           */
          nameContains: 'j',

          /**
           * Enum value is an object with only one key named "$enum".
           * The enum value will not be enclosed in double quotes.
           */
          gender: { $enum: 'Male' },

          /**
           * Object can be nested.
           */
          createdAt: {
            after: '2020-01-01T00:00:00.000Z',
            before: '2023-01-01T00:00:00.000Z'
          },

          /**
           * The fields with the value of `null` or `undefined` will
           * be ignored. If all fields of an object are ignored, the
           * object will also be ignored.
           */
          hasFriendsWith: {
            nameContains: 'j',
            gender: null,
            createdAt: {
              after: null,
              before: null
            },
            hasFriendsWith: undefined
          }
        }
      },

      /**
       * Optional fields.
       * If it is not an object or it is an empty object,
       * nothing will be generated.
       */
      fields: {
        edges: {
          /**
           * If the value type is `number` or `boolean` and the value is truthy,
           * the field will be generated. If the value is falsy, the field will
           * be skipped.
           */
          name: true,
          gender: 1,

          /**
           * If the value is a string and it is not an empty string,
           * the string will be used as the alias of this field.
           * If the value is an empty string, the field will be skipped.
           */
          lastName: 'familyName',

          /**
           * Object can be nested.
           */
          friends: {
            name: true,
            gender: true
          },

          /**
           * The value can be an array with only one element.
           * The element is used to describe the field.
           * You can set the alias, arguments or sub-fields
           * of this field here.
           */
          articles: [
            {
              /**
               * Optional field alias.
               */
              alias: 'top5articles',

              /**
               * Optional arguments.
               */
              args: {
                first: 5,
                orderBy: {
                  field: 'totalLike',
                  order: { $enum: 'DESC' }
                }
              },

              /**
               * Optional sub-fields.
               */
              fields: {
                title: true,
                summary: true
              }
            }
          ]
        }
      }
    }
  }
})

console.log(gql)

The output is:

query {
  results: users (
    first: 10
    filter: {
      nameContains: "j"
      gender: Male
      createdAt: {
        after: "2020-01-01T00:00:00.000Z"
        before: "2023-01-01T00:00:00.000Z"
      }
      hasFriendsWith: {
        nameContains: "j"
      }
    }
  ) {
    edges {
      name
      gender
      familyName: lastName
      friends {
        name
        gender
      }
      top5articles: articles (
        first: 5
        orderBy: {
          field: "totalLike"
          order: DESC
        }
      ) {
        title
        summary
      }
    }
  }
}

FAQs

Package last updated on 27 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts