From 6d8aac5fef141ddee5d1a0803c4ecfaa9b06856f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl?= Date: Thu, 7 Dec 2023 18:14:34 +0300 Subject: [PATCH] fix: typed result and similar (#509) ### Describe your change Fixes datetime return type by returning the `value` field instead of the whole object. ### Motivation and context Prisma has a `$type` tag for formatted string such as `DateTime`, this PR aims to add support for that. ### Migration notes ### Checklist - [ ] The change come with new or modified tests - [x] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change --- .pre-commit-config.yaml | 4 ++-- typegate/src/runtimes/prisma/prisma.ts | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1f718f44eb..c9e0bfb52a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,12 +17,12 @@ repos: # exclude all generated files exclude: (typegate/deno.lock|.*\.snap$|typegate/src/typegraphs/.*\.json|website/docs/reference/) - repo: https://github.com/python-jsonschema/check-jsonschema - rev: 0.27.1 + rev: 0.27.2 hooks: - id: check-dependabot - id: check-github-workflows - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: "v0.1.6" + rev: "v0.1.7" hooks: - id: ruff - id: ruff-format diff --git a/typegate/src/runtimes/prisma/prisma.ts b/typegate/src/runtimes/prisma/prisma.ts index 1436e85e18..3d934a1db0 100644 --- a/typegate/src/runtimes/prisma/prisma.ts +++ b/typegate/src/runtimes/prisma/prisma.ts @@ -269,10 +269,28 @@ export class PrismaRuntime extends Runtime { } else { const resolver: Resolver = ({ _: { parent } }) => { const resolver = parent[field.props.node]; - const ret = typeof resolver === "function" ? resolver() : resolver; + const rawValue = typeof resolver === "function" + ? resolver() + : resolver; const matData = stage.props.materializer ?.data as unknown as PrismaOperationMatData; - return matData.operation === "queryRaw" ? ret["prisma__value"] : ret; + + // if queryRaw is used, the result has a type tag + const ret = matData.operation === "queryRaw" + ? rawValue["prisma__value"] + : rawValue; + + // Prisma uses $type tag for formatted strings + // eg. `createAt: { "$type": "DateTime", value: "2023-12-05T14:10:21.840Z" }` + if ( + !Array.isArray(ret) && + typeof ret === "object" && + ret !== null && + "$type" in ret // ! + ) { + return ret?.["value"] ?? ret; + } + return ret; }; stagesMat.push( new ComputeStage({