Skip to content

Commit

Permalink
Moved data responsibilities to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
jurosh committed Oct 3, 2017
1 parent 9b3015d commit 5c3cf1a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"prettier.singleQuote": true,
"editor.formatOnSave": true
}
28 changes: 28 additions & 0 deletions src/data/people.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const uuid = require('uuid/v4');

const data = {
1: {
id: 1,
name: 'Juraj',
age: 45
},
2: {
id: 2,
name: 'Peto',
age: 15
}
};

module.exports = {
getPeople: () => Object.values(data),
getPerson: id => data[id],
createPerson: (name, age) => {
const newPerson = {
id: uuid(),
name,
age
};
data[newPerson.id] = newPerson;
return newPerson;
}
};
19 changes: 5 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const express = require('express');
const graphqlHTTP = require('express-graphql');
const { buildSchema } = require('graphql');
const person = require('./personData');
const uuid = require('uuid/v4');
const { createPerson, getPeople, getPerson } = require('./data/people');

const PORT = process.env.PORT || 80;
const PORT = process.env.PORT || 81;

// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
Expand All @@ -27,18 +26,10 @@ const schema = buildSchema(`
const root = {
// Queries
hello: () => 'Hello world!',
people: () => Object.values(person),
person: ({ id }) => person[id],
people: () => getPeople(),
person: ({ id }) => getPerson(id),
// Mutations
createPerson: ({ name, age }) => {
const newPerson = {
id: uuid(),
name,
age
};
person[newPerson.id] = newPerson;
return newPerson;
}
createPerson: ({ name, age }) => createPerson(name, age)
};

const app = express();
Expand Down
12 changes: 0 additions & 12 deletions src/personData.js

This file was deleted.

0 comments on commit 5c3cf1a

Please sign in to comment.