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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pothos/plugin-add-graphql

v4.2.1

Published

A Pothos plugin for adding existing GraphQL types to a Pothos schema

Downloads

7,325

Readme

Add-GraphQL for Pothos

This plugin makes it easy to integrate GraphQL types from existing schemas into your Pothos API

It can be used for incremental migrations from nexus, graphql-tools, or any other JS/TS executable schema.

Install

yarn add @pothos/plugin-add-graphql

Setup

import AddGraphQLPlugin from '@pothos/plugin-add-graphql';

const builder = new SchemaBuilder({
  plugins: [AddGraphQLPlugin],
});

Usage

There are 2 ways you can reference existing types.

  • Adding types (or a whole external schema) when setting up the builder
  • Adding types as Refs using new builder methods

Adding types when creating your builder

Adding types to the builder will automatically include the types in your schema when it's built. Types will only be added if no existing type of the same name is added to the builder before building the schema.

Adding types recursively adds any other types that the added type depends in it's fields, interfaces, or union members.

import { existingSchema } from './existing-schema-location';

const builder = new SchemaBuilder({
  plugins: [AddGraphQLPlugin],
  add: {
    // You can add individual types
    // This accepts Any GraphQLNamedType (Objects, Interface, Unions, Enums, Scalars, and InputObjects)
    types: [schema.getType('User'), schema.getType('Post')],
    // Or you can add an entire external schema
    schema: externalSchema,
  },
});

Adding types by themselves isn't very useful, so you'll probably want to be able to reference them when defining fields in your schema. To do this, you can add them to the builders generic Types.

This currently only works for Object, Interface, and Scalar types. For other types, use the builder methods below to create refs to the added types.

import { existingSchema } from './existing-schema-location';

const builder = new SchemaBuilder<{
  Objects: {
    User: UserType;
  };
  Interfaces: {
    ExampleInterface: { id: string };
  };
  Scalars: {
    DateTime: {
      Output: Date;
      Input: Date;
    };
  };
}>({
  plugins: [AddGraphQLPlugin],
  add: {
    types: [
      existingSchema.getType('User'),
      existingSchema.getType('ExampleInterface'),
      existingSchema.getType('DateTime'),
    ],
  },
});

builder.queryFields((t) => ({
  user: t.field({ type: 'User', resolve: () => getUser() }),
  exampleInterface: t.field({ type: 'ExampleInterface', resolve: () => getThings() }),
  now: t.field({ type: 'DateTime', resolve: () => new Date() }),
}));

Adding types using builder methods

Objects

// Passing in a generic type is recommended to ensure type-safety
const UserRef = builder.addGraphQLObject<UserType>(
  existingSchema.getType('User') as GraphQLObjectType,
  {
    // Optionally you can override the types name
    name: 'AddedUser',
    // You can also pass in any other options you can define for normal object types
    description: 'This type represents Users',
  },
);

const PostRef = builder.addGraphQLObject<{
  id: string;
  title: string;
  content: string;
}>(existingSchema.getType('Post') as GraphQLObjectType, {
  fields: (t) => ({
    // remove existing title field from type
    title: null,
    // add new titleField
    postTitle: t.exposeString('title'),
  }),
});

You can then use the returned references when defining fields:

builder.queryFields((t) => ({
  posts: t.field({
    type: [PostRef],
    resolve: () => loadPosts(),
  }),
}));

Interfaces

const NodeRef = builder.addGraphQLInterface<NodeShape>(
  existingSchema.getType('Node') as GraphQLInterfaceType,
  {
    // interface options
  },
);

Unions

const SearchResult = builder.addGraphQLUnion<User | Post>(
  existingSchema.getType('SearchResult') as GraphQLUnionType,
  {
    // union options
  },
);

Enums

const OrderBy = builder.addGraphQLEnum<'Asc' | 'Desc'>(
  existingSchema.getType('OrderBy') as GraphQLEnumType,
  {
    // enum options
  },
);

Input objects

const PostFilter = builder.addGraphQLInput<{ title?: string, tags? string[] }>(
  existingSchema.getType('PostFilter') as GraphQLInputObjectType,
  {
    // input options
  },
);

Scalars

This plugin does not add a new method for scalars, because Pothos already has a method for adding existing scalar types.

builder.addScalarType('DateTime', existingSchema.getType('DateTime') as GraphQLScalar, {
  // scalar options
});