graphql-request
Version:
Minimal GraphQL client supporting Node and browsers for scripts or simple apps.
21 lines (20 loc) • 985 B
text/typescript
/**
* Convenience passthrough template tag to get the benefits of tooling for the gql template tag. This does not actually parse the input into a GraphQL DocumentNode like graphql-tag package does. It just returns the string with any variables given interpolated. Can save you a bit of performance and having to install another package.
*
* @example
* ```
* import { gql } from 'graphql-request'
*
* await request('https://foo.bar/graphql', gql`...`)
* ```
*
* @remarks
*
* Several tools in the Node GraphQL ecosystem are hardcoded to specially treat any template tag named "gql". For example see this prettier issue: https://github.com/prettier/prettier/issues/4360. Using this template tag has no runtime effect beyond variable interpolation.
*/
export const gql = (chunks: TemplateStringsArray, ...variables: unknown[]): string => {
return chunks.reduce(
(acc, chunk, index) => `${acc}${chunk}${index in variables ? String(variables[index]) : ``}`,
``,
)
}