0.1.3 • Published 5 months ago

libgqlts v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

libgqlts

Type-safe GraphQL

Full example:

import { Query, z } from "libgqlts";

const userIdQuery = Query.typed(
  "https://graphql.anilist.co", // api endpoint
  "AnimeList", // name of the query
  { // arguments (required when calling execute)
    userId: z.number().int().named("Int"),
    type: z.enum(["ANIME", "MANGA"]).named("MediaType"),
  },
  { // query schema
    MediaListCollection: {
      // _args is a special key that allows to add arguments to this part of the query
      _args: { userId: "$userId", type: "$type" },
      lists: [
        {
          entries: [
            {
              media: {
                title: {
                  english: z.string().nullable(),
                  romaji: z.string().nullable(),
                  native: z.string().nullable(),
                },
                isFavourite: z.boolean(),
                mediaListEntry: {
                  score: z.number(),
                  status: z.enum([
                    "CURRENT",
                    "PLANNING",
                    "COMPLETED",
                    "DROPPED",
                    "PAUSED",
                    "REPEATING",
                  ]),
                },
                bannerImage: z.string().nullable(),
                coverImage: {
                  color: z.string().nullable(),
                  medium: z.string().nullable(),
                },
              },
            },
          ],
        },
      ],
    },
    Viewer: {
      mediaListOptions: {
        scoreFormat: z.enum([
          "POINT_100",
          "POINT_10_DECIMAL",
          "POINT_10",
          "POINT_5",
          "POINT_3",
        ]),
      },
    },
  },
);

// `viewer` is fully type safe
// GraphQL errors reject the Promise from `execute`
const viewer = await userIdQuery.execute(
  {
    userId: 5812973,
    type: "ANIME",
  },
  "Bearer ey...bU",
);

console.log(
  viewer.MediaListCollection.lists[0]?.entries[0]?.media.title.english,
);
0.1.3

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago