Skip to content

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

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/add-required-typename-example
Draft

Add requiredTypename example demonstrating custom plugin for type-safe __typename handling#1557
Copilot wants to merge 5 commits intomainfrom
copilot/add-required-typename-example

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 25, 2025

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/prisma@6.15.0_typescript@5.9.2/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)


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

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Nov 25, 2025

⚠️ No Changeset found

Latest commit: 4fb6337

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

@vercel
Copy link
Copy Markdown

vercel Bot commented Nov 25, 2025

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

Copilot AI and others added 2 commits November 25, 2025 22:04
Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
… main

Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
Copilot AI changed the title [WIP] Create example for requiredTypename method in plugin Add requiredTypename example demonstrating custom plugin for type-safe __typename handling Nov 25, 2025
Copilot AI requested a review from hayes November 25, 2025 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Demo: requiredTypename

2 participants