Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ typings/
.yarn-integrity

# dotenv environment variables file
.env
*.env
!example.env

# next.js build output
.next
60 changes: 9 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,10 @@
# MadeByCodera GraphQL Test

## Description

You are tasked with writing the GraphQL api for a new Todo List mobile app.

### Todo GraphQL Type
The structure of the Todo GraphQL Type according to the frontend developer should look like the following:

Field | Data Type | Description
------------ | ------------- | -------------
id | UUID | Unique identifier for the todo.
description | String | Describes what the todo is.
createdAt | Date | Tells us when the todo was created. Defaults to current datetime.
completed | Boolean | Indicates if the todo is complete. Defaults to false.
priority | Int | 1 is the highest priority. Defaults to 1.

### Todo GraphQL Query and Mutations
And the frontend developer is counting on you to implement the following 5 methods under the GraphQL api endpoint:
1. **List Todos** - Query - Retrieved todos can be sorted by the `priority`, `createdAt`, or `description` fields in ascending or descending order. By default the todos are unsorted. In addition, the todos can be filtered by the `completed` field.
2. **Create todo** - Mutation - `description` is required. `priority` is optional and if not provided should default to 1. The rest of the fields: `id`, `createdAt`, and `completed` should have defaults supplied for them as noted in the Todo GraphQL Type mentioned above.
3. **Update todo** - Mutation - Should update a todo based on the `id` provided in the request. `description` and/or `priority` fields can be updated. `priority` must be 1 or greater if sent in request.
4. **Mark todo complete** - Mutation - Should update a todo's `complete` field to `true` based on the `id` provided in the request.
4. **Delete todo** - Mutation - Should delete a todo based on the `id` provided in the request.

### Documentation

The front end developer would also like a little bit of documentation to help him use the api. The easiest way to provide documentation is to add comments to each query, mutation, and type that is defined in the GraphQL schema. The following multi-line comments example highlights how you should be declaring comments in the query, mutation, or type definitions:

## Configuration app
Provide .env file with arguments(see example.env).
SQL database: postgres.
Dump is located in ./todo_test_it.dump.sql.

## Run app
To run app
```
"""
<my comments go here>
"""
```

This will make it very easy for the frontend developer to see them when inspecting the running GraphQL server using [GraphQL Playground](https://www.apollographql.com/docs/apollo-server/features/graphql-playground/)

As demonstrated in the following screenshot

[![graphql-playground-example.png](https://i.postimg.cc/rw6HMzmt/graphql-playground-example.png)](https://postimg.cc/VdRgFfXY)

## Instructions
1. Fork this repository. Do all of your work on your personal fork. Then create a new pull request on your personal fork
2. You must implement this graphql backend using [Apollo Server](https://www.apollographql.com/docs/apollo-server/)
3. You can use whatever data structure you are most comfortable with for storing the data such as in-memory, MySQL, Postgres, SQLite, MongoDB, etc... . Using Postgres is preferred and considered a plus, since that is what we use currently.
4. This repo should be an npm project such that we can install the dependencies that you define for the server in order to run it using Node.

Here is an example of creating a pull request from a fork of a repository:
[![pull-request-example.png](https://i.postimg.cc/QCgrr53S/pull-request-example.png)](https://postimg.cc/RJ0Y7Wqn)

## NOTE
If you are not storing the data in-memory, please commit a sql dump file of the database schema to the repo. Please add a note of where this file is located in this `README.md` if the sql dump is not located at the root of the repo. Your submission will be **DISCARDED** if you **DO NOT** commit a sql dump file for your implementation, if it uses a database.
npm start
```
20 changes: 20 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const {
DATABASE_USERNAME,
DATABASE_PASSWORD,
DATABASE_NAME,
DATABASE_HOST
} = process.env;
module.exports = {
port: 4000,
apolloServer: {
playground: true,
introspection: true
},
db: {
dialect: 'postgres',
username: DATABASE_USERNAME,
password: DATABASE_PASSWORD,
database: DATABASE_NAME,
host: DATABASE_HOST
}
};
4 changes: 4 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_USERNAME=
DATABASE_PASSWORD=
DATABASE_NAME=
DATABASE_HOST=
35 changes: 35 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Koa = require('koa');
const cors = require('@koa/cors');
const bodyParser = require('koa-bodyparser');
const config = require('config');
const dotenv = require('dotenv');
const { resolve } = require('path');
const { ApolloServer } = require('apollo-server-koa');
const errorMiddleware = require('../middlewares/error.middleware');
const typeDefs = require('../schemas/todos.schema');
const createResolvers = require('../resolvers/todos.resolvers');
const createSequilize = require('./createSequilize');

const PORT = config.get('port');
const sequilize = createSequilize();

dotenv.config();

const server = new ApolloServer({
typeDefs,
resolvers: createResolvers(sequilize.models),
...config.get('apolloServer')
});

const app = new Koa();
app.context.sequilize = sequilize;
app.use(cors());
app.use(bodyParser());
app.use(errorMiddleware);
server.applyMiddleware({ app });

app.listen({ port: PORT }, () =>
console.log(
`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`
)
);
23 changes: 23 additions & 0 deletions lib/createSequilize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Sequelize = require('sequelize');
const config = require('config');
const dbConfig = config.get('db');
const models = require('../models');

const createSequilize = () => {
const sequelize = new Sequelize(
dbConfig.database,
dbConfig.username,
dbConfig.password,
dbConfig
);
Object.keys(models).forEach(name => {
const model = models[name](sequelize);
if ('associate' in model) {
model.associate(sequelize.models);
}
});

return sequelize;
};

module.exports = createSequilize;
13 changes: 13 additions & 0 deletions middlewares/error.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = async (ctx, next) => {
try {
await next();
} catch (error) {
ctx.status = error.status || 500;
if (ctx.status === 500) {
console.error(error);
ctx.body = '';
} else {
ctx.body = error.body || error.message;
}
}
};
5 changes: 5 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const todos = require('./todos.model');

module.exports = {
todos
};
32 changes: 32 additions & 0 deletions models/todos.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Sequelize = require('sequelize');

module.exports = sequelize => {
const todos = sequelize.define(
'todos',
{
id: {
primaryKey: true,
type: Sequelize.INTEGER,
autoIncrement: true
},
createdAt: {
defaultValue: new Date(),
type: Sequelize.DATE
},
completed: {
defaultValue: false,
type: Sequelize.BOOLEAN
},
description: {
allowNull: false,
type: Sequelize.STRING
},
priority: {
defaultValue: 1,
type: Sequelize.INTEGER
}
},
{ timestamps: false }
);
return todos;
};
Loading