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

@leaven-graphql/schema

v0.1.0

Published

Schema building utilities for Leaven GraphQL

Downloads

145

Readme

@leaven-graphql/schema

Schema building and merging utilities for Leaven.

Installation

bun add @leaven-graphql/schema graphql

Quick Start

import { SchemaBuilder, createSchemaBuilder } from '@leaven-graphql/schema';

const builder = new SchemaBuilder();

// Define types
builder.addType('User', {
  id: 'ID!',
  name: 'String!',
  email: 'String!',
});

// Define queries
builder.addQuery('user', {
  type: 'User',
  args: { id: 'ID!' },
});

// Build the schema
const schema = builder.build();

Features

Schema Builder

Fluent API for building schemas programmatically:

import { SchemaBuilder } from '@leaven-graphql/schema';

const builder = new SchemaBuilder();

// Add object types
builder.addType('User', {
  id: 'ID!',
  name: 'String!',
  email: 'String!',
  posts: '[Post!]!',
});

builder.addType('Post', {
  id: 'ID!',
  title: 'String!',
  content: 'String',
  author: 'User!',
});

// Add queries
builder.addQuery('user', {
  type: 'User',
  args: { id: 'ID!' },
});

builder.addQuery('users', {
  type: '[User!]!',
});

// Add mutations
builder.addMutation('createUser', {
  type: 'User!',
  args: {
    name: 'String!',
    email: 'String!',
  },
});

const schema = builder.build();

Schema Merging

Combine multiple schemas:

import { mergeSchemas, mergeSchemasFromStrings } from '@leaven-graphql/schema';

// Merge existing schemas
const merged = mergeSchemas([
  usersSchema,
  postsSchema,
  commentsSchema,
]);

// Merge from SDL strings
const schema = mergeSchemasFromStrings([
  `
    type Query {
      users: [User!]!
    }
    type User {
      id: ID!
      name: String!
    }
  `,
  `
    extend type Query {
      posts: [Post!]!
    }
    type Post {
      id: ID!
      title: String!
    }
  `,
]);

Resolvers

Create and merge type-safe resolvers:

import { createResolvers, mergeResolvers } from '@leaven-graphql/schema';

const userResolvers = createResolvers({
  Query: {
    user: async (_, { id }, context) => {
      return context.db.users.findById(id);
    },
    users: async (_, __, context) => {
      return context.db.users.findAll();
    },
  },
  User: {
    posts: async (user, _, context) => {
      return context.db.posts.findByAuthor(user.id);
    },
  },
});

const postResolvers = createResolvers({
  Query: {
    posts: async (_, __, context) => {
      return context.db.posts.findAll();
    },
  },
  Post: {
    author: async (post, _, context) => {
      return context.db.users.findById(post.authorId);
    },
  },
});

// Merge resolvers
const resolvers = mergeResolvers([userResolvers, postResolvers]);

File Loaders

Load schemas from .graphql files:

import {
  loadSchemaFromFile,
  loadSchemaFromDirectory,
  loadSchemaFromGlob
} from '@leaven-graphql/schema';

// Load a single file
const schema1 = await loadSchemaFromFile('./schema.graphql');

// Load all files in a directory
const schema2 = await loadSchemaFromDirectory('./schemas');

// Load files matching a glob pattern
const schema3 = await loadSchemaFromGlob('./modules/**/*.graphql');

Custom Directives

Create and apply custom directives:

import { createDirective, applyDirectives } from '@leaven-graphql/schema';

// Create an @auth directive
const authDirective = createDirective({
  name: 'auth',
  locations: ['FIELD_DEFINITION'],
  args: {
    requires: 'Role = USER',
  },
  transform: (schema, directiveArgs) => {
    return wrapFieldWithAuth(schema, directiveArgs.requires);
  },
});

// Apply directives to schema
const schema = applyDirectives(baseSchema, [authDirective]);

API Reference

SchemaBuilder

class SchemaBuilder {
  addType(name: string, fields: Record<string, string | FieldDefinition>): this;
  addInput(name: string, fields: Record<string, string>): this;
  addEnum(name: string, values: string[]): this;
  addInterface(name: string, fields: Record<string, string | FieldDefinition>): this;
  addUnion(name: string, types: string[]): this;
  addQuery(name: string, definition: FieldDefinition): this;
  addMutation(name: string, definition: FieldDefinition): this;
  addSubscription(name: string, definition: FieldDefinition): this;
  build(): GraphQLSchema;
}

Schema Merging

function mergeSchemas(schemas: GraphQLSchema[], options?: MergeOptions): GraphQLSchema;
function mergeSchemasFromStrings(sdls: string[], options?: MergeOptions): GraphQLSchema;

interface MergeOptions {
  onTypeConflict?: 'error' | 'first' | 'last' | 'merge';
  onFieldConflict?: 'error' | 'first' | 'last';
}

File Loaders

function loadSchemaFromFile(path: string, options?: LoaderOptions): Promise<GraphQLSchema>;
function loadSchemaFromDirectory(path: string, options?: LoaderOptions): Promise<GraphQLSchema>;
function loadSchemaFromGlob(pattern: string, options?: LoaderOptions): Promise<GraphQLSchema>;

interface LoaderOptions {
  encoding?: BufferEncoding;
  resolvers?: Resolvers;
}

License

Apache 2.0 - Pegasus Heavy Industries LLC