Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gpoitch/graphql-builder
A simple string utility to build GraphQL queries
https://github.com/gpoitch/graphql-builder
fragment graphql mutation query string
Last synced: 3 months ago
JSON representation
A simple string utility to build GraphQL queries
- Host: GitHub
- URL: https://github.com/gpoitch/graphql-builder
- Owner: gpoitch
- Created: 2017-04-18T22:23:56.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-22T14:42:04.000Z (over 7 years ago)
- Last Synced: 2024-10-10T18:27:22.963Z (4 months ago)
- Topics: fragment, graphql, mutation, query, string
- Language: JavaScript
- Homepage:
- Size: 32.2 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GraphQL Builder [![Build Status](https://travis-ci.org/gpoitch/graphql-builder.svg?branch=master)](https://travis-ci.org/gpoitch/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:
Strings with fragment interpolation
```js
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
```js
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
}
*/
```