question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typescript-urql-graphcache plugin: function argument types always have optional fields

See original GitHub issue

Describe the bug All generated object types for graphcache handler function arguments have their field types converted to T | undefined. This means I have to perform a lot of redundant checks (which is infeasible for a large number of fields) or I must type-assert everywhere.

To Reproduce Steps to reproduce the behavior:

  1. Generate the GraphCacheConfig type using the typescript-urql-graphcache plugin
  2. Use this type parameter on your cacheExchange constructor
  3. Try to e.g. define an update function using a non-nullable object return value with non-nullable fields
  4. The object’s type annotation will declare all of its fields as optional.

The culprit is the WithTypename type definition, which makes all fields of the input type optional.

Reproduction with example of an issue this causes: https://codesandbox.io/s/amazing-field-zlkgp?file=/main.ts

image

Expected behavior Objects defined in the generated typedefs have the exact same field types as specified in the gql schema.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
charlypolycommented, Mar 30, 2022

Thank you for your contribution @AndysonDK!

The fix has been released in @graphql-codegen/typescript-urql@3.5.7 and @graphql-codegen/typescript-urql-graphcache@2.2.10 📦

1reaction
charlypolycommented, Mar 10, 2022

Hi @tobz1000,

Indeed, it seems that the WithTypename type definition is incorrect, it should keep the given T type as it, not make all its properties optional.

Would you have time to submit a PR to fix this and test it in your application? 👀

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do I define a function with optional arguments?
Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c . You would have...
Read more >
Documentation - More on Functions - TypeScript
We've created a type parameter Func that doesn't relate two values. That's always a red flag, because it means callers wanting to specify...
Read more >
Using Python Optional Arguments When Defining Functions
In this tutorial, you'll explore the techniques you have available for defining Python functions that take optional arguments. When you master Python ...
Read more >
Parameters — Dataiku DSS 11 documentation
The definition of each parameter has the following fields: type : Type of the parameter. Most common types are STRING, INT, DOUBLE, BOOLEAN,...
Read more >
Declaring optional function parameters in JavaScript - Flexiple
In today's article, let's take a dive into how Optional Parameters in JavaScript can be implemented and also understand where exactly using them ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found