๐ฆซ
46์ผ์ฐจ
Part 14. GraphQL ๋ง๋ณด๊ธฐ (Server ๋ง๋ค๊ธฐ)
Part 14. GraphQL ๋ง๋ณด๊ธฐ (Server ๋ง๋ค๊ธฐ)
https://slides.com/woongjae/graphql2021
GraphQL 2021
๊ทธ๋ํํ์ 2021
slides.com
GraphQL | A query language for your API
Evolve your APIwithout versions Add new fields and types to your GraphQL API without impacting existing queries. Aging fields can be deprecated and hidden from tools. By using a single evolving version, GraphQL APIs give apps continuous access to new featu
graphql.org
๐ Apollo ๋ฅผ ์ด์ฉํด์ Node.js ์๋ฒ ๋ง๋ค๊ธฐ
https://github.com/apollographql/apollo-server
GitHub - apollographql/apollo-server: ๐ Spec-compliant and production ready JavaScript GraphQL server that lets you develop
๐ Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more. - GitHub - apollographql/apollo-server...
github.com
$ npm i apollo-server graphql
$ npm i nodemon -D
// ./index.js
const { ApolloServer, gql } = require("apollo-server");
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => "world",
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true, // โญ
});
server.listen().then(({ url }) => {
console.log(`๐ Server ready at ${url}`);
});
// package.json
{
...
"scripts": {
"dev": "nodemon index.js" // $ npm run dev
},
...
}

๐ typeDef ์ resolver
- typeDef(s)
- GraphQL Schema ๋ฅผ ์ ์ํ๋ ๊ณณ
- โObject
- Query
- Mutation
- Input
- gpl ๊ณผ Tagged Template Literals ๋ก ์์ฑํ๋ค.
- GraphQL Schema ๋ฅผ ์ ์ํ๋ ๊ณณ
- resolver(s)
- Schema ์ ํด๋นํ๋ ๊ตฌํ์ ํ๋ ๊ณณโ
- ์์ฒญ์ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ์กฐํ, ์์ , ์ญ์
๐ ์ฌ๋ฌ๊ฐ์ ๋ฐ์ดํฐ ์กฐํํ๊ธฐ - Query
// ./index.js
const { ApolloServer, gql } = require("apollo-server");
const { readFileSync } = require("fs");
const { join } = require("path");
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
books: [Book]
}
type Book {
bookId: Int
title: String
message: String
author: String
url: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => "world",
books: () => {
return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`๐ Server ready at ${url}`);
});
// ./books.json
[
{
"bookId": 1,
"title": "titke test",
"message": "message test",
"author": "author test",
"url": "url test"
},
{
"bookId": 2,
"title": "titke test 2",
"message": "message test 2",
"author": "author test 2",
"url": "url test 2"
}
]

๐ ํน์ ๋ฐ์ดํฐ ์กฐํํ๊ธฐ - Query
// ./index.js
const { ApolloServer, gql } = require("apollo-server");
const { readFileSync } = require("fs");
const { join } = require("path");
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
books: [Book]
book(bookId: Int): Book
}
type Book {
bookId: Int
title: String
message: String
author: String
url: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => "world",
books: () => {
return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
},
book: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
return books.find((book) => book.bookId === args.bookId);
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`๐ Server ready at ${url}`);
});

๐ ๋ฐ์ดํฐ ์ถ๊ฐํ๊ธฐ - Mutation
// ./index.js
const { ApolloServer, gql } = require("apollo-server");
const { readFileSync, writeFileSync } = require("fs");
const { join } = require("path");
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
books: [Book]
book(bookId: Int): Book
}
type Mutation {
addBook(title: String, message: String, author: String, url: String): Book
}
type Book {
bookId: Int
title: String
message: String
author: String
url: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
// ๋ฐ์ดํฐ ์กฐํ
Query: {
hello: () => "world",
books: () => {
return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
},
book: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
return books.find((book) => book.bookId === args.bookId);
},
},
// ๋ฐ์ดํฐ ์ถ๊ฐ
Mutation: {
addBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
const maxId = Math.max(...books.map((book) => book.bookId));
const newBook = { ...args, bookId: maxId + 1 };
writeFileSync(
join(__dirname, "books.json"),
JSON.stringify([...books, newBook])
);
return newBook;
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`๐ Server ready at ${url}`);
});

// ./books.json
[{"bookId":1,"title":"titke test","message":"message test","author":"author test","url":"url test"},{"bookId":2,"title":"titke test 2","message":"message test 2","author":"author test 2","url":"url test 2"},{"title":"t","message":"m","author":"a","url":"u","bookId":3}]
๐ ๋ฐ์ดํฐ ๋ณ๊ฒฝํ๊ธฐ - Mutation
// ./index.js
const { ApolloServer, gql } = require("apollo-server");
const { readFileSync, writeFileSync } = require("fs");
const { join } = require("path");
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
books: [Book]
book(bookId: Int): Book
}
type Mutation {
addBook(title: String, message: String, author: String, url: String): Book
editBook(
bookId: Int
title: String
message: String
author: String
url: String
): Book
}
type Book {
bookId: Int
title: String
message: String
author: String
url: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
// ๋ฐ์ดํฐ ์กฐํ
Query: {
hello: () => "world",
books: () => {
return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
},
book: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
return books.find((book) => book.bookId === args.bookId);
},
},
// ๋ฐ์ดํฐ ์ถ๊ฐ & ๋ณ๊ฒฝ & ์ญ์
Mutation: {
// ์ถ๊ฐ
addBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
const maxId = Math.max(...books.map((book) => book.bookId));
const newBook = { ...args, bookId: maxId + 1 };
writeFileSync(
join(__dirname, "books.json"),
JSON.stringify([...books, newBook])
);
return newBook;
},
editBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
const newBooks = books.map((book) => {
if (book.bookId === args.bookId) {
return args;
} else {
return book;
}
});
writeFileSync(join(__dirname, "books.json"), JSON.stringify(newBooks));
return args;
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`๐ Server ready at ${url}`);
});

// ./package.json
[{"bookId":1,"title":"titke test","message":"message test","author":"author test","url":"url test"},{"bookId":2,"title":"titke test 2","message":"message test 2","author":"author test 2","url":"url test 2"},{"bookId":3,"title":"t1","message":"m","author":"a","url":"u"}]
๐ ๋ฐ์ดํฐ ์ญ์ ํ๊ธฐ - Mutation
// ./index.js
const { ApolloServer, gql } = require("apollo-server");
const { readFileSync, writeFileSync } = require("fs");
const { join } = require("path");
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
books: [Book]
book(bookId: Int): Book
}
type Mutation {
addBook(title: String, message: String, author: String, url: String): Book
editBook(
bookId: Int
title: String
message: String
author: String
url: String
): Book
deleteBook(bookId: Int): Book
}
type Book {
bookId: Int
title: String
message: String
author: String
url: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
// ๋ฐ์ดํฐ ์กฐํ
Query: {
hello: () => "world",
books: () => {
return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
},
book: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
return books.find((book) => book.bookId === args.bookId);
},
},
// ๋ฐ์ดํฐ ์ถ๊ฐ & ๋ณ๊ฒฝ & ์ญ์
Mutation: {
// ์ถ๊ฐ
addBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
const maxId = Math.max(...books.map((book) => book.bookId));
const newBook = { ...args, bookId: maxId + 1 };
writeFileSync(
join(__dirname, "books.json"),
JSON.stringify([...books, newBook])
);
return newBook;
},
editBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
const newBooks = books.map((book) => {
if (book.bookId === args.bookId) {
return args;
} else {
return book;
}
});
writeFileSync(join(__dirname, "books.json"), JSON.stringify(newBooks));
return args;
},
deleteBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, "books.json")).toString()
);
const deleted = books.find((book) => book.bookId === args.bookId);
const newBooks = books.filter((book) => book.bookId !== args.bookId);
writeFileSync(join(__dirname, "books.json"), JSON.stringify(newBooks));
return deleted;
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`๐ Server ready at ${url}`);
});

// ./package.json
[{"bookId":1,"title":"titke test","message":"message test","author":"author test","url":"url test"},{"bookId":2,"title":"titke test 2","message":"message test 2","author":"author test 2","url":"url test 2"}]
'๐ฌ > ใ ใ ใ ใ ใ ใ ์ฑ๋ฆฐ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
48์ผ์ฐจ (0) | 2022.03.30 |
---|---|
47์ผ์ฐจ (0) | 2022.03.29 |
45์ผ์ฐจ (0) | 2022.03.28 |
43, 44์ผ์ฐจ (0) | 2022.03.26 |
42์ผ์ฐจ (0) | 2022.03.26 |