-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Null instead of Undefined #36
Comments
Can you show me your use case? where are you using |
@remorses You can check it here: import traverse from "traverse";
import { createClient } from "./generated";
function nullToUndefined<D extends any>(data: D) {
return traverse(data).map(function (value) {
if (value === null) this.update(undefined);
}) as D;
}
const client = createClient();
(async () => {
const data = await client.query({
users: {
email: true,
image: true,
},
});
const { users } = nullToUndefined(data);
const { image } = users[0]; // const image: string | undefined
})(); |
I used undefined to simplify the typescript types, tell me if you find any problem where null should be used instead |
Hey @remorses, I have a use case where I have the entity customer from Shopify that is primary parsed via webhooks, where I have some fields as I also have to retrieve some data from orders ecc, but the two are incompatible since the types from JSON rest are Until now, I simply use the await processAutomations.queue.add(
"process automations of type ORDER_CONFIRMATION",
{
customer: {
admin_graphql_api_id: customer.id,
created_at: customer.createdAt,
email: customer.email ?? null,
first_name: customer.firstName ?? null,
last_name: customer.lastName ?? null,
phone: customer.phone ?? null,
tags: customer.tags,
updated_at: customer.updatedAt,
sms_marketing_consent:
smsMarketingConsent == null
? null
: {
state: smsMarketingConsent.marketingState,
consent_updated_at:
smsMarketingConsent.consentUpdatedAt ?? null,
consent_collected_from:
smsMarketingConsent.consentCollectedFrom ?? null,
},
default_address: {
country,
country_code: countryCodeV2,
},
},
type: "ORDER_CONFIRMATION",
shopifyDomain,
},
); I understand that Apart my example, take the example where I should save the data from the genql client into a database, maybe with an orm like prisma. Prisma use |
Why is this closed? There is a mismatch between the types and what graphql returns. |
i will add |
Thanks @remorses! |
hey @remorses, we've ran into the same problem for mutations where we base the mutation inputs on the return of a graphql query (where null sets it as null and undefined doesn't change the field at all). is there an eta on this behavior, would you like some help with it? |
Released in |
@remorses this is awesome! thank you so much this is a huge win for our use case. however i think there's a small bug and response fields are still incorrectly typed as undefinable with |
If you want to remove the undefined values from a type simply use I don't want to remove undefined from the response fields, undefined makes reusing types much easier, for example let repository: Repository = {
name: 'x'
} instead of let repository: Repository = {
name: 'x',
anotherField: null,
someOtherField: null,
// ...
} |
hm great suggestions with can you elaborate on the problem with removing undefined from response fields? i don't see the issue with the latter if that's the return of an endpoint. |
@ayoung19 can you show me an example where having |
@remorses as an example you can take the one I posted above. |
@remorses well the core problem is the type returned by graphql is null, not undefined. we've actually ran into bugs where we tried to destruct and assign a default value to an incoming object from our backend which typescript would incorrectly let us do due to the type annotations, something like this:
beyond that, we also have form state code that looks like what @nullndr posted, we have to add |
I ended up removing the undefined types in version 5 |
This is the
![image](https://user-images.githubusercontent.com/1142799/95305121-c3ab1400-08af-11eb-8329-dce70566cdcb.png)
image
field in Shema.This is the
image
field which is generated bygenql
. It must benull
, notundefined
. Do you have an option to config it?See also: graphql/graphql-js#1298 (comment)
tsconfig.json
Now I have to use this function to convert
null
toundefined
:The text was updated successfully, but these errors were encountered: