Advanced GraphQL Advanced GraphQL

Q&A: Multiple Mutations & Batching

Check out a free preview of the full Advanced GraphQL course:
The "Q&A: Multiple Mutations & Batching" Lesson is part of the full, Advanced GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott discusses optimizing GraphQL using multiple operations and batching.

Get Unlimited Access Now

Transcript from the "Q&A: Multiple Mutations & Batching" Lesson

>> Scott: Could you do a mutation that doesn't return anything? [INAUDIBLE] that doesn't [INAUDIBLE]
>> Speaker 2: So the story of mutations in the GraphQL community and what their supposed return is nobody has a standard. Queries, yeah, you need to return whatever was requested, but a mutation is kind up to your app.

[00:00:21] So if you're making a to do app, and you have mutation where you delete a to do, what does your app need back in order for it to resolve its UI? So if it all it needs back is an ID, then you return an ID. But if it needs back the whole to do, does it need back, whatever?

[00:00:35] So it's kind of up to you on a mutation. So, yeah, if you don't wanna return something, you don't really have to, but it really depends on what you're doing with your application, yeah.
>> Scott: Can you do multiple mutations in a request?
>> Speaker 2: Yes you can. There's different ways to do multiple anything in GraphQL.

[00:00:58] So there's batching, where you actually do multiple queries and/or mutations in one HTTP request, so that's batching. And then you have the ability to do multiple mutations or multiple queries in one single operation. Then you can take those and you can batch those, right? So, [LAUGH] it can get pretty complicated, but yes, you could totally do that.

[00:01:26] So if I wanted to do two queries here, let's say helloWorld message, or yeah. So say helloWorld, watch this, what does it need? Some type of thing here. Right, so I can take this, I can copy this, and I can do it again, and I can do it again, and I can do it again.

[00:01:47] But what you really wanna do is you wanna alias this stuff. So you'll say like one, two,
>> Speaker 2: Three. So these are aliases for these queries, cuz it's gonna spit back an object, and they are gonna have the same key of helloWorld, so it's just not gonna work.

[00:02:09] So you've gotta alias them differently, so that they actually come back as like that. So we've got helloWorld one, two, three. So, yeah, and you can do the same thing with mutations. So if this was a mutation, yeah, these could also be mutations too and it's totally fine.

[00:02:27] You just gotta do the same thing, you've gotta A-list them. But you couldn't mix multiple mutations with multiple queries in one operation. That's where batching would come in. So you can batch mutations and queries in one request, but you couldn't do them in one query. Does that make sense?

>> Scott: Yep.
>> Speaker 2: Okay. Yes?
>> Scott: Are there performance implications when you do batching versus multiple operations in query?
>> Speaker 2: Yep, and that's what you're gonna learn today, how to fix. But yeah, so batching is really cool for the frontend. That's more like, I wanna increase performance on the frontend.

[00:03:05] Less HTTP calls, right? It's not so much of a backend optimization, because a backend is still gonna perform all of those queries. It doesn't matter. And then when it comes down to doing multiple queries or mutations in one operation, again, it's not really a performance. What you're gonna learn today is how to take advantage of that.

[00:03:25] So if you were to get multiple ones, or you were to get those that double referenced each other in a circular way, you're gonna learn how to take advantage of that and be performing. Otherwise, you will have a performance hit, because you'll be querying for the same stuff over, and over, and over for the same request, which depending on where your data source is, could be detrimental.

[00:03:44] If your data source is somewhere across the world in some database for some reason, and every single request somebody is just drilling you with this stuff, yeah, yeah, it could be pretty bad. But luckily, the way the resolvers work in GraphQL, you can take advantage of it in a really good way.