genql: Null instead of Undefined

This is the image field in Shema. image

This is the image field which is generated by genql. It must be null, not undefined. Do you have an option to config it?

See also: https://github.com/graphql/graphql-js/issues/1298#issuecomment-377365916

image

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

Now I have to use this function to convert null to undefined:

import traverse from 'traverse';

function nullToUndefined<D extends any>(data: D) {
  return traverse(data).map(function (value) {
    if (value === null) this.update(undefined);
  }) as D;
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I ended up removing the undefined types in version 5

Released in 4.0.0

i will add null to the types soon

@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:

const { name = 'John Doe' } = person; // typed as { person: string | undefined }
name.split(' '); // runtime error, person.name is null even though it's typed as string | undefined

beyond that, we also have form state code that looks like what @nullndr posted, we have to add || null to all of the incoming fields so that they are properly typed as null, so on mutations we can send the object back. we can’t store it as undefined because undefined means opt out of changes in the mutation input