Skip to content

Fix drizzleField to prevent unnecessary ModelLoader queries in mutations - #1583

Draft
hayes with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-mutation-field-mapping
Draft

Fix drizzleField to prevent unnecessary ModelLoader queries in mutations#1583
hayes with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-mutation-field-mapping

Conversation

Copilot AI commented Dec 21, 2025

Copy link
Copy Markdown
Contributor

drizzleField within mutations caused unnecessary re-queries via ModelLoader even when resolvers returned complete data. This was particularly problematic for delete operations attempting to fetch already-deleted records.

Root Cause

queryFromInfo sets loader mappings that prevent ModelLoader from re-querying. It was only called when resolvers explicitly invoked the query parameter. Mutations using .returning() don't need the query function since they return data directly:

deleteCategory: t.drizzleField({
  type: ['categories'],
  resolve: async (query, _root, args) => {
    // query() never called - mappings never set
    return db.delete(categories)
      .where(eq(categories.name, args.name))
      .returning();
  },
});

This resulted in:

  1. DELETE with RETURNING executed
  2. ModelLoader re-queries for the deleted record (fails)

Changes

Modified drizzleField and drizzleFieldWithInput to:

  • Track if query function is called via flag
  • Await resolver completion to handle async properly
  • Call queryFromInfo after resolver if query function was not invoked

This ensures mappings are set regardless of whether the resolver uses the query function, while avoiding duplicate calls when it does.

Test Coverage

Added mutation-field-mapping.test.ts validating:

  • Delete mutations: 1 query instead of 2
  • Create mutations: 1 query instead of 2
  • Existing query-based mutations: unchanged behavior

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@7.1.0_@types+react@19.2.7_better-sqlite3@12.5.0_react-dom@19.2.3_react@19.2.3__react@19.2.3_typescript@5.9.3/node_modules/prisma/build/child {"product":"prisma","version":"7.1.0","cli_install_type":"local","information":"","local_timestamp":"2025-12-21T09:26:13Z","project_hash":"344f9b9a","cli_path":"/home/REDACTED/work/pothos/pothos/packages/plugin-prisma/node_modules/prisma/build/index.js","cl sh .1/b�� swc src -d lib --config-file ../../.swcrc -C module.type=commonjs --strip-leading-paths node federation/node_modules/.bin/node s --strip-leadinnode node n/node node .1/b�� son node -r @swc-node/register ../../scripts/esm--config-file -d tools/pnpm/10.15.1_tmp_3499/node_modules/pnpm/dist/node-gyp-bin/node --config-file ../../.swcrc -C node (dns block)
    • Triggering command: /usr/local/bin/node /usr/local/bin/node /home/REDACTED/work/pothos/pothos/node_modules/.pnpm/prisma@7.1.0_@types+react@19.2.7_better-sqlite3@12.5.0_react-dom@19.2.3_react@19.2.3__react@19.2.3_typescript@5.9.3/node_modules/prisma/build/child {"product":"prisma","version":"7.1.0","cli_install_type":"local","information":"","local_timestamp":"2025-12-21T09:26:15Z","project_hash":"33bcc4ae","cli_path":"/home/REDACTED/work/pothos/pothos/packages/plugin-prisma/node_modules/prisma/build/index.js","cl node k/po�� -node/register ../../scripts/esm-transformer.ts sh _modules/pnpm/dist/node-gyp-bin/node s --strip-leadinsh sh node node .1/b�� && swc src -d esm --config-file ../../.swcrc -C module.type=es6--config-file node errors/node_modules/.bin/sh sm:extensions sh node sh (dns block)
    • Triggering command: /usr/local/bin/node /usr/local/bin/node /home/REDACTED/work/pothos/pothos/node_modules/.pnpm/prisma@7.1.0_@types+react@19.2.7_better-sqlite3@12.5.0_react-dom@19.2.3_react@19.2.3__react@19.2.3_typescript@5.9.3/node_modules/prisma/build/child {"product":"prisma","version":"7.1.0","cli_install_type":"local","information":"","local_timestamp":"2025-12-21T09:26:25Z","project_hash":"5d274ca5","cli_path":"/home/REDACTED/work/pothos/pothos/packages/plugin-with-input/node_modules/prisma/build/index.js" sh .1/b�� son node -r @swc-node/register ../../scripts/esm-transformer.ts sh _modules/pnpm/dist/node-gyp-bin/node sm:extensions node node node .1/b�� build:esm node les/.bin/sh ../.swcrc -C modsh build /home/REDACTED/.coTS_NODE_PROJECT=../../tsconfig.json node -r @swc-node/register ../../scripts/esm-transformer.ts sh (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>[@pothos/plugin-drizzle] Failed to map the Mutation field, causing an unnecessary ModelLoader to run.</issue_title>
<issue_description>The drizzleField within Mutation returns an empty object for state.mappings in stateFromInfo.
Therefore, even if sufficient values are returned during resolution when executing the Mutation, it always performs a re-query.
As a result, the delete operation attempts to retrieve a non-existent record, preventing the return of valid values.

  • Verification Repository (plugin-test Brunch)

https://github.com/SoraKumo001/pothos-drizzle-generator-sample/tree/plugin-test

  • code

https://github.com/SoraKumo001/pothos-drizzle-generator-sample/blob/4cfc3749d20c8e968d13bbb7da835bbdd239ba07/src/pothos-drizzle-generator/PothosDrizzleGenerator.ts#L555-L603

  • GraphQL
mutation Test {
  createOneCategory(input: { name: "test" }) {
    id
  }
  deleteCategory(where: { name: { eq: "test" } }) {
    id
  }
}
  • SQL
===========================
 insert into
  "Category" ("id", "name", "createdAt", "updatedAt")
values
  (default, $1, default, default)
returning
  "id",
  "name",
  "createdAt",
  "updatedAt"
--
 [ 'test' ]
===========================
 select
  "d0"."id" as "id"
from
  "Category" as "d0"
where
  "d0"."id" in ($1)
--
 [ 'bf26c514-65de-4950-bb75-c996bdcde0b2' ]
set Map(0) {}
===========================
 delete from "Category"
where
  "Category"."name" = $1
returning
  "id",
  "name",
  "createdAt",
  "updatedAt"
--
 [ 'test' ]
===========================
 select
  "d0"."id" as "id"
from
  "Category" as "d0"
where
  "d0"."id" in ($1)
--
 [ 'bf26c514-65de-4950-bb75-c996bdcde0b2' ]
  • Error message
{
  "errors": [
    {
      "message": "Model categories(bf26c514-65de-4950-bb75-c996bdcde0b2) not found",
      "locations": [
        {
          "line": 6,
          "column": 5
        }
      ],
      "path": [
        "deleteCategory",
        0,
        "id"
      ]
    }
  ]
}

</issue_description>

<agent_instructions>Create a simple failing test, then implement a fix</agent_instructions>

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@changeset-bot

changeset-bot Bot commented Dec 21, 2025

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 69ad2ff

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

vercel Bot commented Dec 21, 2025

Copy link
Copy Markdown

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

Project Deployment Review Updated (UTC)
pothos Ready Ready Preview, Comment Dec 21, 2025 9:43am

Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
…ield

Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
Co-authored-by: hayes <816527+hayes@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix mapping in Mutation field to prevent unnecessary re-query Fix drizzleField to prevent unnecessary ModelLoader queries in mutations Dec 21, 2025
Copilot AI requested a review from hayes December 21, 2025 09:45
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.

[@pothos/plugin-drizzle] Failed to map the Mutation field, causing an unnecessary ModelLoader to run.

2 participants