@spoot/gql
v1.0.0
Published
GraphQL query execution helper with Zod schema validation
Downloads
79
Readme
@spoot/gql
GraphQL query execution helper with Zod schema validation. Wraps the native fetch API to send GraphQL requests and parse/validate the response.
Install
npm install @spoot/gqlUsage
import { gql, fetchGql } from "@spoot/gql";
import { z } from "zod";
const GET_USER = gql`
query GetUser($id: ID!) {
user(id: $id) {
id
name
}
}
`;
const UserSchema = z.object({
user: z.object({ id: z.string(), name: z.string() }),
});
const data = await fetchGql(
new URL("https://api.example.com/graphql"),
{ Authorization: "Bearer token" },
GET_USER,
UserSchema,
);Throws a descriptive error on HTTP failure or GraphQL errors in the response.
Development
pnpm typecheck # type-check
pnpm build:lib # compile to dist/