data-client-graphql-setup
reactive/data-client/.agents/skills/data-client-graphql-setup/SKILL.md
Set up @data-client/graphql for GraphQL APIs. Configures GQLEndpoint with auth and custom options. Use after data-client-setup detects GraphQL patterns.
Skill2k starsChanged yesterday
- Installs packages
What's in it
- GraphQL Protocol Setup
- Installation
- GQLEndpoint Setup
- Basic Configuration
- Detection Checklist
- With Authentication
- Async Authentication (token refresh)
- Custom Error Handling
- Defining Queries and Mutations
- Query Example
- Mutation Example
- With Collection
- Usage in Components
- Next Steps
- References
---
name: data-client-graphql-setup
description: Set up @data-client/graphql for GraphQL APIs. Configures GQLEndpoint with auth and custom options. Use after data-client-setup detects GraphQL patterns.
disable-model-invocation: true
---
# GraphQL Protocol Setup
This guide configures `@data-client/graphql` for a project. Use it once the Data Client provider is set up and the project calls GraphQL APIs.
## Installation
Install the GraphQL package alongside the core package:
```bash
# npm
npm install @data-client/graphql
# yarn
yarn add @data-client/graphql
# pnpm
pnpm add @data-client/graphql
```
## GQLEndpoint Setup
### Basic Configuration
Create a file at `src/api/gql.ts` (or similar):
```ts
import { GQLEndpoint } from '@data-client/graphql';
export const gql = new GQLEndpoint('/graphql');
```
### Detection Checklist
Scan the existing codebase for GraphQL patterns:
1. **GraphQL endpoint URL**: Look for `/graphql` or custom paths
2. **Authentication**: Check for auth headers in existing GraphQL client setup
3. **Custom headers**: API keys, tenant IDs, etc.
4. **Error handling**: GraphQL error parsing patterns
### With Authentication
```ts
import { GQLEndpoint } from '@data-client/graphql';
export const gql = new GQLEndpoint('/graphql', {
getHeaders() {
const token = localStorage.getItem('authToken');
return {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
};
},
});
```
### Async Authentication (token refresh)
```ts
import { GQLEndpoint } from '@data-client/graphql';
export const gql = new GQLEndpoint('/graphql', {
async getHeaders() {
const token = await getValidToken();
return {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
};
},
});
```
### Custom Error Handling
```ts
import { GQLEndpoint } from '@data-client/graphql';
class CustomGQLEndpoint extends GQLEndpoint {
async fetchResponse(input: RequestInfo, init: RequestInit): Promise<any> {
const response = await super.fetchResponse(input, init);
// Handle GraphQL errors
if (response.errors?.length) {
const authError = response.errors.find(
e => e.extensions?.code === 'UNAUTHENTICATED'
);
if (authError) {
window.dispatchEvent(new CustomEvent('auth:expired'));
}
}
return response;
}
}
export const gql = new CustomGQLEndpoint('/graphql');
```
## Defining Queries and Mutations
### Query Example
```ts
import { gql } from './gql';
import { User } from '../schemas/User';
export const getUser = gql.query(
(v: { id: string }) => `
query GetUser($id: ID!) {
user(id: $id) {
id
name
email
}
}
`,
{ schema: User },
);
```
### Mutation Example
```ts
import { gql } from './gql';
import { User } from '../schemas/User';
export const updateUser = gql.mutation(
(v: { id: string; name: string }) => `
mutation UpdateUser($id: ID!, $name: String!) {
updateUser(id: $id, name: $name) {
id
name
}
}
`,
{ schema: User },
);
```
### With Collection
```ts
import { gql } from './gql';
import { User, UserCollection } from '../schemas/User';
export const listUsers = gql.query(
() => `
query ListUsers {
users {
id
name
email
}
}
`,
{ schema: UserCollection },
);
export const createUser = gql.mutation(
(v: { name: string; email: string }) => `
mutation CreateUser($name: String!, $email: String!) {
createUser(name: $name, email: $email) {
id
name
email
}
}
`,
{ schema: UserCollection.push },
);
```
## Usage in Components
```tsx
import { useSuspense, useController } from '@data-client/react';
import { getUser, updateUser } from './api/users';
function UserProfile({ id }: { id: string }) {
const user = useSuspense(getUser, { id });
const ctrl = useController();
const handleUpdate = async (name: string) => {
await ctrl.fetch(updateUser, { id, name });
};
return (
<div>
<h1>{user.name}</h1>
<button onClick={() => handleUpdate('New Name')}>Update</button>
</div>
);
}
```
## Next Steps
1. Apply skill "data-client-schema" to define Entity classes
2. Apply skill "data-client-react" or "data-client-vue" for usage
## References
- [GQLEndpoint](https://dataclient.io/graphql/api/GQLEndpoint) - Full GQLEndpoint API
- [GraphQL Guide](https://dataclient.io/graphql) - GraphQL usage guide
- [Authentication Guide](references/auth.md) - Auth patterns for GraphQL
More agent context in reactive/data-client
28 other files this repository gives its agents.
Cursor rule
Skill
- changeset.agents/skills/changeset/SKILL.md
- data-client-endpoint-setup.agents/skills/data-client-endpoint-setup/SKILL.md
- data-client-manager.agents/skills/data-client-manager/SKILL.md
- data-client-react.agents/skills/data-client-react/SKILL.md
- data-client-react-testing.agents/skills/data-client-react-testing/SKILL.md
- data-client-rest-setup.agents/skills/data-client-rest-setup/SKILL.md
- data-client-rest.agents/skills/data-client-rest/SKILL.md
- data-client-schema.agents/skills/data-client-schema/SKILL.md
- data-client-setup.agents/skills/data-client-setup/SKILL.md
- data-client-v0.18-migration.agents/skills/data-client-v0.18-migration/SKILL.md
- data-client-vue.agents/skills/data-client-vue/SKILL.md
- data-client-vue-testing.agents/skills/data-client-vue-testing/SKILL.md
- frontend-design.agents/skills/frontend-design/SKILL.md
- initialize.agents/skills/initialize/SKILL.md
- interface-design.agents/skills/interface-design/SKILL.md
- packages-documentation.agents/skills/packages-documentation/SKILL.md
- path-to-regexp-v8-migration.agents/skills/path-to-regexp-v8-migration/SKILL.md
- pr.agents/skills/pr/SKILL.md
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool registry_write, action report. How to connect one.

