Relay modern: Testing queries.

Written by ven_korolev | Published 2018/07/25
Tech Story Tags: react | relay | graphql | testing-queries | relay-modern

TLDRvia the TL;DR App

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.


Published by HackerNoon on 2018/07/25