Search⌘ K
AI Features

Organizing the Backend Project

Explore how to organize your GraphQL backend project for clearer code management by separating schema, resolvers, and configuration files. Learn to configure your server, run with nodemon, and set up a local GraphQL playground for testing your API.

Organizing the server

To make our project more organized, we’ll split our code into the following files:

  • resolvers.js: A file fort our resolvers.

  • schema.graphql: The GraphQL schema for our project.

  • schema.js: A file that contains code to read the GraphQL schema.

  • nodemon.json: The configuration for the nodemon tool.

This is how the files are structured on the disk:

Javascript (babel-node)
├── nodemon.json
├── package-lock.json
├── package.json
└── src
├── index.js
├── resolvers.js
├── schema.graphql
└── schema.js

First, we need to move our schema to a separate file. For now, it contains a single appName query.

type Query {
appName: String
}

Now we need to read it.

Javascript (babel-node)
const { readFileSync } = require('fs')
function readSchema() {
return readFileSync('src/schema.graphql').toString('utf-8')
}
...