Search code examples
vue.jsgraphqlurql

removing __typename field from urql query result before mutation


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


Solution

  • 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.