[GraphQL] Movie API 만들기(4)-Mutation

bomhada·2022년 5월 8일
0

GraphQL

목록 보기
4/5
post-thumbnail

👾 Mutation-add Movie

  • mutation은 Database 상태가 변할 때 사용되는 것입니다.
  • 개발자가 원하는 만큼 정의할 수 있고, 얼마나 많은 type을 정의했는지 상관하지 않습니다.
    하지만, Graphql에게 Mutaion이나 Query를 요청하길 원한다면 그것들을 type Query와 type Mutation에 넣어야합니다.

schema.js

import { buildSchema } from "graphql";

const schema = buildSchema(`
  type Movie {
    id: Int!
    title: String!
    year: Int!
  }
  
  type Query {
    movies: [Movie]!
    movie(id: Int!): Movie
  }
  
  type Mutation {
    addMovie(title: String!, year: Int!): Movie!
  }
`);

export default schema;
  • type Mutation에 id의 타입을 넣지 않은 이유는 Database에 의해 자동적으로 id가 생성되도록 작성할 예정입니다.

db.js

let movies = [
  {
    id: 0,
    title: lalaland,
    year: 2016
  },
  {
    id: 1,
    title: whiplash,
    year: 2014
  },
  {
    id: 2,
    title: begin again,
    year: 2013
  },
  {
    id: 3,
    title: interstella,
    year: 2014
  },
];

export const getMovies = () => movies;

export cosnt getById = id => {
  const filteredMovies = movies.filter(movie => movie.id === id);
  return filteredMovies[0];
}

export const addMovie = (title, year) => {
  const newMovie = {
    id: `${movies.length + 1}`,
    title,
    year
  }
  movies.push(newMovie);
  return newMovie;
}
  • 새로운 객체들이 추가될 수 있도록 let으로 변수를 선언해주세요.
  • People에서 person으로 걸렀던 함수와 비슷하게 getById 함수를 작성하였습니다.
  • addMovie는 일반 js문법으로 작성했습니다.
  • title과 year 값을 받아서 newMovie 객체를 작성해 그 안에 title과 year을 담아줍니다.
    id는 객체 길이에서 +1을 하여 자동으로 생성되도록 하였습니다.
  • 그리고 새로 만든 newMovie를 movies에 넣은 뒤 리턴해줍니다.

resolvers.js

import { getMovies, getById, addMovie } from "./db";

const resolvers = {
  Query: {
    movies: () => getMovies(),
    movie: (_, { id }) => getById(id)
  },
  Mutation: {
    addMovie: (_, { title, year }) => addMovie(title, year)
  },
}

playground
영화 추가하기

mutation {
  addMovie(title: "soul", year: 2020) {
    title,
    year
  }
}

추가한 영화 확인하기

query {
  movies {
    title,
    year
  }
}
  • 위와 같이 작성을 하면 이제 playground에서 addMovie를 이용해 새로운 영화 정보를 추가할 수 있게됩니다.
  • Query를 적지 않아도 Query가 defaul로 내제되어있기 때문에{}만 작성해도 됩니다.

👾 Mutation-delete Movie

schema.js

import { buildSchema } from "graphql";

const schema = buildSchema(`
  type Movie {
    .
  }
  
  type Query {
    .
  }
  
  type Mutation {
    addMovie(title: String!, year: Int!): Movie!
    deleteMovie(id: Int!): Boolean!
  }
`);

export default schema;

db.js

let movies = [
  .
  .
  .
];

export const getMovies = () => movies;

export cosnt getById = id => {
  .
}

export const addMovie = (title, year) => {
  .
}

export const deleteMovie = id => {
  const cleanedMovies = movies.filter(movie => movie.id !== id);
  if (movies.length > cleanedMovies.length) {
    movies = cleanedMovies;
    return true;
  } else {
    return false;
  }
}
  • id를 argument로 받고, 그 id를 사용하여 delete를 할 수 있습니다.
  • cleanedMovies는 같은 id를 가지지 않은 movie들을 담아놓습니다.
  • movies 배열의 길이가 cleanedMovies배열의 길이보다 길면 movies = cleanedMovies를 수행하고 true를 반환합니다.

resolvers.js

import { getMovies, getById, addMovie, deleteMovie } from "./db";

const resolvers = {
  Query: {
    .
  },
  Mutation: {
    addMovie: (_, { title, year }) => addMovie(title, year),
    deleteMovie: (_, { id }) => deleteMovie(id)
  },
}

playground
영화 삭제하기

mutation {
  deleteMovie(id: 3)
}

0개의 댓글