How to query your schema with GraphQL fragments

David Mráz
Atheros
3 min readMar 20, 2018

--

Also available at https://atheros.ai/blog/how-to-query-your-schema-with-graphql-fragments without the paywall.

What is GraphQL fragment

First, we have to answer the question, “what is a fragment”?

A fragment is basically a reusable piece of query.

In GraphQL, you often need to query for the same data fields in different queries. The code to define these fields has to be written multiple times, leading to more errors. By reusing this code, we can be more efficient with our time and reuse these pieces of query logic on different queries. But how we can use it in GraphQL? Let’s say we want to use the same query as in this article on aliases.

How to use GraphQL fragments

You can start by cloning our example repository

git clone git@github.com:atherosai/graphql-gateway-apollo-express.git

install dependencies with

npm i

and start the server in development with

npm run dev

Then you should be able to access GraphQL Playground. You should then be able to execute the query where the aliases concept is used:

--

--

David Mráz
Atheros

Founder of https://atheros.ai | Artificial Intelligence | Machine Learning | JavaScript, Python, C++ | Business