[graphQL] SDL 정리

seokki kwon·2022년 9월 14일
0

Apollo Server

흐름

아폴로클라이언트 -> 아폴로서버 -> 구문분석 -> 리졸버 -> 값리턴

type

type User {
	status: String!
    success: Boolean!
    error: String!
	}

id 와 username 을 반환하는 타입지정

Query

Query {
 	 login(username: String! password: String!):
     User
}

Mutation

type Mutation {
	addBook(title: String, author: String):Book
}

addBook 이라는 리졸버는 type Book 이라는 결과값을 받응

클라이언트와 함께사용

아폴로클라이언트 에서 gql로 지정하여 useQuery 또는 useLazyQuery 등으로 쿼리를 날림
//client`

query EventList {
  upcomingEvents {
    name
    date
    location {
      name
      weather {
        temperature
        description
      }
    }
  }
}

//server

type Query {
  upcomingEvents: [Event!]!
}

type Event {
  name: String!
  date: String!
  location: Location
}

type Location {
  name: String!
  weather: WeatherInfo
}

type WeatherInfo {
  temperature: Float
  description: String
}

Mutaion 변수

mutation updateMyUser {
  updateUserEmail(id: 1, email: "jane@example.com"){
    id
    name
    email
  }
}

클라이언트에서 쿼리를 보내면 해당 리졸버를 실행하고 리졸버의 결과값을 클라이언트에 리턴한다.

인터페이스 구조화

서버로 부터 요청이 이루어지고 예기치 오류를 대비하여 항상 일관된 값을 전달하는게 중요

interface MutationResponse {
  code: String!
  success: Boolean!
  message: String!
}


type UpdateUserEmailMutationResponse implements MutationResponse {
  code: String!
  success: Boolean!
  message: String!
  user: User
}

응답 JSON

{
  "data": {
    "updateUser": {
      "code": "200",
      "success": true,
      "message": "User email was successfully updated",
      "user": {
        "id": "1",
        "name": "Jane Doe",
        "email": "jane@example.com"
      }
    }
  }
}

\

code는 상태값이며
success 는 처리결과의 성공유무
message 는 처리 결과의 메세지이며 클라이언트 측에서 ui로 쓰일수 있다.

profile
웹 & 앱개발 기록

0개의 댓글