Skip to content

Mutations

The mutation hooks wrap TanStack Query’s useMutation and handle cache invalidation automatically.

Inserts one or more rows. The values parameter accepts an array of insert objects.

import { useEntityInsert } from '@graphql-suite/query'
const { mutate, isPending } = useEntityInsert(articleEntity, { id: true, title: true })
// Insert a single row
mutate({
values: [{ title: 'New Article', authorId: userId, status: 'draft' }],
})

Signature: useEntityInsert(entity, returning?, options?)

ParameterTypeDescription
entityEntityClientThe entity client to insert into
returningselect objectOptional fields to return from inserted rows
optionsmutation optionsOptional callbacks and cache settings

The mutate function accepts { values: InsertInput[] }.

Updates rows matching a filter.

import { useEntityUpdate } from '@graphql-suite/query'
const { mutate } = useEntityUpdate(articleEntity, { id: true, title: true })
mutate({
set: { status: 'published', publishedAt: new Date().toISOString() },
where: { id: { eq: articleId } },
})

Signature: useEntityUpdate(entity, returning?, options?)

The mutate function accepts { set: UpdateInput, where?: FilterInput }.

Deletes rows matching a filter.

import { useEntityDelete } from '@graphql-suite/query'
const { mutate } = useEntityDelete(articleEntity, { id: true })
mutate({ where: { id: { eq: articleId } } })

Signature: useEntityDelete(entity, returning?, options?)

The mutate function accepts { where?: FilterInput }.

All three mutation hooks accept the same options object as their third parameter:

OptionTypeDefaultDescription
invalidatebooleantrueInvalidate cached queries after success
invalidateKeyunknown[]['gql']Query key prefix to invalidate
onSuccess(data) => voidCallback after successful mutation
onError(error) => voidCallback on mutation error

By default, after a successful mutation, all TanStack Query caches with the ['gql'] prefix are invalidated. This causes any active useEntityList, useEntityQuery, and useEntityInfiniteQuery hooks to refetch.

To disable automatic invalidation:

const { mutate } = useEntityInsert(articleEntity, { id: true }, {
invalidate: false,
})

To invalidate a narrower set of queries, provide a more specific key:

const { mutate } = useEntityUpdate(articleEntity, { id: true }, {
invalidateKey: ['gql', 'list'],
})