Skip to content

Creating a Client

There are two ways to create a client, depending on whether your Drizzle schema is available at the call site.

Infers all entity types directly from your Drizzle schema and config. This is the recommended approach when the Drizzle schema is importable.

import { createDrizzleClient } from '@graphql-suite/client'
import * as schema from './db/schema'
const client = createDrizzleClient({
schema,
config: { suffixes: { list: 's', single: '' } },
url: '/graphql',
headers: () => ({ Authorization: `Bearer ${getToken()}` }),
})
OptionTypeDescription
schemaRecord<string, unknown>Your Drizzle schema exports (tables and relations)
configBuildSchemaConfigMust match the server’s config for correct query generation
urlstring | () => stringGraphQL endpoint URL (static or dynamic)
headersHeadersInit | () => HeadersInit | Promise<HeadersInit>Optional headers (static, sync, or async function)

Manual setup with a pre-built schema descriptor. Use this when you have a generated schema descriptor (from generateEntityDefs or buildSchemaDescriptor) rather than the raw Drizzle schema.

import { GraphQLClient } from '@graphql-suite/client'
const client = new GraphQLClient({
url: '/graphql',
schema: schemaDescriptor,
headers: { Authorization: 'Bearer ...' },
})

The schema parameter is a SchemaDescriptor — a record mapping entity names to their query/mutation names, fields, and relations. This is the same format produced by buildSchemaDescriptor and generateEntityDefs.

Both url and headers accept functions, which is useful for authentication tokens and multi-tenant setups:

const client = createDrizzleClient({
schema,
config,
url: () => `${getApiBase()}/graphql`,
headers: async () => ({
Authorization: `Bearer ${await refreshToken()}`,
'X-Tenant-Id': getTenantId(),
}),
})

The url function is called synchronously on each request. The headers function can be async and is awaited before each request.

Once created, use the .entity() method to access type-safe operations:

const articles = await client.entity('article').query({
select: { id: true, title: true },
})

You can also execute raw GraphQL queries with .execute():

const data = await client.execute(
`query { articles { id title } }`,
{},
)