Usage
selecting-fields

Selecting fields

Gqlts queries are similar to graphql queries in structure, to select some fields you pass an object where the keys are the field names.

Here is an example of a query made with the client generated from the Quick start guide

import { createClient } from './generated'
 
const client = createClient()
 
client
    .query({
        countries: {
            name: true,
            code: true,
            languages: {
                name: true,
            },
        },
    })
    .then(console.log)

Passing arguments

If you want to pass arguments to a query you must use an array where the first object represents the arguments and the second object the fields selection.

import { createClient } from './generated'
const client = createClient()
 
client
    .query({
        countries: [
            {
                filter: { code: 'BR' },
            },
            {
                name: true,
                code: true,
                languages: {
                    name: true,
                },
            },
        ],
    })
    .then(console.log)
 
client
    .mutation({
        createUser: [{ user: { name: 'user' } }, { name: true, age: true }],
    })
    .then(console.log)

Querying all fields

Gqlts let you query all scalar fields in a type by using the everything object:

import { everything } from './generated' // everything is just an object: const everything = { __scalar: true };
 
client
    .query({
        countries: {
            ...everything, // same as __scalar: true
            languages: {
                ...everything,
            },
        },
    })
    .then((x) => console.log(JSON.stringify(x)))

everything queries only the leaf types, you have to manually query object types

Excluding fields

You can also exclude some fields from the selection passing falsy values

client
    .query({
        countries: {
            ...everything, // same as __scalar: true
            id: false,
            code: false,
        },
    })
    .then((x) => console.log(JSON.stringify(x)))