Nice Philanthropist Metalhead
Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@wheelroom/graphql-query-builder

1.4.7 • Public • Published

graphql-query-builder

About

Small library to build graphQL queries dynamically.

How does it work?

The library translates a javascript object into a GraphQL query string.

The object below:

{
  fields: {
    firstName: {},
    lastName: {},
  },
  operationName: 'firstAndLastName',
  operationType: 'query',
}

Turns into:

query firstAndLastName {
  firstName
  lastName
}

Examples

See jest test script for a larger example.

import {
  graphqlQueryBuilder as qb,
  Question,
} from '@wheelroom/graphql-query-builder'
 
const question: Question = {
  fields: {
    firstName: {
      arguments: {
        id: '4',
      },
    },
    lastName: {
      directive: {
        name: '@include',
        value: 'if: $withFriends',
      },
    },
  },
  operationType: 'query',
}
 
console.log(qb(question))
// query {
//  firstName(id: 4)
//  lastName @include(if: $withFriends)
// }

Supported fields

Have a look into the typescript definitions for that.

Install

npm i @wheelroom/graphql-query-builder

DownloadsWeekly Downloads

18

Version

1.4.7

License

MIT

Unpacked Size

28.8 kB

Total Files

16

Last publish

Collaborators

  • avatar
  • avatar