Check out a free preview of the full REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course:
The "Using REST & GraphQL" Lesson is part of the full, REST & GraphQL API Design in Node.js, v2 (using Express & MongoDB) course featured in this preview video. Here's what you'd learn in this lesson:

There are many different strategies to exposing and using both REST and GraphQL APIs on the same API.

Get Unlimited Access Now

Transcript from the "Using REST & GraphQL" Lesson

[00:00:03]
>> Scott: All right, so let's move on to the the last little bit that I got here. That's the last excercise we have. I just got some more stuff to talk about here, and then we can just freely chat. The last bit I wanna talk about was REST and GraphQL.

[00:00:15] So, we've been working with REST, we've been working with GraphQL, and they've kinda just been sitting next to each other, so how can they work together?
>> Scott: So, there's some different ways you can use them. REST next to GraphQL is basically what we've been doing now, and this is what we're using, like I said.

[00:00:32] Great if you still need to use your REST API. If you have a API that's serving this app, and this app might be like, I don't know, a web app, let's say it's a web app, right, that same API, you want to use for your new mobile app, your new shiny mobile app that you built in Swift or React Native, but you want that to use GraphQL, but you don't wanna build a whole another API.

[00:00:56] You wanna use the same API that's the same data. So you're just like, I'm gonna put GraphQL next to it, and then that way the Web app can still use the REST, and the mobile app can use the GraphQL. So they're sitting next to each other on the same API, accessing the same data, but with two different approaches.

[00:01:10] One's via REST. One's via GraphQL. So that's something. That's the strategy you can implement there. It's also great if you're still evaluating GraphQL and/or you are still building it. So let's say you do have a web app, you're thinking about using GraphQL, so you build it next to your REST implementation or whatever implementation you have, and you switch some components over to user GraphQL in point progressively while you're still evaluating it, or maybe you made your decision, you totally wanna use it, but it's gonna take time, and you've still gotta serve your customers.

[00:01:39] So you still put it there side by side, and eventually you change everything over with some tests and stuff. So that's implementation. I've helped tons of companies convert over to GraphQL, that's the approach we took. We put it right next to REST and we start with some test page based on some criteria, and we'll start switching it over until everything is done, and then we'll just turn off the rest.

[00:01:58] So you can totally do that, and that's literally what we're doing now. It's sitting next to each other, and you know you're sitting next to each other, the GraphQL and the REST, when inside of the server it's mounted. Here's our rest API. Here's our GraphQL API. They're literally right next to each other.

[00:02:20] Another approach you can do is you can convert REST to GraphQL. So what that means is you take an incoming REST request, so if you have a URL like /user/ID you would take that URL, and you would turn that into a query on the fly. So, basically what I'm saying is, you would take a URL like this.

[00:02:39] So you would say I'm gonna take /api/user/12, and I'm gonna convert it to this string that looks like user with the id of, user with 12, and give me back the id, give me back the username, something like that. So you would take that and convert it to this, and then you would take this and you would run your GraphQL schema, I mean, sorry, you will run the GraphQL query against the schema with that given query.

[00:03:19] So you will literally do exactly what we did in the text this whole time. So we go back to our test see how we're running a query right here. You can just do this on your server, there's nothing stopping you from doing that, except for this query right here would be the dynamic query that was created from your REST string.

[00:03:35] So you would convert the REST route into a query or a mutation, and then just run it against your schema with the variable, with the given sign in user and then you just convert REST to GraphQL on the fly. So at the end of the day you still have GraphQL getting your data, but you're accessing it via REST.

[00:03:57] So, again, it allows you to run GraphQL schema, great for using the power of GraphQL to retrieving data. We also have the power of GraphQL, how easy it is to pick data and do stuff on the fly. So by masking it with REST, you get the best of both worlds.

[00:04:12] If you love REST, then you can still use that, but then you have GraphQL actually retrieving the data, going to multiple sources across the Internet, or across your replicants, or where ever all your data is stored they can go and get all that data and come back and feed it to your REST requesters.

[00:04:28] It is also great for keeping your REST API. So if you really just wanna keep you rest API, but still use GraphQL for data retrieving, you can totally do that. So that's another implementations. There's some other implementations you can probably fix like you can have convert to GraphQL, but still use some of REST over here, and maybe these routes are GraphQL and this one's not, so it can be pretty custom, but these are just the two overall things that you can probably do.