GraphQL Backend - Structure

Explanation of the folder structure for the GraphQL Backend
This post is part of a Series of posts, and may not make a lot of sense without reading the previous installments.

Introduction

In the previous post of this series, GraphQL Backend, we went over how to clone the repo, and deploy it to have a functional graphql backend endpoint.

On this post, we will go over the folders and files in the repo and how to add more queries.

structure

Structure

<root>

config.json

A JSON file to hold configuration settings in case we need some.

serverless.yml

Configuration for the serverless framework. Function gql is defined here. The settings allow for an https trigger using API Gateway.

utils.js

Utility belt with some functions.

gql.js

Entry point to our lambda function.

Of note:

/data

Holds a JSON file pokemon.json with some information about 26 pokemon.

/g

This is where our graphql stuff will reside.

folder-g

/entities

Where we define our object types. The two main files in here are _Query.gql and _Mutation.gql.

Any files in this folder are read by utils.gql.readSchema() and aggregated into our graphql schema.

_Query.gql

In this file, we define the queries that will be available from our graphql API.

_query.gql

To add a new query, you can add a new row with the query name, its parameters, and a return type. A type is defined in a .gql file. You can inspect the available ones like Paging and Pokemon.

In the Pokemon type, you can see how other types are nested in it, like Stats and Evolution.

A comprehensive explanation of type definitions can be found here.

_Mutation.gql

This file defines mutations available from our graphql API.

_mutation.gql

It is the same as _Query.gql; the difference being that these queries change (mutate) underlying data.

You can think of a query as a GET, and a mutation as a POST or PUT.

/resolvers

Our javascript logic for our graphql API.

/Query

Query resolvers go here.

If you open up ./entities/_Query.gql, you will see that each defined query has a respective resolver in this folder.

query-folder-match

Thru this convention, our lambda will know to call the proper resolver for the queries.

/Mutation

Same as /Query but for mutations.

What next

Next entry for this series will be

How the pokemon query works and adding a new query for move.



Back to Post List
ok!