📚 GraphQL과 Apollo
Chapter 3. GraphQL 서버 깊이 파보기
Lesson 4. 인자와 인풋 타입

2. 인자와 인풋 타입

  • 지난 실습 그대로 진행 또는 3-4-arg-input-type 열기

People 데이터 조건들로 필터 넣어 받아오기

_queries.js

type Query { ... peopleFiltered( team: Int, sex: Sex, blood_type: BloodType, from: String ): [People] ... }

people.js

Query: { // ... peopleFiltered: (parent, args) => dbWorks.getPeople(args), }
query { peopleFiltered ( team: 1 blood_type: B from: "Texas" ) { id first_name last_name sex blood_type serve_years role team from } }


페이지로 나누어 받아오기

_queries.js

type Query { ... peoplePaginated( page: Int!, per_page: Int! ): [People] ... }

people.js

Query: { // ... peoplePaginated: (parent, args) => dbWorks.getPeople(args), // ... }
query { peoplePaginated(page: 1, per_page: 7) { id first_name last_name sex blood_type serve_years role team from } }


별칭으로 받아오기

query { badGuys: peopleFiltered(sex: male, blood_type: B) { first_name last_name sex blood_type } newYorkers: peopleFiltered(from: "New York") { first_name last_name from } }


인풋 타입

people.js

const typeDefs = gql` .... input PostPersonInput { first_name: String! last_name: String! sex: Sex! blood_type: BloodType! serve_years: Int! role: Role! team: ID! from: String! } ` const resolvers = { // ... Mutation: { postPerson: (parent, args) => dbWorks.postPerson(args), } }

_mutation.js

type Mutation { postPerson(input: PostPersonInput): People! ... }
mutation { postPerson(input: { first_name: "Hanna" last_name: "Kim" sex: female blood_type: O serve_years: 3 role: developer team: 1 from: "Pusan" }) { id first_name last_name sex blood_type role team from } }
📚 GraphQL과 Apollo