Apollo로 GraphQL 서버 구축하기
Apollo Server를 사용하여 Node.js에 GraphQL API를 구현하고 데이터 소스에 연결하며 리졸버를 정의합니다.
Apollo로 GraphQL 서버 구축하기은(는) CoddyKit의 무료 Node.js Backend Development Bootcamp 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Node.js Backend Development Bootcamp 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Node.js Backend Development Bootcamp 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Meet Apollo Server
Welcome to building a GraphQL server! We'll use Apollo Server, a popular open-source library that helps you implement a production-ready GraphQL API in Node.js.
- It's easy to set up.
- It integrates with many Node.js frameworks like Express.
- It provides powerful tools like GraphQL Playground for testing.
Apollo Server handles the heavy lifting, letting you focus on your data.

Project Setup for Apollo
Let's get started by setting up our project. Open your terminal and run these commands to create a new Node.js project and install the required libraries:
apollo-server: The core library for building our GraphQL server.graphql: The JavaScript implementation of the GraphQL specification.
Here's how to do it:
mkdir my-graphql-api
cd my-graphql-api
npm init -y
npm install apollo-server graphql
Once installed, you're ready to write some code!
Schema & Resolvers: The Core
A GraphQL server has two main parts:
- Schema (Type Definitions): This defines the shape of your data and the operations clients can perform (queries, mutations). Think of it as the contract between client and server.
- Resolvers: These are functions that tell GraphQL how to fetch the data for each type and field defined in your schema. They are the "backend logic" for your API.
Together, they define your API's capabilities and how it interacts with your data.
Crafting Your GraphQL Schema
We define the schema using a special template literal tag called gql (from apollo-server). This allows us to write GraphQL Schema Definition Language (SDL).
The most important type is Query, which defines all the ways clients can read data from your API. Let's define a simple Query that returns a "hello" message.
const { gql } = require('apollo-server');
const typeDefs = gql`
type Query {
hello: String
}
`;
console.log('Your schema is ready!');Building Your Resolvers
Now that we have our schema, we need to tell GraphQL how to actually get the data for the hello field. This is where resolvers come in.
Resolvers are functions that match the fields in your schema. When a client queries hello, the corresponding resolver function will be executed.
const resolvers = {
Query: {
hello: () => 'Hello from your GraphQL API!',
},
};
console.log('Resolvers are defined!');Launching Your First Server
Let's put it all together! We'll combine the typeDefs and resolvers with ApolloServer to create a complete, runnable GraphQL server.
Create a file named index.js and add the following code. Then, run it with node index.js.
const { ApolloServer, gql } = require('apollo-server');
// 1. Define your schema (type definitions)
const typeDefs = gql`
type Query {
hello: String
}
`;
// 2. Define your resolvers (how to fetch data)
const resolvers = {
Query: {
hello: () => 'Hello from your GraphQL API!',
},
};
// 3. Create an Apollo Server instance
const server = new ApolloServer({ typeDefs, resolvers });
// 4. Start the server
server.listen({ port: 4000 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
console.log('Try opening your browser to test it!');
});Defining Custom Data Types
A simple "hello" isn't very exciting. Let's define a custom type, like Book, with its own fields. This shows how you structure more complex data.
Update your typeDefs to include the Book type and a books query that returns a list of them.
const { gql } = require('apollo-server');
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
hello: String
books: [Book]
}
`;
console.log('Schema updated with Book type!');Resolving Custom Types
To make the books query work, we need to update our resolvers. For now, we'll use an in-memory array of objects to simulate a database. In real applications, resolvers connect to databases or other APIs.
Let's update the resolvers to provide data for our books query.
const books = [
{
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
},
{
title: 'To Kill a Mockingbird',
author: 'Harper Lee',
},
];
const resolvers = {
Query: {
hello: () => 'Hello from your GraphQL API!',
books: () => books,
},
};
console.log('Resolvers updated for books!');Full API with Books
Here's the complete index.js file with our new Book type and its resolver. Run this with node index.js.
Once running, open the URL in your browser (e.g., http://localhost:4000). You'll see the GraphQL Playground, where you can test queries like:
query {
books {
title
author
}
}
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type Book {
title: String
author: String
}
type Query {
hello: String
books: [Book]
}
`;
const books = [
{
title: 'The Great Gatsby',
author: 'F. Scott Fitzgerald',
},
{
title: 'To Kill a Mockingbird',
author: 'Harper Lee',
},
];
const resolvers = {
Query: {
hello: () => 'Hello from your GraphQL API!',
books: () => books,
},
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen({ port: 4000 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
console.log('Explore your API in GraphQL Playground!');
});Schema vs. Resolvers Check
You've learned about the two fundamental parts of a GraphQL server: the schema (type definitions) and the resolvers.
Which of the following best describes the primary role of GraphQL Resolvers in an Apollo Server?
Apollo Server: Quick Recap
Great job! You've successfully built your first GraphQL server with Apollo Server.
- We installed
apollo-serverandgraphql. - We defined our API's structure using Type Definitions (
typeDefs) written in GraphQL SDL. - We implemented Resolvers to specify how to fetch data for the fields in our schema, using mock data.
- We launched our server using
ApolloServerand explored it with GraphQL Playground.
This is the foundation for building powerful and flexible GraphQL APIs. Next, you can explore mutations, arguments, and connecting to real databases!
자주 묻는 질문
“Apollo로 GraphQL 서버 구축하기” 강의는 무료인가요?
네 — “Apollo로 GraphQL 서버 구축하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Node.js Backend Development Bootcamp 강의 전체를 잠금 해제할 수 있습니다. Node.js Backend Development Bootcamp 강의에는 총 4개의 강의가 포함되어 있습니다.
“Apollo로 GraphQL 서버 구축하기”에서 뭘 배우나요?
Apollo Server를 사용하여 Node.js에 GraphQL API를 구현하고 데이터 소스에 연결하며 리졸버를 정의합니다. 브라우저에서 직접 실행하는 실습 코드로 Node.js Backend Development Bootcamp을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Node.js Backend Development Bootcamp을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Node.js Backend Development Bootcamp은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“Apollo로 GraphQL 서버 구축하기” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Node.js Backend Development Bootcamp 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Node.js Backend Development Bootcamp 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Node.js 서버리스 입문
- GraphQL API 설계 원칙
- Apollo로 GraphQL 서버 구축하기
- 실시간 데이터를 위한 GraphQL 구독