Prisma1: Upsert Mutations

Created on 18 Oct 2016  路  14Comments  路  Source: prisma/prisma1

Currently each model gets an update and a create mutation. Performing an _update or create if needed_ requires an additional query to be performed before the mutation.

A possible solution could be an upsertModel mutation that would try and query the model by one of the passed fields instead of a passed ID (still need to come up with a smart way of defining which field should be used for the lookup) and updates the row. If no model object matches the query a new one will be created with the passed parameters.

For the _upsert_ mutation the parameter signatures would be similar as for the _create_ mutation. All fields that are required for _create_ need to be required for _upsert_ (contrary to _update_ where every field besides the id is optional).

Most helpful comment

Would be ideal if you can run an update without knowing the id in for the model, In my mind its kind of the point of an upsert really.

All 14 comments

When can we expect to use this feature? What are workarounds for the time being?

We introduced a updateOrCreate mutation (undocumented so far) that allows you to update a node by id or create a new one instead (if there is no node by that id), but after the initial feedback we got we are taking additional time to make it possible to update nodes by other unique fields. We're not on a specific timeline for this feature though.

A current workaround is first querying the node in question and then either using the update if it exists or the create mutation.

Thanks for the quick response as always @marktani! So updateOrCreate let's you specify the id for creation? For example, my use case involves the login/signup flow:

  1. User goes through auth0 login popup
  2. On authentication set the token in localStorage, getUserInfo from auth0 and create user
  3. If creation fails with error code 3023 (user exists), updateUser
  4. After creation or update, reload page to add authorization header with token from localStorage to networkInterface

At this point I can query query { user { id } } and get the id, but not when I'd like to perform the user update in step 3. If updateOrCreate will allow me to create a user node with the id supplied from auth0, then I can use that id in the authorization callback to update the node on future logins. Alternatively, if I could update the user node by specifying the unique auth0UserId field that could also solve my problem.

Still doesn't look like it has made it into the Docs. Am I safe to use the undocumented version? Is this still on the horizon? I see this issue was part of the GitHub project tracker that was closed May 3rd. Has the project roadmap moved?

Hey @mjsisley just saw this. updateOrCreate is still undocumented and may receive breaking changes in the future, when we add more functionality to it.

@mvanlonden It sounds like your workflow is nicely covered by the custom authentication feature currently in beta, here's an example: https://github.com/graphcool-examples/functions/tree/master/auth0-authentication

Would be ideal if you can run an update without knowing the id in for the model, In my mind its kind of the point of an upsert really.

It would also be great if upserts were available within nested mutations.

For example, assuming the following schema (not showing id/created/updated fields):

type Book {
  isbn: String! @isUnique
  title: String!
  reviews: [Review!]! @relation(name: "BookReviews")
}

type Review {
  title: String!
  book: Book @relation(name: "BookReviews")
}

Let's say I wanted to add a new Review, but don't know if the relevant Book already exists in my data or not. I'd like to be able to use a createReview mutation that allows me to include the book field's isbn and title (similar to how nested create mutations currently work). That way, a new Book could be created if necessary, otherwise it would just relate the existing Book (with a matching isbn) to my newly created Review.

@export-mike +1

This has been implemented in the Prisma 1.0 release.

Assuming this means it's usable outside of Prisma also? Just using the graphcool cloud?

No, Prisma is being developed separately from Graphcool Framework. You can read more about Prisma and Graphcool Framework in the Forum.

@marktani Any plans to add support to Graphcool? I would really love to use updateOrCreate without knowing the ID first.

I am wondering if @shanecav case is possible now?

I can see in prisma there's field create and connect. inside nested mutation. Thus in @shanecav case. would be nice if we have something like createOrConnect?

Thanks a lot for bringing this up @muhajirframe. There's a now a new feature request for adding createOrConnect support: #3724

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcmar picture mcmar  路  57Comments

schickling picture schickling  路  44Comments

marktani picture marktani  路  41Comments

sorenbs picture sorenbs  路  52Comments

marktani picture marktani  路  34Comments