@manta-eu/client
v0.25.0
Published
The Manta GraphQL contract and the typed React Query hooks generated from it
Readme
@manta-eu/client
The Manta GraphQL contract and the typed React Query hooks generated from it.
Documentation: developers.manta.eu
Installation
pnpm add @manta-eu/client@tanstack/react-query, react and react-router are peer dependencies — the generated hooks are
React Query hooks, so your app supplies the versions it already runs.
Usage
Build a client against your deployment, then call the generated hooks:
import { mantaGraphQLClient } from "@manta-eu/client";
const client = mantaGraphQLClient(new URL("https://app.hiremanta.com"));Pass the origin — mantaGraphQLClient appends the GraphQL path itself.
Requests carry the session from that origin, so the user signs in there.
An app on any other origin passes the person's access token instead, and the client then sends no cookies:
const client = mantaGraphQLClient(new URL("https://app.hiremanta.com"), undefined, {
Authorization: `Bearer ${accessToken}`,
});Access tokens last five minutes, so a long-lived client takes a token source instead of a token.
getAccessToken is called before every request. On a 401 the client calls its renew(), when it
has one, asks again, and retries once; onUnauthorized fires only if that retry is refused too.
cachedToken from @manta-eu/auth/web is such a source:
import { cachedToken } from "@manta-eu/auth/web";
const getAccessToken = cachedToken(() => myServer.freshAccessToken());
const client = mantaGraphQLClient(new URL("https://app.hiremanta.com"), onSignedOut, undefined, { getAccessToken });Exports
| Export | Contents |
| ---------------- | ---------------------------------------------------------------------------------------------- |
| . | The generated GraphQL types and hooks, plus the client constructors |
| ./shared/* | Contract pieces shared with the server — page context, models, datastore paths, RDF namespaces |
| ./types/* | Standalone type modules |
| ./shared/tools | Tool definitions the assistant is given |
| ./testing | An in-memory Manta for testing your app's SPARQL reads and writes |
Most consumers reach this package through @manta-eu/react,
which mounts the copilot and its providers for you. Use it directly when you want the contract
without the UI.
Testing your app
createMantaMock from @manta-eu/client/testing answers /sparql and /sparql-update from an
in-memory store, so tests run your app's real LDKit queries without a Manta stack. It adds the
prefixes Manta declares, to the seed Turtle too, and refuses the writes Manta refuses: anything but INSERT DATA, DELETE DATA
and DELETE/INSERT with a scoped WHERE, and writes that name a graph.
It runs on Comunica and N3, which only tests need, so add them as dev dependencies:
pnpm add -D @comunica/query-sparql-rdfjs-lite n3@^1.26.0Pass its fetch wherever your code takes one, with absolute URLs. As on Manta, /sparql reads, and writes
an update sent as application/sparql-update, so one lens does both. It also answers the deprecated
/sparql-update:
import { createMantaMock } from "@manta-eu/client/testing";
import { createLens } from "ldkit";
const manta = createMantaMock({ turtle: "<https://example.com/todo/1> a <https://example.com/Todo> ." });
const todos = createLens(TodoSchema, { sources: ["https://manta.test/d/acme/sparql"], fetch: manta.fetch });In Playwright, route the page's SPARQL requests to it:
import { createMantaMock, mantaMockRoutePattern } from "@manta-eu/client/testing";
test.beforeEach(async ({ page }) => {
const manta = createMantaMock();
await page.route(mantaMockRoutePattern, manta.route);
});mantaMockRoutePattern matches /sparql and /sparql-update on any origin. Pass a narrower pattern, such
as https://manta.test/**, when the page also queries other SPARQL endpoints. Requests the mock
does not serve fall through to the network. Like Manta, it answers a cross-origin page with CORS headers for that
origin, and answers the preflight of a write itself.
manta.updates lists the updates it applied, in order. Every datastore path shares one store.
License
MIT
