GraphQL이란?

장광진·2024년 2월 13일

CS

목록 보기
5/13

GraphQL이란?

GraphQL은 FaceBook에서 개발된 오픈소스 기술로 데이터 질의(Query + Schema)언어이다.
클라이언트는 GraphQL 서버로 쿼리를 전송하고, 서버는 해당 쿼리를 해석하고 데이터를 반환한다.
이 때, 클라이언트가 요청한 필드만 반환되므로 over fetching을 줄여 효율적이다.
또한, GraphQL은 스키마를 사용하여 데이터 모델을 정의하기 때문에 클라이언트와 서버 간의 일관성 있는 데이터 통신을 보장한다.이를 통해 클라이언트가 서버가 제공하는 데이터 중 원하는 데이터를 가져오는 것이 가능해진다.

GraphQL의 장단점

장점

1. 오버페칭과 언더페칭을 해결할 수 있다.

2. 엔드포인트와 요청 형식을 고민하지 않아도 된다.

3. 클라이언트 로직이 간결해진다.

단점

1. HTTP 캐싱을 활용하기 어렵다.

2. 에러 핸들링이 어렵다.

예시

// REST API request
GET, https://swapi.dev/api/people/1

// REST API response
{
    "name": "Luke Skywalker",
    "height": "172",
    "mass": "77",
    "hair_color": "blond",
    "skin_color": "fair",
    "eye_color": "blue",
    "birth_year": "19BBY",
    "gender": "male",
    "homeworld": "http://swapi.dev/api/planets/1/",
    "films": ["http://swapi.dev/api/films/1/", "http://swapi.dev/api/films/2/", "http://swapi.dev/api/films/3/", "http://swapi.dev/api/films/6/"],
    "species": [],
    "vehicles": ["http://swapi.dev/api/vehicles/14/", "http://swapi.dev/api/vehicles/30/"],
    "starships": ["http://swapi.dev/api/starships/12/", "http://swapi.dev/api/starships/22/"],
    "created": "2014-12-09T13:50:51.644000Z",
    "edited": "2014-12-20T21:17:56.891000Z",
    "url": "http://swapi.dev/api/people/1/"
}
// GraphQL request
query {
    person(personID: 1) {
        name
        height
        mass
    }
}

// GraphQL response
{
    "data": {
        "person": {
            "name": "Luke Skywalker",
            "height": 172,
            "mass": 77
        }
    }
}
profile
점진적 과부하

0개의 댓글