Returning data when doing a mutation trying to re-create record which was already created

Hi. When I try to do a create operation with an ID to an entity which was already created, I don’t get the entity itself in the result, but only an empty response/array.

For eg. let’s say that I create a user with ID 1234 for the first time - I get the created record itself as the response to my GraphQL Mutation for the first time.

Now, let’s say I try to create the same user with ID 1234 again without changing anything - I get an empty [] in the response something like this:

q3

Is there any way to get Dgraph to respond back with the data of the already created node/record during mutation of an already created record rather than having to query it separately? Also, a problem I notice here is that, Dgraph does not indicate in any way saying that the record already exists.

Thanks.

Btw, I am manually generating UUID myself and inserting them for every node and not using the Dgraph generated IDs itself. Using it like this:

type Account {
    """
    Unique ID for the account
    """
    id: String! @id
    """
    First name
    """
    firstName: string!
}

Hi @tvvignesh,

I believe returning out the entry will not be the right solution. From your example, I noticed you are creating the same node with id 1234 again and again. But consider this for an example, a service that selects id based on a mechanism and due to some concurrent access, give same id to two different objects say usernames A and B. Now in this case, first mutation for username A will be success, but in second mutation, it will create a conflict. And Sending out the previously created object for the given id can set wrong expectation with API’s. I believe better way would be to check the numUids value. It specifies the number of Uids created for the mutation. Its a more concrete way to validate the success of mutation.

Can you share some more details. Because as per the implementation while using add mutation, if the id object already exist, we do send out already exist error.

1 Like

@aman-bansal Hmm. Makes sense. Maybe an upsert operation can behave this way rather since if the data already exists, it will update and return or if the data doesn’t it can create and return. Maybe that will satisy this usecase.

But the tricky thing is I was just getttng an empty array in response. Will check this again as well and revert back. Thanks.