urql-graphql / urql

The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
https://urql.dev/goto/docs
MIT License
8.54k stars 444 forks source link

Invalid field error after updating from 7.1.0 to 7.1.1 #3620

Closed schmt5 closed 1 week ago

schmt5 commented 2 weeks ago

Describe the bug

After updating from 7.1.0 to version 7.1.1 we encouter an error message invalid field (https://commerce.nearform.com/open-source/urql/docs/graphcache/errors/#4-invalid-field).

Insideexchanges > cacheExchange > updates > Mutation a Mutation triggers a runtime error, because an entity is null.

I'm not sure if this is a bug or a problem in our codebase, but this error only appears after updating to 7.1.1.

Thank's for a reply

Reproduction

I will not provide a reproduction in the first step, because I'm not sure if this is a real bug or a problem in our codebase.

Urql version

urql v4.1.0 @urql/exchange-graphcache v7.1.1

Validations

JoviDeCroock commented 2 weeks ago

I mean, we can't really tell you if it's an issue in your application without a reproduction 😅 you can look at the changes done in 7.1.1 which is only 1 and extrapolate that to your case https://github.com/urql-graphql/urql/blob/main/exchanges/graphcache/CHANGELOG.md i.e. are you writing an abstract/concrete type? Are you selecting that? .... We need more information that goes outside of "something breaks".

One thing we can derive from invariant 4 being thrown is that you are using schema-awareness.

EDIT: I think I know a possible fix is to change this line to isMatching || (currentOperation === 'write' && !ctx.store.schema) trying that is probably your best bet 😅

negezor commented 2 weeks ago

In the same situation, only in query with union. Going back to version 7.1.0 there are no problems.

Invalid field: The field `activity` does not exist on `User`, but the GraphQL document expects it to exist.
Traversal will continue, however this may lead to undefined behavior!
(Caused At: "fetchNotificationGroups" query, "NotificationNodeCommon" Fragment, Inline Fragment on "User", "TeamCommon" Fragment)
https://bit.ly/2XbVrpR#4

Invalid undefined: The field at `activity` is `undefined`, but the GraphQL query expects a scalar (number, boolean, etc) for this field.
(Caused At: "fetchNotificationGroups" query, "NotificationNodeCommon" Fragment, Inline Fragment on "User", "TeamCommon" Fragment)
https://bit.ly/2XbVrpR#13

Invalid field: The field `viewerIsMember` does not exist on `User`, but the GraphQL document expects it to exist.
Traversal will continue, however this may lead to undefined behavior!
(Caused At: "fetchNotificationGroups" query, "NotificationNodeCommon" Fragment, Inline Fragment on "User")
https://bit.ly/2XbVrpR#4

Invalid undefined: The field at `viewerIsMember` is `undefined`, but the GraphQL query expects a scalar (number, boolean, etc) for this field.
(Caused At: "fetchNotificationGroups" query, "NotificationNodeCommon" Fragment, Inline Fragment on "User")
https://bit.ly/2XbVrpR#13

Invalid field: The field `threadId` does not exist on `User`, but the GraphQL document expects it to exist.
Traversal will continue, however this may lead to undefined behavior!
(Caused At: "fetchNotificationGroups" query, "NotificationNodeCommon" Fragment, Inline Fragment on "User", "CommentCommon" Fragment)
https://bit.ly/2XbVrpR#4

// ... and many other similar things
treygrr commented 2 weeks ago

Same situation as above user.

query GetEmployee {
  employee {
    __typename
    ... on Employee {
      email
      name {
        __typename
        display
      }
      sso
    }
    ... on EmployeeNotFound {
      email
      message
      sso
    }
  }
}

Nets this output: Invalid field: The field `message` does not exist on `Employee`, but the GraphQL document expects it to exist. Traversal will continue, however this may lead to undefined behavior! (Caused At: "GetEmployee" query, Inline Fragment on "Employee")

We are aware this could be an issue with our non compliant schema but posting just in case.

schmt5 commented 1 week ago

Thanks for the quick reply. I will see if I can provide a reproduction.

JoviDeCroock commented 1 week ago

The possible solution is posted as a canary, in the future it would be much easier for someone to try the fix I posted 😅 you can do so now by installing the canary version of graphcache, for the record, if someone is encountering this without schema-awareness, we need a reproduction...