GraphQL+Mongodb.간단한 방법.
                                            
                                                
                                                
                                                
                                                
                                                
                                                 38193 단어  graphqlmongodbjavascriptnode
                    
얘 여기 있어!새 버전:
안녕하세요!저는 알바로라고 합니다. 이것은 제가 여기 있는 첫 번째 댓글입니다.나는 줄곧 다른 사이트에서 글을 썼다. 예를 들면medium이다.
하지만!지금 나는 여기에 있다. 나는 여기에 한동안 머물고 싶다.
지난 몇 달 동안 GraphQL을 해 왔는데 정말 좋아해요.
오늘 우리는 공부할 것이다.
응용 프로그램에서 우리는 인증을 받은 사용자를 가질 것입니다. 이렇게 해야만 댓글을 만들 수 있습니다.
우리 시작합시다!
1、babel로 노드 설정 
mkdir graphql-test && cd graphql-test
yarn init -y
yarn add --dev nodemon @babel/core @babel/node @babel/preset-env
나는 실을 사용하지만, 너는 npm를 사용할 수 있다. 
만들다.루트 디렉토리에 LRC 파일을 만들고 이 구성을 pase합니다.
{
  "presets": ["@babel/preset-env"]
}
2. 파일 및 디렉토리 조직 만들기 
mkdir graphql-test && cd graphql-test
yarn init -y
yarn add --dev nodemon @babel/core @babel/node @babel/preset-env
{
  "presets": ["@babel/preset-env"]
}
yarn add mongoose jsonwebtoken bcrypt express graphql cors apollo-server apollo-server-express
{
  "name": "graphql-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "dev": "nodemon --exec babel-node src/index.js"
  },
  "devDependencies": {
    "@babel/core": "^7.4.5",
    "@babel/node": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "apollo-server": "^2.6.1",
    "apollo-server-express": "^2.6.1",
    "bcrypt": "^3.0.6",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "graphql": "^14.3.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.5.12",
    "nodemon": "^1.19.1"
  }
}
3. 몬고 모형 만들기 
GraphQL에 집중하고 싶다면 모든 몬고의 속도를 높여야 합니다.
모델 내부에서 userModel 및 postModel 만들기:
모델 후.회사 명
import mongoose from 'mongoose';
const postSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },
  content: {
    type: String,
    required: true,
  },
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user',
  },
});
const post = mongoose.model('post', postSchema);
export default post;
사용자 모델.회사 명
import bcrypt from 'bcrypt';
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
  posts: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'post',
    },
  ],
});
userSchema.pre('save', function() {
  const hashedPassword = bcrypt.hashSync(this.password, 12);
  this.password = hashedPassword;
});
const user = mongoose.model('user', userSchema);
export default user;
4. 우리 모드 만들기 
/src/schema에서 postSchema를 만듭니다.js와 userSchema.회사 명
import { gql } from 'apollo-server';
export default gql`
  type Post {
    id: ID!
    title: String!
    content: String!
    author: User!
  }
  extend type Query {
    post(id: ID!): Post!
    posts: [Post!]!
  }
  extend type Mutation {
    createPost(title: String!, content: String!): Post!
  }
`;
import { gql } from 'apollo-server';
export default gql`
  type User {
    id: ID!
    name: String!
    posts: [Post!]!
  }
  type Token {
    token: String!
  }
  extend type Query {
    user(id: ID!): User!
    login(name: String!, password: String!): Token!
  }
  extend type Mutation {
    createUser(name: String!, password: String!): User!
  }
`;
import mongoose from 'mongoose';
const postSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true,
  },
  content: {
    type: String,
    required: true,
  },
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user',
  },
});
const post = mongoose.model('post', postSchema);
export default post;
import bcrypt from 'bcrypt';
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
  posts: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'post',
    },
  ],
});
userSchema.pre('save', function() {
  const hashedPassword = bcrypt.hashSync(this.password, 12);
  this.password = hashedPassword;
});
const user = mongoose.model('user', userSchema);
export default user;
/src/schema에서 postSchema를 만듭니다.js와 userSchema.회사 명
import { gql } from 'apollo-server';
export default gql`
  type Post {
    id: ID!
    title: String!
    content: String!
    author: User!
  }
  extend type Query {
    post(id: ID!): Post!
    posts: [Post!]!
  }
  extend type Mutation {
    createPost(title: String!, content: String!): Post!
  }
`;
import { gql } from 'apollo-server';
export default gql`
  type User {
    id: ID!
    name: String!
    posts: [Post!]!
  }
  type Token {
    token: String!
  }
  extend type Query {
    user(id: ID!): User!
    login(name: String!, password: String!): Token!
  }
  extend type Mutation {
    createUser(name: String!, password: String!): User!
  }
`;
import userSchema from './user';
import postSchema from './post';
import { gql } from 'apollo-server';
const linkSchema = gql`
  type Query {
    _: Boolean
  }
  type Mutation {
    _: Boolean
  }
`;
export default [linkSchema, userSchema, postSchema];
5. 우리의 해상도를 만듭니다 
패턴과 마찬가지로, 우리는 후분해기를 만들었다.js 및 userResolverssrc/resolvers의 js
import { AuthenticationError } from 'apollo-server';
export default {
  Query: {
    post: async (parent, { id }, { models: { postModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const post = await postModel.findById({ _id: id }).exec();
      return post;
    },
    posts: async (parent, args, { models: { postModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const posts = await postModel.find({ author: me.id }).exec();
      return posts;
    },
  },
  Mutation: {
    createPost: async (parent, { title, content }, { models: { postModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const post = await postModel.create({ title, content, author: me.id });
      return post;
    },
  },
  Post: {
    author: async ({ author }, args, { models: { userModel } }, info) => {
      const user = await userModel.findById({ _id: author }).exec();
      return user;
    },
  },
};
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { AuthenticationError } from 'apollo-server';
export default {
  Query: {
    user: async (parent, { id }, { models: { userModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const user = await userModel.findById({ _id: id }).exec();
      return user;
    },
    login: async (parent, { name, password }, { models: { userModel } }, info) => {
      const user = await userModel.findOne({ name }).exec();
      if (!user) {
        throw new AuthenticationError('Invalid credentials');
      }
      const matchPasswords = bcrypt.compareSync(password, user.password);
      if (!matchPasswords) {
        throw new AuthenticationError('Invalid credentials');
      }
      const token = jwt.sign({ id: user.id }, 'riddlemethis', { expiresIn: 24 * 10 * 50 });
      return {
        token,
      };
    },
  },
  Mutation: {
    createUser: async (parent, { name, password }, { models: { userModel } }, info) => {
      const user = await userModel.create({ name, password });
      return user;
    },
  },
  User: {
    posts: async ({ id }, args, { models: { postModel } }, info) => {
      const posts = await postModel.find({ author: id }).exec();
      return posts;
    },
  },
};
import { AuthenticationError } from 'apollo-server';
export default {
  Query: {
    post: async (parent, { id }, { models: { postModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const post = await postModel.findById({ _id: id }).exec();
      return post;
    },
    posts: async (parent, args, { models: { postModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const posts = await postModel.find({ author: me.id }).exec();
      return posts;
    },
  },
  Mutation: {
    createPost: async (parent, { title, content }, { models: { postModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const post = await postModel.create({ title, content, author: me.id });
      return post;
    },
  },
  Post: {
    author: async ({ author }, args, { models: { userModel } }, info) => {
      const user = await userModel.findById({ _id: author }).exec();
      return user;
    },
  },
};
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { AuthenticationError } from 'apollo-server';
export default {
  Query: {
    user: async (parent, { id }, { models: { userModel }, me }, info) => {
      if (!me) {
        throw new AuthenticationError('You are not authenticated');
      }
      const user = await userModel.findById({ _id: id }).exec();
      return user;
    },
    login: async (parent, { name, password }, { models: { userModel } }, info) => {
      const user = await userModel.findOne({ name }).exec();
      if (!user) {
        throw new AuthenticationError('Invalid credentials');
      }
      const matchPasswords = bcrypt.compareSync(password, user.password);
      if (!matchPasswords) {
        throw new AuthenticationError('Invalid credentials');
      }
      const token = jwt.sign({ id: user.id }, 'riddlemethis', { expiresIn: 24 * 10 * 50 });
      return {
        token,
      };
    },
  },
  Mutation: {
    createUser: async (parent, { name, password }, { models: { userModel } }, info) => {
      const user = await userModel.create({ name, password });
      return user;
    },
  },
  User: {
    posts: async ({ id }, args, { models: { postModel } }, info) => {
      const posts = await postModel.find({ author: id }).exec();
      return posts;
    },
  },
};
우리는 데이터를 서로 다른 집합에 저장하기 때문에 이렇게 해야 한다.
보시다시피 해상도는 4개의 매개 변수 (parent,args,context,info) 가 있는 함수입니다.
아버지: 부모 분석 프로그램에서 데이터를 되돌려줍니다.예: 우리는 사고 조회 > 사용자 > 게시물에 들어갑니다.게시물은 모든 데이터를 상위 매개변수로 사용자에게 반환합니다.
args: 우리가 조회/변이에 사용하는 매개 변수가 있습니다.만약 우리가 모드를 보았다면 발표(id:id!):우편으로 배달하다매개 변수가 하나 있습니다, id.
상하문: 상하문은 하나의 대상이다. 이것은 우리가 서버 설정에서 전달하는 모든 내용을 포함할 것이다. 우리의 예시에서 사용자와post에 사용되는 demongo모델과'me', 즉 현재 로그인한 사용자를 포함한다.
정보: 더 복잡합니다. 프리스마는 여기서 깊이 파고듭니다. https://www.prisma.io/blog/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a
우리가 패턴에 대해 한 것처럼 색인을 만듭니다.src/resolvers 내의 js:
import postResolver from './postResolver';
import userResolver from './userResolver';
export default [userResolver, postResolver];
6 모든 설정 
마지막으로, 우리의 색인에서.src/폴더의 js:
import cors from 'cors';
import express from 'express';
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import { ApolloServer, AuthenticationError } from 'apollo-server-express';
import schemas from './schemas';
import resolvers from './resolvers';
import userModel from './models/userModel';
import postModel from './models/postModel';
const app = express();
app.use(cors());
const getUser = async (req) => {
  const token = req.headers['token'];
  if (token) {
    try {
      return await jwt.verify(token, 'riddlemethis');
    } catch (e) {
      throw new AuthenticationError('Your session expired. Sign in again.');
    }
  }
};
const server = new ApolloServer({
  typeDefs: schemas,
  resolvers,
  context: async ({ req }) => {
    if (req) {
      const me = await getUser(req);
      return {
        me,
        models: {
          userModel,
          postModel,
        },
      };
    }
  },
});
server.applyMiddleware({ app, path: '/graphql' });
app.listen(5000, () => {
  mongoose.connect('mongodb://localhost:27017/graphql');
});
import cors from 'cors';
import express from 'express';
import jwt from 'jsonwebtoken';
import mongoose from 'mongoose';
import { ApolloServer, AuthenticationError } from 'apollo-server-express';
import schemas from './schemas';
import resolvers from './resolvers';
import userModel from './models/userModel';
import postModel from './models/postModel';
const app = express();
app.use(cors());
const getUser = async (req) => {
  const token = req.headers['token'];
  if (token) {
    try {
      return await jwt.verify(token, 'riddlemethis');
    } catch (e) {
      throw new AuthenticationError('Your session expired. Sign in again.');
    }
  }
};
const server = new ApolloServer({
  typeDefs: schemas,
  resolvers,
  context: async ({ req }) => {
    if (req) {
      const me = await getUser(req);
      return {
        me,
        models: {
          userModel,
          postModel,
        },
      };
    }
  },
});
server.applyMiddleware({ app, path: '/graphql' });
app.listen(5000, () => {
  mongoose.connect('mongodb://localhost:27017/graphql');
});
7. 신생아 테스트.
http://localhost:5000/graphql
로그인 사용자
헤더에 영패를 설치하다
게시물 작성
게시물 조회
나는 네가 나처럼 이것을 좋아하길 바란다.언제든지 연락 주세요!만약 네가 더 좋은 설명을 얻고 싶다면 얼마든지 물어봐라, 나는 매우 기쁘다.
Reference
이 문제에 관하여(GraphQL+Mongodb.간단한 방법.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/alvarojsnish/graphql-mongodb-the-easy-way-ngc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)