Advanced GraphQL, v2 Advanced GraphQL, v2

Errors & Testing Solution: Resolvers

Check out a free preview of the full Advanced GraphQL, v2 course:
The "Errors & Testing Solution: Resolvers" Lesson is part of the full, Advanced GraphQL, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott live codes the second part of the solution to the testing exercise, and builds the testing resolvers. GraphQL tests only function when testing simple queries and mutations, while Apollo tests directives too.

Get Unlimited Access Now

Transcript from the "Errors & Testing Solution: Resolvers" Lesson

[00:00:00]
>> And then for resolvers, let's make one for resolvers. This one should be pretty simple. So we can say describe resolvers and tests. I'll just pick one, Update settings. Let's just do feed, so query.feed, let's do that one, this can get more simple than that. So we go here, close that will import our resolvers.

[00:00:43] Or is that a default export? Yep, default export and easily resolvers.test, There we go, so resolvers, Source resolvers. Cool, so we got some resolvers here and we'll just say, feed and it's literally just an object. So let's say resolvers.query.feed, and we can just mark out all these values.

[00:01:25] So, no, here we go look at the resolvers and see what it's expecting. Right now it's expecting your context to have models and it's doing fine many cool. I'll just mark that out. No big deal. So that's an object to the models on it. And it has post object on it.

[00:01:44] And that has fine mini, and that's a method that returns something. I get this return an array of strings, I can do whatever I want, doesn't matter here. So then Results, Equals that. So we get the result then I could say expect results to equal whatever is returned here.

[00:02:21] Coming around that to us If I didn't break anything that should work. Cool, and we tested our resolver a real test obviously, you would you would test the input of this but there's no input. So you probably just test to see that it was called. I expect model.post.findMany to have been called.

[00:02:42] And that's what you would test for because this is such a trivial example. Cuz it's literally not doing anything but other calling a factual argument. Cool, just FYI, there is another way to issue these queries and mutations in a test without having to create a server. You could just use the GraphQL function from GraphQL.

[00:03:07] So this GraphQL function right here takes a few things you can basically if you invoke the function, I thought it would give us some type definitions or some TypeScript support. But basically it takes like four arguments. But what it does is it's going to return a promise that when given like some typedefs, some resolvers, actually I think the queries of first argument.

[00:03:32] So you give it a queries and typedefs and some resolvers at minimum, then you can even like sent a context and some stuff like that. It's gonna return a promise of the resolve result which is pretty much exactly what we're doing here. So you can do that without even setting up a server just by using the GraphQL function.

[00:03:48] In fact, this when people talk about GraphQL this what they're talking about is just one function. GraphQL is just one function. It's a function that takes these things and resolves the value has nothing to do with a server, has nothing to do with a client, has nothing to do with anything.

[00:04:03] It's a function that can take a query, validate it against some type definitions or some resolvers, and come up with the result asynchronously, that's GraphQL. So you can use this to actually write a test. The only reason you'd wanna use the Apollo Server setup is if you were testing something that Apollo provided to you, you'd want to use that.

[00:04:21] For instance, if we started using custom direct does and stuff like that, well yeah. We're not gonna use that GraphQL thing because that GraphQL function is not going to know about a custom directives would Apollo it though. So you can't like bypass a lot of that stuff. So it's only good if you're just only testing like simple queries and mutations and things like that.