GraphQL 스키마에서는 조회 가능한 데이터의 유형과 그 조회문에서 사용 가능한 필드를 정의하고,
리졸버에서는 필드가 조회될 때 호출되는 함수를 정의합니다.
Schema : graphql의 구조를 정의
Query : 데이터 조회
Mutation: 등록, 수정, 삭제
localhost:3000/graphql
query {
humans {
humanName
blood
gender
age
phone
hobbies {
hobbyName
}
}
}
사람과 게시물을 동시에 조회
query {
humans {
humanName
blood
gender
age
phone
hobbies {
hobbyName
}
}
posts {
title
userId
category {
name
}
}
}
아래와 같은 경우 skp, limit, text는 특정조건들을 의미한다.
comments (
skip: Int
limit: Int
text: String
): [Comment]
위 조건에 따라 query ( ) 괄호안에 "조건"을 명시 할 수 있다.
1.
query(limit: 10) {
comments {
text
usrId
}
}
2.
query(text: "Bad") {
comments {
text
usrId
}
}
query {
posts { 글목록
title
userId
comments { 댓글
text
user { 유저정보
_id
posts { 유저한 작성한 글목록
title 게시글에 댓글을 단 유저가 작성한 게시글의 제목
}
}
}
}
}
Mutation
mutation {
mutation이름(입력값1: "값1", 입력값2: "값2) {
완료후 받을 필드값 1
완료후 받을 필드값 2
완료후 받을 필드값 3
}
}
완료후 받을값은 무조건 1개이상 조건해야 된다.
예시
mutation {
addPerson(humanName: "철수", blood: "A", gender: "male", age: 20, phoneNumber: "000000000"){
_id
humanName
age
}
}
Schema : graphql의 구조를 의미한다.
query { 스키마
humans { type Human {
humanName humanName: String
blood blood: Blood
gender gender: String
age age: Int
phone phone: String
hobbies { school: Student
hobbyName hobbies: [Hobby]
}
}
} type Query {
humans: [Human] <--리턴타입: 배열
}
스키마의 구성요소
scalar 스칼라타입
type 타입이름 { 사용자 정의 Type
...
}
type Query { 기본 Type
query1
query2
query3
...
}
type Mutation { 기본 Type
Mutation1
MUtation2
Mutation3
}
스키마의 구성요소 중 스칼라
스칼라 :
하나의 수치만으로 완전히 표시되는 양
더 이상 쪼개지지 않는 원자와 같은 타입
Int
Float
String
Boolean
ID
type Human {
humanName: String (스칼라 타입)
blood: Blood (스칼라 타입)
gender: String (스칼라 타입)
age: Int (스칼라 타입)
phone: String → 또 다른 사용자 정의 타입을 참조 스칼라 타입X
school: Student → 또 다른 사용자 정의 타입을 참조 스칼라 타입X
hobbies: [Hobby]
}
스칼라는 사용자가 임의적으로 추가가능
Scalar Date ←사용자가 임의적으로 정의
type Post {
title: String
createAt : Date (사용)
}
사용자 정의 타입
type Human {
...
school : school
hobbies: [Hobby] →결과가 배열
}
type Hobby {
hobbyName: String
}
type School {
_id: ID
name : String
humanType: String
location: String
}
//school의 결과 : 하나의 객체
"school" : {
"name" : "science",
"location" : "Gangu"
},
//hobby의 결과 : 여러 개의 객체가 배열에 포함
"hobbies" : [
{
"hobbyName" : "piano"
},
{
"hobbyName" : "tv"
}
]
type Human {
humanName: String! 호출 시 무조건 사용되어야 함을 의미
blood: Blood
sex: String
...
}
type Sample {
Tests: [test] 배열자체가 null이거나 배열의 요소가 null이어도 된다.
Tests: [test!] 배열자체가 null이어도 된다. 그러나 배열의 요소에 null이 있어서는 안된다.
Tests: [test]! 배열의 요소가 null이어도 된다. 배열자체가 null일수는 없다.
Tests: [test!]!
Query타입을 정의하는 방법
type Query {
humans: [Human] ←리턴타입이 배열
belongs: [BelongTo]
people(humanName: String) : Person 리턴타입이 Person객체, ( ) 쿼리조건
schools: [School]
wokers: [Worker]
}
type Mutation{
addPerson(humanName: String, blood: String, age: Int, sex: String, phone:String): Person
}
--------------------------------------------
schema의 고급기능
interface : 중복해서 사용해야 하는 필수 타입 정의
인터페이스를 참조하는 경우 반드시 그 참조한 필드를 "type정의시" 꼭 사용해야 된다.
query에서 꼭 사용할 필요X
interface TypeCommon {
_id: ID
name: String
}
type Worker implements TypeCommon {
_id: ID
name: String
humanType: String
}
type School implements TypeCommon {
_id: ID
name: String
humanType: String
location: String
}
--------------------------------------------------
union
특별한 형태의 타입선언
union BelongTo = School | Worker
type Worker implements TypeCommon {
_id: ID
name: String
humanType: String
}
type School implements TypeCommon {
_id: ID
name: String
humanType: String
locationn: String
}
type Query {
belongs: [BelongTo] → 서로 다른 객체(School 또는 Worker) 배열로 리턴됨
}
실제 Query작성
query {
belongs {
...on Worker {
name
humanType
}
...on School {
name
humanType
location
}
}
}
----------------------------------------------
enum
타입에 들어갈 값을 미리 정의
enum Blood {
A
B
O
AB
}
type Human {
humanName: String
blood : Blood
sex: String
...
}
blood필드에 해당하는 값은 A B O AB 중에 하나
------------------------------------------------
subscription
구독을 통한 실시간 데이터 처리
변경이 일어난 경우 실시간으로 반영 (ex.알람, 채팅)
type Subscription {
personAdded: Person
}
실제 쿼리
subscription {
personAdded {
_id
humanName
blood
gender
}
}
Person에 어떠한 변동이 있는지 listening하고 있음 (주의를 기울이고 있음)
다른 쪽에서 muation하는 경우 subscription쪽에서 감지
mutation {
addPerson(humanNmae: "kim", age: 13, gender: "male"){
_id
}
}
-----------------------------------------------------------------
실습 case
type Return {
_id:String
number: Int
message: String
}
3개 중에서 반드시 1개 이상은 받아야 한다.
또한 리턴타입이 { } 객체이기 때문에 중괄호를 해주어야 한다.
!가 붙어있는 경우 필수값이므로 반드시 입력
fetchBoardsCount: Int
위와 같은 경우
query {
fetchBoardsCount
}
fetchProfilesCount: Int
위와 같은 경우
query {
fetchProfilesCount
}
likeBoard(boardId: ID!): Int! ← 틀린 것이 아니다
위와 같은 경우
mutation{
likeBoard(boardId : "63c7ecb47d035600293d9a61")
}