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;
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;
}
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
}
}
{}
만 작성해도 됩니다.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;
}
}
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)
}