graphql-builder

0.2.0 • Public • Published

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
}
*/

Package Sidebar

Install

npm i graphql-builder

Weekly Downloads

3

Version

0.2.0

License

MIT

Last publish

Collaborators

  • gdub