On my first interaction with GraphQL i tried to create this Boilerplate for future projects
Clone the project
git clone https://github.com/Ayoub-Mabrouk/GraphQl-Apollo-Express-Mongoose-BoilerPlate
Install packages with yarn/npm
yarn
or
npm install
Environment Variables
cp .env.example .env
# you should be able to modify the environment variables in the .env .
Start the server
yarn dev
Generate types based on the graphQL-Schema
yarn gen
# types can be found at src/generated/graphql.ts
- go to the models directory
linux commands:
cd src/models
mkdir ModelName && touch ModelName/index.ts
- Take a look at the existing Models (User/Company/Role)
- import your model into src/models/index, to use the destructure operator when you need to use your models elsewhere
- go to the graphql-schema directory
- each type has it's own folder
- each folder has two files (Schema & Resolver)
linux commands:
cd src/graphql-schema
mkdir schemaname && touch schemaname/schema.graphql
- Take a look at the existing Schemas (user/company/role)
- src/graphql-schema/index is where all type-defs and resolvers get merged to make a schema
Check DataLoader Docs
- extract dataLoader from the context
- try to use load in this situation
return await dataLoader?.company.load(company)
- extract dataLoader from the context
- try to use loadMany in this situation
return await dataLoader?.company.loadMany(roles)
- import your model and pass it insdie the newLoader function
export default {
//each sub-resolver will have its own dataloader
company: newLoader(companyModel),
role: newLoader(roleModel)
};
Check Shield Docs
- rules can be found or added inside:
cd src/permissions/rules.ts
- use the rules inside permissions/index.ts to specify certain permissions for certain users.
Database: Mongodb
ODM/MongoDB Node.js Driver: Mongoose
Server: NodeJs, Express,Apollo-Server-Express
Best Tool: TypeScript
Contributions are always welcome!