Does anybody have any idea how to solve the follow...
# prisma-whats-new
g
Does anybody have any idea how to solve the following: I have a node
Parent
that has many
child
. Every time I create a
child
I want to set
childNumber
property that auto increments. But
childNumber
will be unique for each
Parent
Is there any way to do this besides querying all the
child
nodes and bruteforcing to find the next number?
n
does that mean a parent might have childs 1, 4, 5, and the auto increment spans across all parents?
g
no each parent would have 1,2,3
but child might get deleted so it could also have 1,3,4
n
in case of 1,3,4 for a specific parent, the next child would get 5?
g
yes
n
cool, you can query the highest current number like so
g
although it might not matter too much if that makes it easier
n
Copy code
query allChildrenOfAParent {
  allChildren(orderBy: childNumber_DESC, filter: {
    parent: {
      id: "<some-id>"
    }
  }
  first: 1  
  ) {
    childNumber
  }
}
g
thanks that should work
this would also be a good use case for synchronous mutation callbacks
any update on that?
n
not yet, sorry! Would an async mutation callback not work here as well?
g
it might, but not as well
I think I’d rather do the query on the client in this case
n
yea I agree. How would sync mutation callbacks be a better answer here though?
g
the value would be available right away
n
I'm not sure I am following. What mutation would you put the callback on, and how would the payload look like?
Ah. I guess you could query the highest number of a parent's children, whenever a new child is created. Is that what you mean?
However, you seem to have a different understanding of the term synchronous mutation callback than I do. Synchronous mutation callbacks as described here: https://github.com/graphcool/feature-requests/issues/58 allow the synchronous execution of multiple callbacks for the same mutation. If one callback fails, subsequent callbacks are not executed.
Is that what you think of or do you describe something else?
g
when I read that feature request it seems like the synchronous callback would be some code that runs in the cloud that gets executed as soon as a mutation is sent to the graph.cool servers
graph.cool would not return a response until the callback returns
the synchronous callback could be used for custom validation logic and other things
n
Ahh I see! We refer to this as the request pipeline and will offer you more hooks (pre/post) for queries and mutations. You are right, this is a great use case for this. Now I understand what you mean, the request pipeline allows you to basically hook into a request synchronously or asynchronously.