Skip to content
Graffle is a work in progress. Learn more.

Raw Document Node Typed

This example shows how to send a request using a Document instance for the GraphQL document while also being typesafe in regards to the passed variables and return type.

ts
import type { 
TypedQueryDocumentNode
} from 'graphql'
import {
gql
,
Graffle
} from 'graffle'
const
graffle
=
Graffle
.
create
({
schema
: `https://countries.trevorblades.com/graphql`,
}) /*************************************** Variation 1 *************************************** * - * - * - * You can pass type variables to the `gql` template tag. * - */ { const
document
=
gql
<{
countries
: {
name
: string;
continent
: {
name
: string } }[] }, {
filter
: string[] }>`
query countries ($filter: [String!]) { countries (filter: { name: { in: $filter } }) { name continent { name } } } ` const
data
= await
graffle
.
raw
({
document
,
variables
: {
filter
: [`Canada`, `Germany`, `Japan`] } })
console
.
log
(
data
?.
countries
)
} /*************************************** Variation 2 *************************************** * - * - * - * You can also cast the type if you have a reference to a pre constructed type. * - */ { type
Document
=
TypedQueryDocumentNode
<
{
countries
: {
name
: string;
continent
: {
name
: string } }[] },
{
filter
: string[] }
> const
document
:
Document
=
gql
`
query countries ($filter: [String!]) { countries (filter: { name: { in: $filter } }) { name continent { name } } } ` const
data
= await
graffle
.
raw
({
document
,
variables
: {
filter
: [`Canada`, `Germany`, `Japan`] } })
console
.
log
(
data
?.
countries
)
}

Outputs

txt
[
  { name: 'Canada', continent: { name: 'North America' } },
  { name: 'Germany', continent: { name: 'Europe' } },
  { name: 'Japan', continent: { name: 'Asia' } }
]
txt
[
  { name: 'Canada', continent: { name: 'North America' } },
  { name: 'Germany', continent: { name: 'Europe' } },
  { name: 'Japan', continent: { name: 'Asia' } }
]

Released under the MIT License.