UNPKG

985 BPlain TextView Raw
1/**
2 * 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.
3 *
4 * @example
5 * ```
6 * import { gql } from 'graphql-request'
7 *
8 * await request('https://foo.bar/graphql', gql`...`)
9 * ```
10 *
11 * @remarks
12 *
13 * 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.
14 */
15export const gql = (chunks: TemplateStringsArray, ...variables: unknown[]): string => {
16 return chunks.reduce(
17 (acc, chunk, index) => `${acc}${chunk}${index in variables ? String(variables[index]) : ``}`,
18 ``,
19 )
20}