1

when querying data from a GraphQL Server, urql adds a _typename field to track the cache:

{
    __typename  "Book"
    name  "test"
    description "the book"
    id  "hPl39w4rzc2HZxkfHDyj"
    auther "John Doe"
}

I want to update this object and save it back to the database. when the useMutation function is called __typename is passed also and since it is not part of the server schema it results in an error:

Field \"__typename\" is not defined by type

I know I can do something like this before calling useMutation:

delete input.__typename;

but I want to know if there is a better way of handling globally in urql? For instance, in apollo-client you can use ApolloLink to remove __typename from all the variables before updating

capiono
  • 2,875
  • 10
  • 40
  • 76

1 Answers1

2
const result = {
    __typename:  "Book",
    name:  "test",
    description: "the book",
    id:  "hPl39w4rzc2HZxkfHDyj",
    auther: "John Doe"
}

const {__typename, ...rest} = result;

console.log(rest)

It's probably best to keep that marker on the objects in the graphql client because it helps the cache do its thing. So you can instead remove it as needed -- here you can update "rest" and use that to call your update mutation.

Aadmaa
  • 829
  • 5
  • 13
  • 2
    I'd also add that there's an option to always automatically hide this property, which we offer in case you'll never be actively using it yourself. We like being explicit, which is why it isn't hidden, but we do offer the option. The option's called `maskTypename` which you can set to `true` when creating the client: https://formidable.com/open-source/urql/docs/api/core/#client – Phil Plückthun May 21 '21 at 13:00
  • In my case, sometimes I need `typename` or not. Can we define this option when we build a request instead that globally on the client ? – Rifton007 Jul 29 '21 at 01:02