Skip to content

Commit

Permalink
fix: typed result and similar (#509)
Browse files Browse the repository at this point in the history
<!--
Pull requests are squash merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog and understand when they need to update his code and
how.
-->

### 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

<!-- Explain HOW users should update their code when required -->

### 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
  • Loading branch information
michael-0acf4 authored Dec 7, 2023
1 parent 62b459f commit 6d8aac5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions typegate/src/runtimes/prisma/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 6d8aac5

Please sign in to comment.