Skip to content

Commit

Permalink
Separate schema and resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
lif3ng committed Aug 24, 2021
1 parent 820ca7a commit cc7925c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
43 changes: 9 additions & 34 deletions src/index.js
@@ -1,5 +1,8 @@
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { ApolloServer } = require('apollo-server-express');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');

require('dotenv').config();

const db = require('./db');
Expand All @@ -9,43 +12,15 @@ const port = process.env.PORT || 4000;

const DB_HOST = process.env.DB_HOST;

const typeDefs = gql`
type Note {
id: ID!
content: String!
author: String!
}
type Query {
hello: String!
notes: [Note!]!
note(id: ID!): Note!
}
type Mutation {
newNote(content: String!): Note!
}
`;

const resolvers = {
Query: {
hello: () => 'hello',
notes: async () => await models.Note.find(),
note: async (parent, args) => await models.Note.findById(args.id)
},
Mutation: {
newNote: async (parent, args) =>
await models.Note.create({
content: args.content,
author: 'a author str'
})
}
};

const app = express();

db.connect(DB_HOST);

const server = new ApolloServer({ typeDefs, resolvers });
const server = new ApolloServer({
typeDefs,
resolvers,
context: () => ({ models })
});
server.applyMiddleware({ app, path: '/api' });
app.listen({ port }, () => {
console.log(
Expand Down
7 changes: 7 additions & 0 deletions src/resolvers/index.js
@@ -0,0 +1,7 @@
const Query = require('./query');
const Mutation = require('./mutation');

module.exports = {
Query,
Mutation
};
7 changes: 7 additions & 0 deletions src/resolvers/mutation.js
@@ -0,0 +1,7 @@
module.exports = {
newNote: async (parent, args, { models }) =>
await models.Note.create({
content: args.content,
author: 'a author str'
})
};
5 changes: 5 additions & 0 deletions src/resolvers/query.js
@@ -0,0 +1,5 @@
module.exports = {
hello: () => 'hello',
notes: async (parent, args, { models }) => await models.Note.find(),
note: async (parent, args, { models }) => await models.Note.findById(args.id)
};
18 changes: 18 additions & 0 deletions src/schema.js
@@ -0,0 +1,18 @@
const { gql } = require('apollo-server-express');

module.exports = gql`
type Note {
id: ID!
content: String!
author: String!
}
type Query {
hello: String!
notes: [Note!]!
note(id: ID!): Note!
}
type Mutation {
newNote(content: String!): Note!
}
`;

0 comments on commit cc7925c

Please sign in to comment.