@wheelroom/graphql-query-builder

Site framework based on Contentful and Gatsby

Usage no npm install needed!

<script type="module">
  import wheelroomGraphqlQueryBuilder from 'https://cdn.skypack.dev/@wheelroom/graphql-query-builder';
</script>

README

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.