Organizing the Backend Project
Learn how to better organize our server project, which will help us easily navigate the project.
We'll cover the following...
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 thenodemon
tool.
This is how the files are structured on the disk:
Press + to interact
├── 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.
Press + to interact
type Query {appName: String}
Now we need to read it.
Press + to interact
const { readFileSync } = require('fs')function readSchema() {return readFileSync('src/schema.graphql').toString('utf-8')}
...