graphql-builder
v0.2.0
Published
A simple string utility to build GraphQL queries
Downloads
27
Maintainers
Readme
GraphQL Builder
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:
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
}
*/
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
}
*/