-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serialization Error: Dates should still be of type Date after useQuery #695
Comments
Yes you're right. This file and line is where we turn the api call into a json object on the client. We need to convert it back to a date there. |
Hey, correct me if I got that wrong, but I do not think that it is blitz responsibility to do that. Thinking about querying data from any API you always get the date fields as strings. Specially on rest, if you use Even though we know, in blitz, that they are date objects that knowledge sits in the server side. The types are not carried over the rest request, so when we get the response in the client we do not know the type of the field. I think we cannot convert to the right date object. Inferring the type based on the content would be tricky too 🤔 |
I do get the point. But I don't agree that we should not care about that. I am totally fine converting the dates manually. (Some documentation would be fine, but maybe on Prisma's side.) But we are providing generated code which is somehow wrong:
This variable Actually |
Yeah, I feel your pain. It's like out of the box blitz is providing a mismatch of types between the type definition that comes from Prisma and our generated code.
That was actually the way I thought about doing it. We would need to use some regex or similar on every field to identify and convert that. Is it possible to have nested fields as well? Thinking about converting the field to Date, does it eliminate some other operation that the user might want to do and would only be possible with strings? I cannot think of anything right now, but it would be nice to foresee if we were cutting some possibilities out. I think we could try doing that and if anyone complains (or anyone identifies it is leading to bugs) then we can undo, or revisit it! |
One thing I was first worried about was the fact that It's funny that the docs highlight the inconsistency, but do not offer an alternative 🤷 |
At first I was stumped, but aha, the solution!! We just need to do more fancy serialization than JSON.parse(JSON.stringify()) which is what we are currently doing. We could use a some third-party library, but I think we probably want to code our own. Proposal for Custom Serialization LibraryGoals
APIconst input = {
normal: 'string',
timestamp: new Date(),
test: /blitz/,
set: new Set([1, 2]),
map: new Map([['hello', 'world']])
}
const {json, meta} = serialize(input: any)
/*
json = {
normal: 'string',
timestamp: "2020-06-20T04:56:50.293Z",
test: "/blitz/",
set: [1, 2],
map: {
hello: 'world'
}
}
// NOTE: `normal` not included here. `meta` only has special cases
meta = {
timestamp: 'Date',
test: 'Regexp',
set: 'Set',
map: 'Map'
}
*/ const output = deserialize(json, meta)
output <deep equals> input In addition to {
"result": {
"normal": "string",
"timestamp": "2020-06-20T04:56:50.293Z",
"test": "/blitz/",
"set": [1, 2],
"map": {
"hello": "world"
}
},
"error": null,
"meta": {
"specialTypes": {
"timestamp": "Date",
"test": "Regexp",
"set": "Set",
"map": "Map"
}
}
} I think it makes sense to write this as a standalone serialization library that anyone can use outside of Blitz too. |
Thanks, @peaonunes and @flybayer for investigation! |
I’ve opened a PR to create the serializer package. Anyone is welcome to finish this issue by using it in the core package once blitz-js/blitz#707 is merged. |
Liked a lot the suggestion @flybayer! |
What is the problem?
Prisma returns DateTime fields as Date object in our querys/mutations. Calling these querys/mutations requires to transform the dates to string, as objects cannot be transferred. (#469) This gets done automatically, but I don't know where.
Take this simplified model from Prisma:
In our pages/components we now recieve a model, for example:
project
now is of typeProject
, generated from Prisma. According to this type all DateTime fields (createdAt, updatedAt, specialDate) are provided as Date objects, but actually they are strings.Steps to Reproduce
Create fresh project
blitz new foo; cd foo
Generate model
blitz generate all foo name:string
Create a foo instance (via Prisma Studio / generated pages)
Check type of
fooDate
within /app/foos/querys/getFoo.ts:fooDate
within /app/foos/pages/foos/[fooId].tsx:Versions
Suggested solution
To actually use the dates as
Date
objects, they manually need to be transformed to Date, e.g.foo.createdAt = new Date(foo.createdAt)
. I thinkuseQuery
should do this for us.The text was updated successfully, but these errors were encountered: