paint-brush
Relay modern: Testing queries.by@ven_korolev
438 reads
438 reads

Relay modern: Testing queries.

by Ven KorolyovJuly 25th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In previous stories I shared my thoughts on how to test <a href="https://hackernoon.com/tagged/react" target="_blank">react</a> components and graphql, now it is Relay’s turn. <a href="https://hackernoon.com/tagged/relay" target="_blank">Relay</a> is a great data-manager from Facebook. You can find some information about it in my previous stories. Let’s start.

Company Mentioned

Mention Thumbnail
featured image - Relay modern: Testing queries.
Ven Korolyov HackerNoon profile picture

In previous stories I shared my thoughts on how to test react components and graphql, now it is Relay’s turn. Relay is a great data-manager from Facebook. You can find some information about it in my previous stories. Let’s start.

Corner cases

The main corner case in using Relay is that any query you are using might be changed over time and you won’t find it out unless you try to compile relay. If you don’t compile and try to run a query which fetches a field and that field no longer exists on the server you will get an error saying that: “Cannot query field \”firstName\” on type \”User\”.”.

I bet you don’t want to have that, so don’t I. That is why I decided to test relay queries. A relay query looks exactly how a graphql query does.

Solution

We need to execute a query against a graphql schema to understand that everything is ok with the query. The only way to check that is to use parseGraphQLText from relay-test-utils library. We need to call that function with a schema and a query text. If an error is thrown it means that query has some issues, otherwise this function just stay silent.

That’s all. Pretty simple and straightforward! If you like this story like and share it :) Piece.