Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Upsert Mutations #25

Closed
stigi opened this issue Oct 18, 2016 · 14 comments
Closed

Upsert Mutations #25

stigi opened this issue Oct 18, 2016 · 14 comments

Comments

@stigi
Copy link

stigi commented Oct 18, 2016

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

@mvanlonden
Copy link

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

@marktani
Copy link
Contributor

marktani commented Apr 1, 2017

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.

@mvanlonden
Copy link

mvanlonden commented Apr 1, 2017

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.

@mjsisley
Copy link

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?

@marktani
Copy link
Contributor

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

@export-mike
Copy link

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.

@shanecav
Copy link

shanecav commented Aug 7, 2017

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.

@sabuydee
Copy link

sabuydee commented Dec 2, 2017

@export-mike +1

@marktani
Copy link
Contributor

This has been implemented in the Prisma 1.0 release.

@tehwayne
Copy link

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

@marktani
Copy link
Contributor

marktani commented Feb 3, 2018

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

@pugson
Copy link

pugson commented Feb 5, 2018

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

@muhajirdev
Copy link
Contributor

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?

@schickling
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests