prototype
MAmattpocock/skills336k近 7 天新增 4,217 个 star
Set up @data-client/graphql for GraphQL APIs. Configures GQLEndpoint with auth and custom options. Use after data-client-setup detects GraphQL patterns.
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.
Install the GraphQL package alongside the core package:
# npm
npm install @data-client/graphql
# yarn
yarn add @data-client/graphql
# pnpm
pnpm add @data-client/graphql
Create a file at src/api/gql.ts (or similar):
import { GQLEndpoint } from '@data-client/graphql';
export const gql = new GQLEndpoint('/graphql');
Scan the existing codebase for GraphQL patterns:
/graphql or custom pathsgetHeaders() runs synchronously, so it can add a token that is already in memory:
import { GQLEndpoint } from '@data-client/graphql';
export const gql = new GQLEndpoint('/graphql', {
getHeaders(headers: HeadersInit): HeadersInit {
const token = localStorage.getItem('authToken');
return token ? { ...headers, Authorization: `Bearer ${token}` } : headers;
},
});
If the token must be awaited (for example, refreshed), override fetchResponse() in a subclass. GraphQL errors in a
response reject with GQLNetworkError, whose errors holds the response's errors array:
import { GQLEndpoint, GQLNetworkError } from '@data-client/graphql';
class AuthGQLEndpoint extends GQLEndpoint<any> {
async fetchResponse(input: RequestInfo, init: RequestInit) {
const headers = new Headers(init.headers);
headers.set('Authorization', `Bearer ${await getValidToken()}`);
try {
return await super.fetchResponse(input, { ...init, headers });
} catch (error) {
if (
error instanceof GQLNetworkError &&
error.errors.some((e: any) => e.extensions?.code === 'UNAUTHENTICATED')
) {
window.dispatchEvent(new CustomEvent('auth:expired'));
}
throw error;
}
}
}
export const gql = new AuthGQLEndpoint('/graphql');
The second argument of gql.query() and gql.mutation() is the schema, shaped like the response's data: a query
for user { ... } takes { user: User }, and the component reads .user. The schema normalizes each response into the
shared cache, and the examples below import Entity classes that must already exist. Apply the skill "data-client-schema" to define them before writing endpoints.
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
}
}
`,
{ user: User },
);
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
}
}
`,
{ updateUser: User },
);
import { gql } from './gql';
import { UserCollection } from '../schemas/User';
export const listUsers = gql.query(
`
query ListUsers {
users {
id
name
email
}
}
`,
{ users: 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
}
}
`,
{ createUser: UserCollection.push },
);
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>
);
}
Apply skill "data-client-react" or "data-client-vue" for usage.
MAmattpocock/skills336k近 7 天新增 4,217 个 star
AFaffaan-m/ECC314k近 7 天新增 3,503 个 star
FIfirecrawl/firecrawl214k近 7 天新增 4,815 个 star
ANanthropics/skills203k近 7 天新增 2,822 个 star
用 React、Tailwind 和 shadcn/ui 构建多组件页面。
ANanthropics/skills203k近 7 天新增 2,822 个 star