pothos icon indicating copy to clipboard operation
pothos copied to clipboard

Add requiredTypename example demonstrating custom plugin for type-safe __typename handling

Open Copilot opened this issue 3 months ago • 2 comments

Adds an example demonstrating a custom plugin that adds a requiredTypename() method to interface and union refs, enforcing __typename: string as a required property through TypeScript's type system.

Implementation

Plugin (examples/required-typename/src/required-typename-plugin.ts):

  • Extends InterfaceRef, ImplementableInterfaceRef, and UnionRef prototypes with requiredTypename() method
  • Returns same ref with updated types requiring __typename: string
  • No plugin class or registration—purely additive types and methods

Example Schema:

  • Union type (Pet) and interface type (Node) using requiredTypename()
  • Omits resolveType/isTypeOf since GraphQL resolves via __typename field directly
  • Method chained immediately at definition
// Plugin adds method to refs
const PetUnion = builder.unionType('Pet', {
  types: [DogType, CatType],
}).requiredTypename();

// TypeScript now requires __typename in resolvers
builder.queryType({
  fields: (t) => ({
    pets: t.field({
      type: [PetUnion],
      resolve: () => [
        { __typename: 'Dog', name: 'Fido', breed: 'Golden Retriever' },
        { __typename: 'Cat', name: 'Whiskers', livesRemaining: 9 },
      ],
    }),
  }),
});

Testing: Includes schema tests validating union/interface resolution with __typename.

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkpoint.prisma.io
    • Triggering command: /usr/local/bin/node /usr/local/bin/node /home/REDACTED/work/pothos/pothos/node_modules/.pnpm/[email protected][email protected]/node_modules/prisma/build/child {"product":"prisma","version":"6.15.0","cli_install_type":"local","information":"","local_timestamp":"2025-11-25T21:58:56Z","project_hash":"344f9b9a","cli_path":"/home/REDACTED/work/pothos/pothos/packages/plugin-prisma/node_modules/prisma/build/index.js","c (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Demo: requiredTypename</issue_title> <issue_description>Create an example in the examples directory that adds a custom plugin that adds a requiredTypename() method on interface and union refs.

The idea is to just return the same ref, but update the types so that __typename: string is a required property for any fields returning that union or interface The example should have the full plugin with types and the method implementation attached to class proptotypes on a single plugin file and an example schema that can be used to ensure it type checks and works with a simple test.

</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes hayes/pothos#1556

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Nov 25 '25 21:11 Copilot

⚠️ No Changeset found

Latest commit: 4fb633733946010b160481c51697acc654c78ba6

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Nov 25 '25 21:11 changeset-bot[bot]

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
pothos Ready Ready Preview Comment Nov 25, 2025 10:10pm

vercel[bot] avatar Nov 25 '25 21:11 vercel[bot]