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 Error: WithTypename type makes all properties optional which conflicts with updateQuery function

See original GitHub issue

Describe the bug

The bug is explained here: https://github.com/dotansimha/graphql-code-generator/issues/7447

You can see an image of the typescript error here, the code snippet comes from this file: image Here you can also see the typescript error in a failed GitHub CI run: https://github.com/AndysonDK/syncbase/runs/7416169280?check_suite_focus=true

Your Example Website or App containing the error

https://github.com/AndysonDK/syncbase/tree/f9fcc30604dddbeee82a5ec69b74a72887825b79

Steps to Reproduce the Bug or Issue

Clone this exact code: https://github.com/AndysonDK/syncbase/tree/f9fcc30604dddbeee82a5ec69b74a72887825b79

You only need to run the typecheck script in the web folder to see the type errors occurring

Expected behavior

The type error shouldn’t appear, the type should probably preserve the original type, but apparently that messes with this issue: https://github.com/FormidableLabs/urql/issues/2502 Also see this: https://github.com/dotansimha/graphql-code-generator/pull/8062#issuecomment-1188820460 I should’ve added tests to my PR to prevent reversion of important changes

Platform

Here are the graphql-codegen packages + the graphql package used in the project:

"@graphql-codegen/cli": "2.9.0",
"@graphql-codegen/typed-document-node": "^2.3.2",
"@graphql-codegen/typescript": "2.7.2",
"@graphql-codegen/typescript-operations": "2.5.2",
"@graphql-codegen/typescript-urql-graphcache": "^2.3.2",
"graphql": "^15.8.0"

I’m running on WSL 2 with the following Linux distro:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.4 LTS
Release:        20.04
Codename:       focal

node version: v16.13.0

Codegen Config File

"codegen": {
  "watch": true,
  "schema": "../server/schema.graphql",
  "documents": "src/graphql/**/*.graphql",
  "generates": {
    "./src/graphql/generated.ts": {
      "config": {
        "defaultScalarType": "string"
      },
      "plugins": [
        "typescript",
        "typescript-operations",
        "typed-document-node",
        "typescript-urql-graphcache"
      ]
    }
  }
}

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
saevarbcommented, Aug 4, 2022

@Axedyson, the issue seems to come from the inference that cache.updateQuery() is doing based on the query definition and the data that you pass from result.

That doesn’t sound right.

I just now started experiencing this issue because a lockfile was regenerated, and it happens because the WithTypename type that is generated by graphql-codegen suddenly changed in some version, and WithTypename is used by the urql-graphcache plugin.

The updateQuery function will always infer the correct type – it’s working directly with type typed document node. Doing what you have suggested is simply wrong, because the type(in your case, { me: WithTypename<User> } is incorrect; that’s not the type of the result of the query.

It seems to me that the urql-graphcache plugin needs fixing so that it doesn’t use this WithTypename, though I’m personally just going to figure out where the WithTypename type changed and downgrade.

1reaction
Axedysoncommented, Jul 29, 2022

Yep that does the job thanks. Yeah would be nice to have generation of cache queries to be used with graphcache!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Some problems with the optional properties in typescript ...
This behavior was introduced by this PR a while ago. The basic idea is that if a type has only optional properties it...
Read more >
Understanding and using interfaces in TypeScript
An interface contains the name of all the properties along with their types. It also includes the signature for functions along with the ......
Read more >
Documentation - TypeScript 2.1
Mapped Types. One common task is to take an existing type and make each of its properties entirely optional. Let's say we have...
Read more >
Customizing the behavior of cached fields - Apollo GraphQL
The second parameter is an object that provides access to several properties and helper functions, including any arguments passed to the field. See...
Read more >
TypeScript Fundamentals - Joy of Code
The pokemon array contains only strings, so we get a type error when trying to assign ... Function arguments can be made optional...
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