이전글 이어서 작성
2022.07.13 - [2022/JavaScript EveryWhere] - express, apollo-server-express, graphql 연동
npm install mongoose dotenv
package.json
"dependencies": {
"dotenv": "^16.0.1",
"mongoose": "^6.4.4",
}
db는 몽고디비 아래주소로 설치하거나 도커로 설치하기
1. https://www.mongodb.com/try/download/community
2. 윈도우 도커로 설치
index.js
// .env 파일 내용 불러오기
require('dotenv').config();
const db = require('./db');
// const models = require('./models');
...
const DB_HOST = process.env.DB_HOST;
...
db.connect(DB_HOST);
db.js
const mongoose = require('mongoose')
module.exports = {
connect: DB_HOST => {
// Connect to the DB
mongoose.connect(DB_HOST);
// Log an error if we fail to connect
mongoose.connection.on('error', err => {
console.error(err);
console.log(
'MongoDB connection error. Please make sure MongoDB is running.'
);
process.exit();
});
},
close: () => {
mongoose.connection.close();
}
};
/*
No More Deprecation Warning Options
useNewUrlParser, useUnifiedTopology, useFindAndModify, and useCreateIndex are no longer supported options.
Mongoose 6 always behaves as if useNewUrlParser, useUnifiedTopology, and useCreateIndex are true, and useFindAndModify is false.
Please remove these options from your code.
*/
// https://mongoosejs.com/docs/migrating_to_6.html#no-more-deprecation-warning-options
.env
DB_HOST=mongodb://docker:mongopw@localhost:49154
index.js 전체소스
더보기
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
require('dotenv').config();
const db = require('./db');
// const models = require('./models');
// Run the server on a port specified in our .env file or port 4000
const port = process.env.PORT || 4000;
const DB_HOST = process.env.DB_HOST;
let notes = [
{
id: '1',
content: 'This is a note',
author: 'Adam Scott'
},
{
id: '2',
content: 'This is another note',
author: 'Harlow Everly'
},
{
id: '3',
content: 'Oh hey look, another note!',
author: 'Riley Harrison'
}
];
// 그래프QL 스키마 언어로 스키마를 구성
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 world!',
notes: () => notes,
note: (parent, args) => {
return notes.find(note => note.id === args.id);
}
},
Mutation: {
newNote: (parent, args) => {
let noteValue = {
id: String(notes.length + 1),
content: args.content,
author: 'Adam Scott'
};
notes.push(noteValue);
return noteValue;
}
}
};
// 아폴로 서버 설정
const server = new ApolloServer({ typeDefs, resolvers });
const app = express();
db.connect(DB_HOST);
server.start().then(res => {
// 아폴로 그래프QL 미들웨어를 적용하고 경로를 /api 로 설정
server.applyMiddleware({ app, path: '/api' });
app.listen({ port }, () =>
console.log(
`GraphQL Server running at http://localhost:${port}${server.graphqlPath}`
)
);
})
// https://javascriptsu.wordpress.com/2021/08/02/apollo-error-must-await-server-start/
반응형
'2022 > JavaScript EveryWhere' 카테고리의 다른 글
express, apollo-server-express, graphql 연동 (0) | 2022.07.13 |
---|