Skip to content

A simple string utility to build GraphQL queries

Notifications You must be signed in to change notification settings

gpoitch/graphql-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Builder Build Status

A simple string utility to build GraphQL queries.

🔑 Features:

  • Automatically interpolates and includes fragment definitions into queries
  • Define fragments/queries/mutations as objects so you can extend them

Examples:

Strings with fragment interpolation
import { fragment, query, mutation } from 'graphql-builder'

const PostAuthorFragment = fragment(`
  fragment PostAuthor on User { 
    id
    name
  }
`)

const PostQuery = query(`
  query ($id: Int!) {
    post (id: $id) {
      id
      title
      author {
        ${PostAuthorFragment}
      }
    }
  }
`)

console.log(PostQuery)
/*
query ($id: Int!) {
  post (id: $id) {
    id
    title
    author {
      ...PostAuthor
    }
  }
}

fragment PostAuthor on User {
  id
  name
}
*/
Building with all options
import { fragment, query, mutation } from 'graphql-builder'

const PostAuthorFragment = fragment({
  name: 'PostAuthor', // name is optional.  If omitted, will be `on`
  on: 'User',
  definition: `{ 
    id
    name
  }`
})

const PostQuery = query({
  name: 'PostQuery' // name is optional, unless you have mutliple operations in your request.
  variables: {      // variables are optional. Useful for extending queries.
    id: 'Int!'
  },
  definition: `{
    post (id: $id) {
      id
      title
      author {
        ${PostAuthorFragment}
      }
    }
  }`
})

console.log(PostQuery)
/*
query PostQuery ($id: Int!) {
  post (id: $id) {
    id
    title
    author {
      ...PostAuthor
    }
  }
}

fragment PostAuthor on User {
  id
  name
}
*/