npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@pallad/query-graphql

v8.0.1

Published

GraphQL types for @pallad/query

Downloads

468

Readme

@pallad/query-graphql

@pallad/query-graphql creates GraphQL input and result types for descriptors from @pallad/query-descriptor.

Use it when GraphQL should expose the same filter, pagination, and sorting rules that your query descriptor validates at runtime.

Installation

yarn add @pallad/query-graphql @pallad/query-descriptor graphql

graphql is a peer dependency.

GraphQLQueryBuilder

GraphQLQueryBuilder is the main entry point. It creates:

  • ${baseName}_Query input type for filter, pagination, and sorting.
  • ${baseName}_Result object type for list/connection results.
  • Resolver wrapper that parses GraphQL args through the descriptor before calling your executor.
import { QueryDescriptor } from "@pallad/query-descriptor";
import { GraphQLQueryBuilder } from "@pallad/query-graphql";
import { GraphQLInputObjectType, GraphQLNonNull, GraphQLObjectType, GraphQLString } from "graphql";
import { z } from "zod";

const descriptor = new QueryDescriptor()
	.filterSchema(
		z.object({
			status: z.enum(["active", "archived"]).optional(),
		})
	)
	.paginationByOffset({ defaultLimit: 20 })
	.sortingByMultipleFields({
		sortableFields: ["name", "createdAt"],
		defaultSorting: [{ field: "createdAt", direction: "DESC" }],
	});

const userFilterType = new GraphQLInputObjectType({
	name: "User_Filter",
	fields: {
		status: { type: GraphQLString },
	},
});

const userType = new GraphQLObjectType({
	name: "User",
	fields: {
		id: { type: new GraphQLNonNull(GraphQLString) },
		name: { type: new GraphQLNonNull(GraphQLString) },
		createdAt: { type: new GraphQLNonNull(GraphQLString) },
	},
});

const usersQuery = new GraphQLQueryBuilder({
	baseName: "Users",
	descriptor,
	filterType: userFilterType,
	entityType: userType,
});

Adding Field To Schema

getField() returns a GraphQLFieldConfig. Its execute callback receives a parsed query, source, context, and GraphQL resolve info.

import { GraphQLObjectType, GraphQLSchema } from "graphql";

const schema = new GraphQLSchema({
	query: new GraphQLObjectType({
		name: "Query",
		fields: {
			users: usersQuery.getField({
				execute: async query => {
					const list = await loadUsers(query);

					return {
						list,
						pagination: {
							hasNextPage: false,
							hasPreviousPage: false,
						},
					};
				},
			}),
		},
	}),
});

execute returns raw entities and pagination context. GraphQLQueryBuilder calls descriptor.createResult(query, list, pagination) and returns the GraphQL-ready result.

Offset Pagination Example

Descriptor:

const descriptor = new QueryDescriptor().paginationByOffset();

Generated result type:

type Users_Result {
  list: [User!]!
  pageInfo: PageInfo_ByOffset!
}

Query:

query {
  users(query: {limit: 20, offset: 40}) {
    list {
      id
      name
    }
    pageInfo {
      limit
      offset
      hasNextPage
      hasPreviousPage
    }
  }
}

Cursor Pagination Example

Descriptor:

const descriptor = new QueryDescriptor()
	.paginationByCursor()
	.sortingBySingleField({
		fields: ["name", "createdAt"],
		default: { field: "createdAt", direction: "DESC" },
	});

Generated result type:

type Users_Result {
  edges: [Users_Edge!]!
  nodes: [User!]!
  pageInfo: PageInfo_ByCursor!
  sortBy: Users_Result_Sort!
}

type Users_Edge {
  node: User!
  cursor: Cursor!
}

Query:

query {
  users(query: {limit: 20, sortBy: {field: createdAt, direction: DESC}}) {
    edges {
      node {
        id
        name
      }
      cursor
    }
    nodes {
      id
    }
    pageInfo {
      limit
      startCursor
      endCursor
      hasNextPage
      hasPreviousPage
    }
    sortBy {
      field
      direction
    }
  }
}

Cursor pagination returns both edges and nodes. Cursors are built by @pallad/query-descriptor from entity ids by default.

Sorting

Sorting fields become a GraphQL enum named ${baseName}_Sort_Field. Field names are camel-cased for GraphQL, while enum values keep original descriptor field names.

const descriptor = new QueryDescriptor().sortingByMultipleFields({
	sortableFields: ["created_at", "name"],
	defaultSorting: [{ field: "created_at", direction: "DESC" }],
});
enum Users_Sort_Field {
  createdAt
  name
}

Single sorting uses one input object:

query {
  users(query: {sortBy: {field: name, direction: ASC}}) {
    list { id }
  }
}

Multiple sorting uses a list:

query {
  users(query: {sortBy: [{field: createdAt, direction: DESC}, {field: name, direction: ASC}]}) {
    list { id }
  }
}

Low-Level Helpers

The package also exports helpers used by GraphQLQueryBuilder:

  • createQueryType() - creates ${baseName}_Query input type.
  • createSortFieldType() - creates ${baseName}_Sort_Field enum.
  • createInputSortType() - creates GraphQL sort input object or list.
  • createResultSortType() - creates GraphQL sort result object or list.
  • getQueryFieldsForPaginationByOffset() - creates limit and offset input fields.
  • getQueryFieldsForPaginationByCursor() - creates after, before, and limit input fields.
  • GraphQLPageInfoPaginationByOffset - shared offset pageInfo type.
  • GraphQLPageInfoPaginationByCursor - shared cursor pageInfo type.

Use low-level helpers when you need custom schema composition. For common query fields, prefer GraphQLQueryBuilder.