[nodejs] GraphQL 쿼리 언어 중첩 객체 조회 예시
GraphQL은 클라이언트가 서버로부터 특정 필드의 데이터를 요청할 수 있는 강력한 쿼리 언어입니다. 중첩된 객체를 조회하는 예시를 살펴보겠습니다.
예시 코드
query {
user(id: "123") {
name
email
posts {
title
content
comments {
text
user {
name
avatar
}
}
}
}
}
위의 코드는 사용자의 이름, 이메일 및 해당 사용자가 작성한 게시물의 제목, 내용, 그리고 댓글과 댓글 작성자의 이름 및 아바타를 조회하는 GraphQL 쿼리입니다.
결과
서버가 이 쿼리에 대한 응답으로 다음과 같은 중첩된 객체의 데이터를 반환할 수 있습니다.
{
"user": {
"name": "John Doe",
"email": "john@example.com",
"posts": [
{
"title": "First post",
"content": "This is the content of the first post",
"comments": [
{
"text": "Great post!",
"user": {
"name": "Jane Smith",
"avatar": "https://example.com/avatar.jpg"
}
},
{
"text": "Keep up the good work!",
"user": {
"name": "Sam Williams",
"avatar": "https://example.com/avatar2.jpg"
}
}
]
},
{
"title": "Second post",
"content": "This is the content of the second post",
"comments": [
{
"text": "Interesting read!",
"user": {
"name": "Megan Brown",
"avatar": "https://example.com/avatar3.jpg"
}
}
]
}
]
}
}
이렇게 중첩된 객체를 조회하는 GraphQL 쿼리를 사용하여 원하는 데이터를 쉽게 얻을 수 있습니다.