Skip to Content
SDK (Deep Dive)GraphQL Plugin

GraphQL Plugin (@glubean/graphql)

@glubean/graphql is a first-party GraphQL client plugin for Glubean.

Status: Experimental (pre-1.0). API may evolve.

Install

import { graphql } from "jsr:@glubean/graphql";

Or in deno.json imports:

{ "imports": { "@glubean/graphql": "jsr:@glubean/graphql" } }

Quick Start (configure plugin mode)

import { configure, test } from "@glubean/sdk"; import { graphql } from "@glubean/graphql"; const { gql } = configure({ plugins: { gql: graphql({ endpoint: "{{graphql_url}}", headers: { Authorization: "Bearer {{api_key}}" }, throwOnGraphQLErrors: true, }), }, }); export const getUser = test("get-user", async (ctx) => { const { data } = await gql.query<{ user: { name: string } }>( `query GetUser($id: ID!) { user(id: $id) { name } }`, { variables: { id: "1" } }, ); ctx.expect(data?.user.name).toBe("Alice"); });

Standalone mode (createGraphQLClient)

import { test } from "@glubean/sdk"; import { createGraphQLClient } from "@glubean/graphql"; export const quick = test("quick-gql", async (ctx) => { const gql = createGraphQLClient(ctx.http, { endpoint: ctx.vars.require("GQL_URL"), headers: { Authorization: `Bearer ${ctx.secrets.require("TOKEN")}` }, }); const { data } = await gql.query<{ health: string }>(`{ health }`); ctx.assert(data?.health === "ok", "Service healthy"); });

Extra helpers

gql tagged template

Identity tag for better GraphQL editor highlighting.

import { gql } from "@glubean/graphql"; const GET_USER = gql` query GetUser($id: ID!) { user(id: $id) { id name email } } `;

fromGql(path)

Load query text from .gql files.

import { fromGql } from "@glubean/graphql"; const GET_USER = await fromGql("./queries/get-user.gql");

Client methods

  • gql.query<T>(query, options?)
  • gql.mutate<T>(mutation, options?)

Both return Promise<{ data: T | null; errors?: GraphQLError[] }>.

Options

  • endpoint (string, required)
  • headers (Record<string, string>, optional)
  • throwOnGraphQLErrors (boolean, default false)

See package docs: @glubean/graphql on JSR 

Last updated on