Testing GraphQL with Laravel

Oliver Nybroe
2 min readMay 23, 2018



This article assumes knowledge about GraphQL and we will use the package Lighthouse for making the GraphQL server, if you wan’t to learn more about Ligthouse checkout my article for that. We will focus on creating tests for graphQL in this article and write some tests for the queries we made in the previous article.

In our schema we have articles and users, where articles has an author which is a user. There is only one query endpoint called articles, which returns a Relay compliant paginated object. We are going to write tests for the endpoint articles.

Our schema definition for our GraphQL server

When testing GraphQL we first have to mirror how a GraphQL post request looks like. This is done with a query parameter where the value is our query, so let’s make a quick helper method for this as this is something we will use often.

So let’s start with a simple one, let’s check if we can get the title from the endpoint.

Not that pretty, and we should probably check if we in fact only get 1 article as there should only exist one, but it works. Well let’s change it to contain multiple articles and fix our mistakes from before.

Okay, so lets break down what happened here.

  • First we created our articles with different titles.
  • Next we made our call the the graphQL endpoint.
  • Afterwards we fetch the titles from the response.
  • Then we assert if there only is 3 titles.
  • At the end we make sure that the 3 titles are in fact the 3 titles we expected.

Well how do we then check if the id is correct? Remember the id is a globalId so this has to be decoded first. Let’s create a helper for decoding the ids and checking if the globalId type is correct also.

Let’s use this new helper method to create our new test.

It works pretty much the same way as the other tests, except we now run it through our helper method first to take care of the global ids.

Well that tests looks simple too, of course we are only checking the name of the author, but checking if the fields are correct for the author should be done by tests for the author class itself. We haven’t written the test for the body field of the article, but that is pretty much just the same as the name field for the article.

Hope you enjoyed this article, you can find all of the source code in Github.



Oliver Nybroe