@digital-retex/flos-pim-graphql-client
v1.0.1
Published
TypeScript client generated from local GraphQL schema files
Readme
Product GraphQL TypeScript Client
TypeScript library generated from the GraphQL schemas in this repository, designed to execute type-safe calls against a GraphQL endpoint.
Main features
- Automatic TypeScript type generation from GraphQL schema (
graphql-codegen). - Typed GraphQL documents (
TypedDocumentNode) for compile-time safety. - Ready-to-use client built on
graphql-request(ProductGraphqlClient). - Dedicated methods for core queries:
getProduct(code)searchProducts(filter)
- Generic
request(options)method for custom queries. - Included Postman collection for manual testing:
product-graphql.postman_collection.json.
Requirements
- Bun 1.1+
Installation
bun installAvailable scripts
bun run generate: generates types from schema + GraphQL operations.bun run build: regenerates types and builds outputs indist/(CJS + ESM + d.ts).bun run check: runs project type-check.bun run release:patch: bumps patch version, publishes to npm, pushes commit and tags.bun run release:minor: bumps minor version, publishes to npm, pushes commit and tags.bun run release:major: bumps major version, publishes to npm, pushes commit and tags.bun run release:beta: bumps beta prerelease version, publishes withbetatag, pushes commit and tags.bun run publish:public: publishes to npmjs as public package.
Project structure
schemas.graphqls+types/**/*.graphqls: source GraphQL schema.src/operations/*.graphql: library GraphQL operations.src/generated/graphql.ts: generated types and documents (auto-generated).src/client.ts: client implementation.src/index.ts: public exports.
Usage in your application
import { ProductGraphqlClient } from "@digital-retex/flos-pim-graphql-client";
const client = new ProductGraphqlClient(
"https://your-api.example.com/graphql",
{
headers: {
Authorization: `Bearer ${process.env.API_TOKEN}`,
},
},
);
const product = await client.getProduct("ABC123");
const page = await client.searchProducts({
page: 0,
size: 20,
onlyWeb: true,
});Typed custom requests
You can also use the request(options) method with a generated document:
import {
ProductGraphqlClient,
SearchProductsDocument,
type SearchProductsQuery,
type SearchProductsQueryVariables,
} from "@digital-retex/flos-pim-graphql-client";
const client = new ProductGraphqlClient("https://your-api.example.com/graphql");
const data = await client.request<
SearchProductsQuery,
SearchProductsQueryVariables
>({
document: SearchProductsDocument,
variables: {
filter: {
page: 0,
size: 10,
},
},
});How to add new queries
- Create a
.graphqlfile insrc/operations/. - Run
bun run generate. - Import the new
...Documentand related types from the package. - (Optional) Add a dedicated method in
src/client.ts.
Testing with Postman
- Import
product-graphql.postman_collection.jsoninto Postman. - Set collection variables:
baseUrl(GraphQL endpoint)authToken(if required)productCode,page,size
- Run
Get ProductorSearch Products.
Release and versioning
1) Prepare release
Make sure your working tree is clean and you are authenticated on npm:
npm whoamiIf needed:
npm loginAlso make sure git remote is configured and you have push permissions.
2) Run one release command
Choose one of:
bun run release:patch
bun run release:minor
bun run release:majorFor beta builds:
bun run release:betaEach release command performs all steps automatically:
- runs
npm version ...(updatespackage.json+ creates git tag) - publishes to npm (
publicfor stable,--tag betafor beta) - pushes commit and tags to remote (
git push && git push --tags)
prepublishOnly runs automatically before every publish and executes:
bun run buildbun run check
Operational notes
src/generated/graphql.tsis auto-generated: do not edit it manually.- If you update schema or queries, always regenerate with
bun run generate.
