GraphQL๊ณผ Apollo ๋‹ค์„ฏ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - GraphQL์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ณด์•„์š” ๐Ÿ˜€

2023. 11. 19. 21:38ใ†Back-End ์ž‘์—…์‹ค/๊ธฐํƒ€ ์ฐธ๊ณ  ์ž๋ฃŒ

728x90
๋ฐ˜์‘ํ˜•

 

 

์นด์นด์˜คํŽ˜์ด | ๋งˆ์Œ ๋†“๊ณ  ๊ธˆ์œตํ•˜๋‹ค

์—ฌ๊ธฐ๋ฅผ ๋ˆŒ๋Ÿฌ ๋งํฌ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

qr.kakaopay.com

 

 

 

GraphQL๊ณผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๊ฐœ๋ฐœํ•˜๋Š” ์›น ์„œ๋น„์Šค:์„ค๊ณ„๋ถ€ํ„ฐ ๊ฐœ๋ฐœ·๋ฐฐํฌ๊นŒ์ง€ ๋”ฐ๋ผ ํ•˜๋ฉฐ ์™„์„ฑํ•˜๋Š” ์›น ํ’€

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 




๐Ÿ—‚ ๋ชฉ์ฐจ

โœ… GraphQL๊ณผ Apollo ์ฒซ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - ๊ฐœ๋… ์ตํžˆ๊ธฐ
โœ… GraphQL๊ณผ Apollo ๋‘๋ฒˆ์งธ ์ด์•ผ๊ธฐ - REST API๋ž€?
โœ… GraphQL๊ณผ Apollo ์„ธ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - GraphQL์˜ ์ •๋ณด ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ฐฉ์‹

โœ… GraphQL๊ณผ Apollo ๋„ค๋ฒˆ์งธ ์ด์•ผ๊ธฐ - Apollo๋ž€?
โœ… GraphQL๊ณผ Apollo ๋‹ค์„ฏ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - GraphQL์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ณด์•„์š” ๐Ÿ˜€
โœ… GraphQL๊ณผ Apollo ์—ฌ์„ฏ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - GraphQL Moduleํ™”์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•„์š” ๐Ÿ˜€
โœ… GraphQL๊ณผ Apollo ์ผ๊ณฑ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - GraphQL Data Type์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•„์š” ๐Ÿ˜€
โœ… GraphQL๊ณผ Apollo ์—ฌ๋Ÿ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - GraphQL Union๊ณผ Interface ๊ทธ๋ฆฌ๊ณ  ์ธ์ž์™€ ์ธํ’‹ ํƒ€์ž…์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•„์š” ๐Ÿ˜€
โœ… GraphQL๊ณผ Apollo ์•„ํ™‰๋ฒˆ์งธ ์ด์•ผ๊ธฐ - Java + Spring Boot์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ™˜๊ฒฝ ๊ตฌ์„ฑ
โœ… GraphQL๊ณผ Apollo ์—ด๋ฒˆ์งธ ์ด์•ผ๊ธฐ - Java + Spring Boot์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ•ด๋ณด๊ธฐ
โœ… GraphQL๊ณผ Apollo ์—ด ํ•œ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - TypeScript + Nest.js์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ™˜๊ฒฝ ๊ตฌ์„ฑ
โœ… 
GraphQL๊ณผ Apollo ์—ด ๋‘๋ฒˆ์งธ ์ด์•ผ๊ธฐ - TypeScript + Nest.js์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ™˜๊ฒฝ ํ…Œ์ŠคํŠธ
โœ… GraphQL๊ณผ Apollo ์—ด ์„ธ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - TypeScript + Nest์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ•ด๋ณด๊ธฐ
โœ… 
GraphQL๊ณผ Apollo ์—ด ๋„ค๋ฒˆ์งธ ์ด์•ผ๊ธฐ - React์™€ Apollo Client
โœ… GraphQL๊ณผ Apollo ์—ด๋‹ค์„ฏ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - React์™€ Apollo Client - Query์™€ Mutation ์‚ฌ์šฉํ•˜์—ฌ ์›น ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ
โœ… GraphQL๊ณผ Apollo ์—ด ์—ฌ์„ฏ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - Kotlin + Spring Boot์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ™˜๊ฒฝ ๊ตฌ์„ฑ
โœ… GraphQL๊ณผ Apollo ์—ด ์ผ๊ณฑ๋ฒˆ์งธ ์ด์•ผ๊ธฐ - Kotlin + Spring Boot์—์„œ GraphQL ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ - ์‹ค์Šต ํ•ด๋ณด๊ธฐ


๐Ÿค” ๋‚ด๊ฐ€ ๋งŒ๋‚œ ๋ฌธ์ œ

โš ๏ธ [Nest.js] TypeORM Table ๊ด€๊ณ„๊ฐ€ ๋งบ์–ด์กŒ์„ ๋•Œ, Seeding (feat. Migration)
โš ๏ธ [Spring Boot 3.0] Could not resolve org.springframework.boot:spring-boot-gradle-plugin
โš ๏ธ [Spring Boot 3] Spring Doc(Swagger) White Label Error


๐Ÿ“‹ ๋ถ€๋ก

๐Ÿ” [Nest.js] ์ดˆ๊ธฐ ํ™˜๊ฒฝ ๊ตฌ์„ฑ (feat. TypeORM, QueryBuilder, GraphQL, Apollo)
๐Ÿ” [SOLID][Nest.js][Java + Spring] Interface๋ฅผ ํ™œ์šฉํ•œ ๊ฒฐํ•ฉ๋„ ๋ถ„๋ฆฌ (Interface๋ฅผ ์ด์šฉํ•œ Dependency Injection - DI)

 

 

 

๐Ÿš€ GraphQL๊ณผ Apollo ๋‹ค์„ฏ๋ฒˆ์งธ ์ด์•ผ๊ธฐ

    ๐Ÿ”ฝ GraphQL์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด ๋ณด์•„์š” ๐Ÿ˜€

        ๐Ÿ“ฆ ๊ฐœ์š”

1. type ํ•ด๋ถ€!

์ง€๋‚œ ๊ธ€์—์„œ ์ฃผ๋‹ˆ๋Š” GraphQL ๋ฐ Apollo ์‹ค์Šต์„ ์œ„ํ•œ Node.js ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜์–ด์š”.
ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋กœ ์ด์•ผ๊ธฐ๋ฅผ ์ด์–ด๋‚˜๊ฐ€ ๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

index.js


index.js์— ๋ณด๋ฉด 4 ~ 6๋ฒˆ์งธ ์ค„์— Query ๋ผ๋Š” type์ด ์ง€์ • ๋˜์–ด ์žˆ๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.
Query๋ž€ Type ์•ˆ์— ์–ด๋–ค ์š”์ฒญ๋“ค์ด ๋“ค์–ด์˜ค๊ณ , ๊ทธ๊ฒƒ์œผ๋กœ ์–ด๋–ค ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋“ค์ด ๋ฐ˜ํ™˜๋ ์ง€ ์ง€์ •๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋ž˜์„œ ํด๋ผ์ด์–ธํŠธ์—์„œ Teams๋ผ๋Š” Query๋ฅผ ์š”์ฒญํ•˜๊ฒŒ ๋˜๋ฉด 5๋ฒˆ์งธ ์ค„์— ๋Œ€๊ด„ํ˜ธ์•ˆ์— Team์ด ์žˆ๋Š”๋ฐ, ์ด Team์ด๋ž€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณต์ˆ˜๋กœ ์—ฌ๋Ÿฌ๊ฐœ ๋ฐ˜ํ™˜๋œ๋‹ค๋Š” ์˜๋ฏธ์—์š”.

index.js


์ฆ‰, ์œ„์˜ Team์ด๋ผ๋Š” Type์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ๋ฐ˜ํ™˜๋ ๊ฑฐ์•ผ! ๋ผ๋Š” ์˜๋ฏธ์—์š”.
์ •๋ฆฌํ•ด๋ณด์ž๋ฉด ํด๋ผ์ด์–ธํŠธ์—์„œ teams๋ผ๋Š” Query๋ฅผ ์š”์ฒญํ•˜๋ฉด Team์ด๋ผ๋Š” ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ๋ฐ˜ํ™˜๋œ๋‹ค!๊ฐ€ ๋˜๊ฒ ๋„ค์š”.


index.js


์œ„ ๋ถ€๋ถ„์„ GraphQL์—์„œ๋Š” Query Root Type์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ณ  ์žˆ์–ด์š”.
Query Root Type์€ ์ž๋ฃŒ ์š”์ฒญ์— ์‚ฌ์šฉ๋  Query๋“ค์„ ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด๊ณ , Query ๋ช…๋ น๋ฌธ๋งˆ๋‹ค ๋ฐ˜ํ™˜๋  ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

728x90

 

 

 

 

2. Resolver ํ•ด๋ถ€!

index.js


Resolver๋Š” ์œ„์™€ ๊ฐ™์ด ์ฝ”๋”ฉํ•ด ์ฃผ์—ˆ๋Š”๋ฐ, Query๋ผ๋Š” ๊ฒƒ์€ Object(๊ฐ์ฒด)์˜ ํ•ญ๋ชฉ๋“ค๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ ์„ ์–ธ์ด์—์š”.
์‹ค์ œ ํ”„๋กœ์ ํŠธ๋กœ ์˜ˆ๋ฅผ ๋“ค๋ฉด DBMS์—์„œ Database ์กฐํšŒ๋ฅผ ์œ„ํ•œ Code ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ์–ด์š”.

์ฆ‰, Resolver๋Š” Query๋กœ ์ง„ํ–‰ํ•˜๋Š” ์ž‘์—…๋“ค์ด ์‹ค์งˆ์ ์œผ๋กœ ์ด๋ค„์ง€๋Š” ์ฝ”๋“œ์—์š”.
19๋ฒˆ์งธ ์ค„์— teams ํ•ญ๋ชฉ์€ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•ˆ์— ์žˆ๋Š” teams๋ผ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ๊ณ  ์ด๋ค„์ง„ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”.

 

 

 

 

    ๐Ÿ”ฝ ์‹ค์Šต!

        ๐Ÿ“ฆ equipments๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Query๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•„์š” ๐Ÿ˜€

์ตœ์ดˆ dbtester.js๋ฅผ ๋งŒ๋“ค์–ด ์ค„๊ฑฐ์—์š”.

dbtester.js


์œ„์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ ์ตœ์ƒ์œ„ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ฐ‘์— dbtester.js๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.


dbtester.js


์ผ๋‹จ, database ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด ๋ณด์•˜์–ด์š”.


npm install


๊ทธ๋Ÿฐ ๋’ค ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด Module์„ ์ถ”๊ฐ€ ์„ค์น˜ ํ•ด ์ฃผ์—ˆ์–ด์š”.

nodemon dbtester.js

 JunySS ๐Ÿต  ๎‚ฐ ~/Programming/study/apolloStudy ๎‚ฐ nodemon dbtester.js
[nodemon] 3.0.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node dbtester.js`
{
  teams: [
    {
      id: 1,
      manager: 'Mandy Warren',
      office: '101A',
      extension_number: '#5709',
      mascot: 'Panda',
      cleaning_duty: 'Monday',
      project: 'Hyperion'
    },
    {
      id: 2,
      manager: 'Stewart Grant',
      office: '101B',
      extension_number: '#4012',
      mascot: 'Tadpole',
      cleaning_duty: 'Tuesday',
      project: 'Zen'
    },
    {
      id: 3,
      manager: 'Smantha Wheatly',
      office: '102A',
      extension_number: '#3852',
      mascot: 'Falcon',
      cleaning_duty: 'Wednesday',
      project: 'Duranno'
    },
    {
      id: 4,
      manager: 'Francis Buckley',
      office: '103B',
      extension_number: '#1039',
      mascot: 'Beaver',
      cleaning_duty: 'Thursday',
      project: 'Genghis'
    },
    {
      id: 5,
      manager: 'Blake Smith',
      office: '104A',
      extension_number: '#7750',
      mascot: 'Wildcat',
      cleaning_duty: 'Friday',
      project: 'Acheron'
    }
  ],
  people: [
    {
      id: 1,
      first_name: 'Alex',
      last_name: 'Davidson',
      sex: 'male',
      blood_type: 'O',
      serve_years: 2,
      role: 'developer',
      team: 2,
      from: 'California'
    },
    {
      id: 2,
      first_name: 'Lindsay',
      last_name: 'West',
      sex: 'female',
      blood_type: 'A',
      serve_years: 3,
      role: 'designer',
      team: 3,
      from: 'Arizona'
    },
    {
      id: 3,
      first_name: 'Nathan',
      last_name: 'Jenkins',
      sex: 'male',
      blood_type: 'B',
      serve_years: 1,
      role: 'planner',
      team: 1,
      from: 'Texas'
    },
    {
      id: 4,
      first_name: 'Christine',
      last_name: 'Harris',
      sex: 'female',
      blood_type: 'B',
      serve_years: 2,
      role: 'developer',
      team: 4,
      from: 'Ohio'
    },
    {
      id: 5,
      first_name: 'Page',
      last_name: 'Adams',
      sex: 'female',
      blood_type: 'O',
      serve_years: 5,
      role: 'developer',
      team: 2,
      from: 'California'
    },
    {
      id: 6,
      first_name: 'Herbert',
      last_name: 'Ford',
      sex: 'male',
      blood_type: 'A',
      serve_years: 2,
      role: 'designer',
      team: 5,
      from: 'Messachusetts'
    },
    {
      id: 7,
      first_name: 'Dennis',
      last_name: 'Marshall',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 1,
      role: 'planner',
      team: 2,
      from: 'Colorado'
    },
    {
      id: 8,
      first_name: 'Isabella',
      last_name: 'Martin',
      sex: 'female',
      blood_type: 'A',
      serve_years: 3,
      role: 'developer',
      team: 1,
      from: 'Georgia'
    },
    {
      id: 9,
      first_name: 'Alfred',
      last_name: 'Clarke',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 4,
      role: 'designer',
      team: 4,
      from: 'Pennsylvania'
    },
    {
      id: 10,
      first_name: 'Tyler',
      last_name: 'Philips',
      sex: 'male',
      blood_type: 'O',
      serve_years: 1,
      role: 'designer',
      team: 5,
      from: 'Virginia'
    },
    {
      id: 11,
      first_name: 'Noelle',
      last_name: 'Holmes',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 5,
      role: 'planner',
      team: 3,
      from: 'California'
    },
    {
      id: 12,
      first_name: 'Kate',
      last_name: 'Owen',
      sex: 'female',
      blood_type: 'B',
      serve_years: 2,
      role: 'developer',
      team: 1,
      from: 'Maine'
    },
    {
      id: 13,
      first_name: 'Priscilla',
      last_name: 'Walker',
      sex: 'female',
      blood_type: 'O',
      serve_years: 2,
      role: 'developer',
      team: 4,
      from: 'California'
    },
    {
      id: 14,
      first_name: 'Shirley',
      last_name: 'Grant',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 1,
      role: 'developer',
      team: 3,
      from: 'Messachusetts'
    },
    {
      id: 15,
      first_name: 'Byron',
      last_name: 'Barnes',
      sex: 'male',
      blood_type: 'A',
      serve_years: 3,
      role: 'designer',
      team: 1,
      from: 'Idaho'
    },
    {
      id: 16,
      first_name: 'Toby',
      last_name: 'Lewis',
      sex: 'male',
      blood_type: 'A',
      serve_years: 1,
      role: 'planner',
      team: 3,
      from: 'New York'
    },
    {
      id: 17,
      first_name: 'Barbara',
      last_name: 'White',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 4,
      role: 'developer',
      team: 4,
      from: 'Ohio'
    },
    {
      id: 18,
      first_name: 'Arnold',
      last_name: 'Armstrong',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 5,
      role: 'designer',
      team: 4,
      from: 'Nevada'
    },
    {
      id: 19,
      first_name: 'Eleanor',
      last_name: 'Smith',
      sex: 'female',
      blood_type: 'B',
      serve_years: 2,
      role: 'planner',
      team: 2,
      from: 'Texas'
    },
    {
      id: 20,
      first_name: 'Nina',
      last_name: 'Dawson',
      sex: 'female',
      blood_type: 'O',
      serve_years: 1,
      role: 'developer',
      team: 5,
      from: 'Virginia'
    },
    {
      id: 21,
      first_name: 'Jamie',
      last_name: 'Reynolds',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 1,
      role: 'designer',
      team: 3,
      from: 'Arizona'
    },
    {
      id: 22,
      first_name: 'Myrtle',
      last_name: 'Hamilton',
      sex: 'female',
      blood_type: 'O',
      serve_years: 1,
      role: 'developer',
      team: 4,
      from: 'Maine'
    },
    {
      id: 23,
      first_name: 'Zelda',
      last_name: 'Hall',
      sex: 'female',
      blood_type: 'A',
      serve_years: 3,
      role: 'developer',
      team: 2,
      from: 'Tennessee'
    },
    {
      id: 24,
      first_name: 'Edgar',
      last_name: 'Willson',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 2,
      role: 'designer',
      team: 4,
      from: 'Texas'
    },
    {
      id: 25,
      first_name: 'Brian',
      last_name: 'Hunt',
      sex: 'male',
      blood_type: 'B',
      serve_years: 2,
      role: 'planner',
      team: 1,
      from: 'Indiana'
    },
    {
      id: 26,
      first_name: 'Naomi',
      last_name: 'Taylor',
      sex: 'female',
      blood_type: 'O',
      serve_years: 5,
      role: 'developer',
      team: 4,
      from: 'Oklahoma'
    },
    {
      id: 27,
      first_name: 'Brooke',
      last_name: 'Evans',
      sex: 'male',
      blood_type: 'O',
      serve_years: 1,
      role: 'planner',
      team: 2,
      from: 'California'
    },
    {
      id: 28,
      first_name: 'Katherine',
      last_name: 'Bennett',
      sex: 'female',
      blood_type: 'A',
      serve_years: 3,
      role: 'designer',
      team: 5,
      from: 'Utah'
    },
    {
      id: 29,
      first_name: 'Violet',
      last_name: 'Pearson',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 5,
      role: 'designer',
      team: 1,
      from: 'Pennsylvania'
    },
    {
      id: 30,
      first_name: 'Margaret',
      last_name: 'Davies',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 2,
      role: 'developer',
      team: 5,
      from: 'California'
    },
    {
      id: 31,
      first_name: 'Raymond',
      last_name: 'Cook',
      sex: 'male',
      blood_type: 'O',
      serve_years: 1,
      role: 'planner',
      team: 3,
      from: 'Kansas'
    },
    {
      id: 32,
      first_name: 'Garth',
      last_name: 'Roberts',
      sex: 'male',
      blood_type: 'A',
      serve_years: 4,
      role: 'designer',
      team: 5,
      from: 'Minnesota'
    },
    {
      id: 33,
      first_name: 'Victoria',
      last_name: 'Brown',
      sex: 'female',
      blood_type: 'B',
      serve_years: 4,
      role: 'planner',
      team: 2,
      from: 'Georgia'
    },
    {
      id: 34,
      first_name: 'Russel',
      last_name: 'Jones',
      sex: 'male',
      blood_type: 'O',
      serve_years: 2,
      role: 'designer',
      team: 3,
      from: 'Nebraska'
    },
    {
      id: 35,
      first_name: 'Sally',
      last_name: 'Fox',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 5,
      role: 'planner',
      team: 1,
      from: 'Messachusetts'
    },
    {
      id: 36,
      first_name: 'Chloe',
      last_name: 'Bailey',
      sex: 'female',
      blood_type: 'A',
      serve_years: 5,
      role: 'developer',
      team: 4,
      from: 'New York'
    },
    {
      id: 37,
      first_name: 'Will',
      last_name: 'McDonald',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 1,
      role: 'developer',
      team: 5,
      from: 'Wyoming'
    },
    {
      id: 38,
      first_name: 'Grant',
      last_name: 'Cole',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 4,
      role: 'designer',
      team: 3,
      from: 'Pennsylvania'
    },
    {
      id: 39,
      first_name: 'Audrey',
      last_name: 'Fisher',
      sex: 'female',
      blood_type: 'O',
      serve_years: 2,
      role: 'designer',
      team: 5,
      from: 'Ohio'
    },
    {
      id: 40,
      first_name: 'Amber',
      last_name: 'Russel',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 5,
      role: 'developer',
      team: 3,
      from: 'Kansas'
    },
    {
      id: 41,
      first_name: 'Peter',
      last_name: 'Payne',
      sex: 'male',
      blood_type: 'O',
      serve_years: 3,
      role: 'planner',
      team: 4,
      from: 'California'
    },
    {
      id: 42,
      first_name: 'Russ',
      last_name: 'Lawrence',
      sex: 'male',
      blood_type: 'O',
      serve_years: 5,
      role: 'designer',
      team: 1,
      from: 'New York'
    },
    {
      id: 43,
      first_name: 'Margot',
      last_name: 'Watson',
      sex: 'female',
      blood_type: 'AB',
      serve_years: 2,
      role: 'planner',
      team: 5,
      from: 'Virginia'
    },
    {
      id: 44,
      first_name: 'Nancy',
      last_name: 'Brooks',
      sex: 'female',
      blood_type: 'A',
      serve_years: 1,
      role: 'developer',
      team: 2,
      from: 'California'
    },
    {
      id: 45,
      first_name: 'Oliver',
      last_name: 'Simpson',
      sex: 'male',
      blood_type: 'O',
      serve_years: 3,
      role: 'designer',
      team: 2,
      from: 'Arizona'
    },
    {
      id: 46,
      first_name: 'Pansy',
      last_name: 'Moore',
      sex: 'female',
      blood_type: 'B',
      serve_years: 2,
      role: 'planner',
      team: 3,
      from: 'Minnesota'
    },
    {
      id: 47,
      first_name: 'Leroy',
      last_name: 'Elliott',
      sex: 'male',
      blood_type: 'AB',
      serve_years: 2,
      role: 'developer',
      team: 1,
      from: 'Indiana'
    },
    {
      id: 48,
      first_name: 'Barbara',
      last_name: 'Murphy',
      sex: 'female',
      blood_type: 'O',
      serve_years: 1,
      role: 'developer',
      team: 5,
      from: 'Texas'
    },
    {
      id: 49,
      first_name: 'Simon',
      last_name: 'Henderson',
      sex: 'male',
      blood_type: 'A',
      serve_years: 4,
      role: 'designer',
      team: 2,
      from: 'Pennsylvania'
    },
    {
      id: 50,
      first_name: 'Ned',
      last_name: 'Butler',
      sex: 'male',
      blood_type: 'O',
      serve_years: 2,
      role: 'planner',
      team: 1,
      from: 'Messachusetts'
    }
  ],
  roles: [
    {
      id: 'developer',
      job: 'programming',
      requirement: 'computer science degree'
    },
    {
      id: 'designer',
      job: 'web design',
      requirement: 'graphic design certificate'
    },
    {
      id: 'planner',
      job: 'service planning',
      requirement: 'portfolio'
    }
  ],
  softwares: [
    {
      id: 'Eclipse',
      used_by: 'developer',
      developed_by: 'Eclipse Foundation',
      description: 'integrated development environment'
    },
    {
      id: 'Excel',
      used_by: 'planner',
      developed_by: 'Microsoft',
      description: 'spreadsheet'
    },
    {
      id: 'Illustrator',
      used_by: 'designer',
      developed_by: 'Adobe',
      description: 'vector graphics editor'
    },
    {
      id: 'Photoshop',
      used_by: 'designer',
      developed_by: 'Adobe',
      description: 'raster graphics editor'
    },
    {
      id: 'PowerPoint',
      used_by: 'planner',
      developed_by: 'Microsoft',
      description: 'presentation program'
    },
    {
      id: 'Sketch',
      used_by: 'designer',
      developed_by: 'Sketch B.V.',
      description: 'vector graphics editor'
    },
    {
      id: 'VS Code',
      used_by: 'developer',
      developed_by: 'Microsoft',
      description: 'source code editor'
    },
    {
      id: 'Word',
      used_by: 'planner',
      developed_by: 'Microsoft',
      description: 'word processor'
    },
    {
      id: 'Xcode',
      used_by: 'developer',
      developed_by: 'Apple',
      description: 'integrated development environment'
    }
  ],
  equipments: [
    {
      id: 'machanical keyboard',
      used_by: 'developer',
      count: 24,
      new_or_used: 'used'
    },
    {
      id: 'pen tablet',
      used_by: 'designer',
      count: 15,
      new_or_used: 'used'
    },
    {
      id: 'notebook',
      used_by: 'planner',
      count: 37,
      new_or_used: 'new'
    },
    {
      id: 'ergonomic mouse',
      used_by: 'designer',
      count: 31,
      new_or_used: 'used'
    },
    {
      id: 'dual monitor',
      used_by: 'developer',
      count: 20,
      new_or_used: 'used'
    },
    {
      id: 'whiteboard',
      used_by: 'planner',
      count: 12,
      new_or_used: 'used'
    },
    {
      id: 'sketchboard',
      used_by: 'designer',
      count: 48,
      new_or_used: 'new'
    }
  ],
  supplies: [
    { id: 'ergonomic mouse', team: 1 },
    { id: 'mug', team: 1 },
    { id: 'webcam', team: 2 },
    { id: 'hoodie', team: 2 },
    { id: 'chair', team: 3 },
    { id: 'usb hub', team: 3 },
    { id: 'headphone', team: 4 },
    { id: 'stempler', team: 4 },
    { id: 'calculator', team: 5 },
    { id: 't shirt', team: 5 }
  ]
}
[nodemon] clean exit - waiting for changes before restart


์œ„์™€ ๊ฐ™์ด dbtester.js๋ฅผ nodemon์œผ๋กœ ๊ธฐ๋™ํ•˜๋ฉด ๋ชจ๋“  ํ•ญ๋ชฉ๋“ค์ด json ํ˜•ํƒœ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

dbtester.js


๋‹ค์‹œ ์ฝ”๋“œ๋ฅผ ์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•ˆ์— equipments ๋ฐ์ดํ„ฐ๋งŒ ๋ฐ˜ํ™˜ ๋ฐ›๋„๋ก ์„ค์ •ํ•ด ์ฃผ์—ˆ์–ด์š”.

๊ทธ๋Ÿผ ์ด์ œ equipments ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ์ž‘์—…ํ•ด ๋ณผ๊ฒŒ์š”.

์ตœ์ดˆ ์ž๋ฃŒํ˜•์„ ์–ด๋–ป๊ฒŒ ์งœ๋Š”์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ๊ฒŒ์š”.

[nodemon] restarting due to changes...
[nodemon] starting `node dbtester.js`
[
  {
    id: 'machanical keyboard',
    used_by: 'developer',
    count: 24,
    new_or_used: 'used'
  },
  {
    id: 'pen tablet',
    used_by: 'designer',
    count: 15,
    new_or_used: 'used'
  },
  { id: 'notebook', used_by: 'planner', count: 37, new_or_used: 'new' },
  {
    id: 'ergonomic mouse',
    used_by: 'designer',
    count: 31,
    new_or_used: 'used'
  },
  {
    id: 'dual monitor',
    used_by: 'developer',
    count: 20,
    new_or_used: 'used'
  },
  {
    id: 'whiteboard',
    used_by: 'planner',
    count: 12,
    new_or_used: 'used'
  },
  {
    id: 'sketchboard',
    used_by: 'designer',
    count: 48,
    new_or_used: 'new'
  }
]
[nodemon] clean exit - waiting for changes before restart


์ฝ˜์†”์— ์ถœ๋ ฅ๋œ equipments ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๋ฉด ์œ„์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ๊ฐ€์ง„๊ฑธ ์•Œ ์ˆ˜ ์žˆ์–ด์š”.
์ฆ‰ Count๋Š” ์ •์ˆ˜ํ˜•์œผ๋กœ ๋˜์–ด ์žˆ๊ณ , ๋‚˜๋จธ์ง€๋Š” ๋ฌธ์ž์—ด๋กœ ์ด๋ค„์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

index.js


index.js์˜ 18 ~ 23๋ฒˆ์งธ ์ค„์— Equipment Type์„ ์ƒˆ๋กœ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ์–ด์š”.
์ด๋ ‡๊ฒŒ ํ•ด์„œ ๋ฐ˜ํ™˜ํ•  Equipment Type์ด ๋งŒ๋“ค์–ด์ง„๊ฑฐ์—์š”.

์ด์ œ Root Query Type์—๋„ ์ด ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ด ์ฃผ์–ด์•ผ ํ•ด์š”.

index.js


index.js์˜ 6๋ฒˆ์งธ ์ค„์— ์œ„์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์–ด์š”.

์ด์ œ ์œ„ ๋‚ด์šฉ์— ์‹ค์ œ Action(์•ก์…˜)์„ ์ทจํ•ด์ค„ Resolver๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์–ด์•ผ ํ•ด์š”.

index.js

 

const resolvers = {
    Query: {
        teams: () => database.teams,
        equipments: () => database.equipments
    }
}


์œ„์™€ ๊ฐ™์ด resolvers ๊ฐ์ฒด์— ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ equipments ๊ด€๋ จ Query ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ equipments๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•ด ์ค„ ์ˆ˜ ์žˆ๋„๋ก ์ž‘์„ฑ์„ ํ•ด ์ฃผ์—ˆ์–ด์š”. 

๊ตฌ๋™์ค‘์ธ nodemon์„ ์ข…๋ฃŒ(Ctrl + C)ํ•˜๊ณ , ๋‹ค์‹œ Npm์„ ๊ตฌ๋™ ์‹œ์ผœ ๋ณผ๊ฒŒ์š”.

npm start

 

http://localhost:4000


Playground์—์„œ ์œ„์™€ ๊ฐ™์ด Query๋ฅผ ์ž…๋ ฅํ•ด์„œ ๋‚ ๋ ค๋ณด๋ฉด ์‘๋‹ต์„ ์ •์ƒ์ ์œผ๋กœ ๋ฐ›๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

 

 

        ๐Ÿ“ฆ supplies๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Query๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•„์š” ๐Ÿ˜€

index.js


์œ„์—์„œ ์ด์•ผ๊ธฐํ•œ ๋‚ด์šฉ๋Œ€๋กœ Supply์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์•˜์–ด์š”.

http://localhost:4000

 

{
  "data": {
    "equipments": [
      {
        "id": "machanical keyboard",
        "used_by": "developer",
        "count": 24,
        "new_or_used": "used"
      },
      {
        "id": "pen tablet",
        "used_by": "designer",
        "count": 15,
        "new_or_used": "used"
      },
      {
        "id": "notebook",
        "used_by": "planner",
        "count": 37,
        "new_or_used": "new"
      },
      {
        "id": "ergonomic mouse",
        "used_by": "designer",
        "count": 31,
        "new_or_used": "used"
      },
      {
        "id": "dual monitor",
        "used_by": "developer",
        "count": 20,
        "new_or_used": "used"
      },
      {
        "id": "whiteboard",
        "used_by": "planner",
        "count": 12,
        "new_or_used": "used"
      },
      {
        "id": "sketchboard",
        "used_by": "designer",
        "count": 48,
        "new_or_used": "new"
      }
    ],
    "supplies": [
      {
        "id": "ergonomic mouse",
        "team": 1
      },
      {
        "id": "mug",
        "team": 1
      },
      {
        "id": "webcam",
        "team": 2
      },
      {
        "id": "hoodie",
        "team": 2
      },
      {
        "id": "chair",
        "team": 3
      },
      {
        "id": "usb hub",
        "team": 3
      },
      {
        "id": "headphone",
        "team": 4
      },
      {
        "id": "stempler",
        "team": 4
      },
      {
        "id": "calculator",
        "team": 5
      },
      {
        "id": "t shirt",
        "team": 5
      }
    ]
  }
}


์œ„์™€ ๊ฐ™์ด Equipments์™€ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ Supplies์— ๋Œ€ํ•ด ์š”์ฒญ์„ ๋ณด๋‚ด๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

 

        ๐Ÿ“ฆ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ํŠน์ • ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ

CRUD๋ฅผ ์ž‘์—…ํ•  ๋•Œ, ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋งŒ ์กฐํšŒํ•˜์ง„ ์•Š์„๊ฑฐ์—์š”.
์–ด๋–ค ์กฐ๊ฑด์— ์˜ํ•ด์„œ ํ•ด๋‹น ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์™€์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์–ด์š”.

๊ทธ๋Ÿฐ ๋ถ€๋ถ„์€ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

 

Playgroud


ํ˜„ ์ƒํƒœ์—์„œ Playgroud๋ฅผ ํ†ตํ•ด ์œ„์™€ ๊ฐ™์ด Teams์˜ ID๋ฅผ ์กฐํšŒํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ์‘๋‹ต์„ ๋ฐ›๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.
์—ฌ๊ธฐ์„œ ํŠน์ • ์กฐ๊ฑด์„ ์ค˜์„œ ํ•ด๋‹น ์กฐ๊ฑด์— ๋งž๋Š” ID๋งŒ ์ถ”์ถœํ•ด์„œ ์‘๋‹ต ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณผ๊ฒŒ์š”.

์ตœ์ดˆ ์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” Resolver ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด ๋ณผ๊ฑฐ์—์š”.

index.js

 

        team: (parent, args, context, info) =>
            database.teams.filter((team) => {
                return team.id === args.id;
            }),


์œ„์™€ ๊ฐ™์ด Resolver ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋Š”๋ฐ, ์ด ์ฝ”๋“œ๋Š” team์ด๋ผ๋Š” Query๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , ํ•ด๋‹น ํ•จ์ˆ˜์— ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ parent, args, context, info๋ฅผ ๋ฐ›๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”. ๋งค๊ฐœ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๋‹ค๋ฅธ ๊ธ€์—์„œ ์กฐ๊ธˆ ๋” ์•Œ์•„๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

์ผ๋‹จ ์ด ๊ณณ์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์ฃผ๋ชฉํ•ด์•ผ ํ•˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” args(Arguments)์—์š”.

ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด database์—์„œ teams๋ฅผ ๋ชจ๋‘ ๋ฐ˜ํ™˜ํ•  ๊ฑด๋ฐ, Filter(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ฐ›์•„์˜จ Teams ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ ํŒ€์ด ์–ด๋–ค ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฅ˜๋ ์ง€๋ฅผ ์ง€์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์žฌ ์ง€์ •)๋ฅผ ๊ฑธ๊ฒŒ ๋˜๊ณ , 38๋ฒˆ์งธ ์ค„์— team์˜ ID๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜ args์—๋Š” ID๋ผ๋Š” ํ•ญ๋ชฉ์ด ์žˆ๋Š”๋ฐ ์ด ID์™€ ๊ฐ™์€ ๊ฒƒ๋งŒ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

์ด ๋•Œ, Filter์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๊ทธ ํŒ€ ํ•˜๋‚˜๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ํ•ด๋‹น ํŒ€ ํ•˜๋‚˜๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด์ด ๋ฐ˜ํ™˜๋˜๊ฒŒ ๋ผ์š”.

index.js


์ด ๋•Œ, ์œ„ 39๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด ์ค‘ 0๋ฒˆ์งธ ๋ฐฐ์—ด๋งŒ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ฒ˜๋ฆฌํ•ด ๋ณผ๊ฒŒ์š”.


์—ฌ๊ธฐ๊นŒ์ง€ ํ•ด ๋†“์œผ๋ฉด ์ฝ˜์†”์€ ์œ„์™€ ๊ฐ™์ด ๋‚œ๋ฆฌ๊ฐ€ ๋‚ฌ์„ ๊ฑฐ์—์š”.
์™œ๋ƒํ•˜๋ฉด Resolver๋Š” ์ž‘์„ฑํ•ด ๋†“๊ณ , Type์€ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.
์ด๋ฒˆ์—” ๊ธฐ์กด team์— ๋Œ€ํ•œ Root Query Type๋ฅผ ์ˆ˜์ •ํ•ด ๋ณผ๊ฒŒ์š”.

index.js


6๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€๋ฅผ ํ•ด ์ฃผ์—ˆ๋Š”๋ฐ team์ด๋ผ๋Š” Query๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š”๋ฐ ์ •์ˆ˜ํ˜• ID๋ฅผ ๋ฐ›๊ฒŒ ๋˜๊ณ , ๋ฐ˜ํ™˜ ๊ฐ์ฒด๋กœ Team์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์–ด์š”.


์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ฝ˜์†”์€ ๋‹ค์‹œ ํ‰ํ™”๋ฅผ ์ฐพ๊ฒŒ ๋ ๊ฑฐ์—์š”.


Playgroud


Playgroud์—์„œ ์œ„์™€ ๊ฐ™์ด ID๊ฐ€ 1์ธ ํŒ€์˜ ์ „์ฒด ์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์˜ค๊ณ  ์‹ถ๋‹ค๊ณ  Query๋ฅผ ๋‚ ๋ ค์ฃผ๋‹ˆ ์‘๋‹ต์œผ๋กœ ID๊ฐ€ 1์ธ ํŒ€์— ๋Œ€ํ•œ
์ •๋ณด๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



Playgroud


์ด๋ฒˆ์—” ID๊ฐ€ 4์ธ ํŒ€์— ๋Œ€ํ•ด ์กฐํšŒ๋ฅผ ์š”์ฒญํ–ˆ๋”๋‹ˆ ์œ„์™€ ๊ฐ™์ด 4ํŒ€ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ ๋ฐ›์€๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

์œ„์—์„œ Query ์š”์ฒญ ์‹œ ID๊ฐ’์ด ๋ฐ”๋กœ Resolver์—์„œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ฃผ์—ˆ๋˜ args์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด์—์š”.
์ฆ‰, ํ•ด๋‹น ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์— ๋”ฐ๋ผ ID๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ , ๋‹ค๋ฅธ ๊ฒƒ์„ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

 

 

 

        ๐Ÿ“ฆ team๊ณผ supplies๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•˜๋Š” Query๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•„์š” ๐Ÿ˜€

์ด๋ฒˆ์—๋Š” Team ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•  ๋•Œ, ํ•ด๋‹น Team์— ํ•ด๋‹นํ•˜๋Š” Supplies๋„ ๊ฐ™์ด ์‘๋‹ต์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด๋ณผ๊ฒŒ์š”.
์ฐธ๊ณ ๋กœ Supplies์—๋Š” ํ•ด๋‹น ํŒ€์˜ ๋ฒˆํ˜ธ๋ฅผ ํ•ญ๋ชฉ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์š”.

index.js

๋ฐ˜์‘ํ˜•


๊ธฐ์กด teams Query๋ฅผ ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•ด ๋ณด์•˜์–ด์š”.

์ฝ”๋“œ๋ฅผ ์ž ์‹œ ์‚ดํŽด๋ณผ๊ฒŒ์š”.

Filter๋Š” ์œ„์—์„œ ์ด์•ผ๊ธฐ ํ–ˆ๋“ฏ ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด์„ {} ์•ˆ์— ๋ช…์‹œํ•œ ๋‚ด์šฉ์œผ๋กœ ๊ฑธ๋Ÿฌ์ฃผ๋Š” ํ•จ์ˆ˜์—์š”.
์œ„ ์ฝ”๋“œ์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ supplies ๋ฐฐ์—ด ๋‚ด์šฉ์„ Filterํ•˜๋Š”๋ฐ, ์ด ๋•Œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ supply๋ฅผ ๋„ฃ์–ด์ฃผ๊ฒŒ ๋˜๊ณ , ํ•ด๋‹น supply์•ˆ์— team ๋‚ด์šฉ๊ณผ team์˜ ID๊ฐ€ ๊ฐ™์€ ๊ฒƒ๋งŒ ๋ฐ˜ํ™˜ํ•ด๋‹ฌ๋ผ๊ณ  ํ•œ ๊ฑฐ์—์š”.

Map์€ ํ•ด๋‹น ๋ฐฐ์—ด์„ ํŠน์ • ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ๋‚ด๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์นœ๊ตฌ์—์š”.

์ฝ”๋“œ๋ฅผ ์ „์ฒด์ ์œผ๋กœ ๋ณด๋ฉด 35๋ฒˆ์งธ ์ค„์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ teams ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ ๋‹ค์Œ ํ•ด๋‹น ๋ฐฐ์—ด๋“ค์˜ ๊ฐ ํŒ€ ๋ฐ์ดํ„ฐ๋ฅผ Supplies๋ผ๋Š” ํ•ญ๋ชฉ์— ๋„ฃ๊ณ , ๊ทธ๊ฒƒ์˜ ๊ฐ’์œผ๋กœ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ Supplies๋ฅผ ๋ฐ›์•„์˜จ ๋‹ค์Œ ํ•ด๋‹น ํŒ€์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ๋“ค๋งŒ ๊ฑธ๋Ÿฌ๋‚ด์„œ ๋‹ค์‹œ ๊ทธ Supplies ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด๋‹น ํŒ€์— ๋„ฃ์€ ๋’ค ๊ทธ ํŒ€ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์—์š”.

index.js


๊ธฐ์กด Root Query Type์— Team์€ ์œ„์™€ ๊ฐ™์ด ๋˜์–ด ์žˆ๋Š”๋ฐ,

index.js


์ด๋ ‡๊ฒŒ 19๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด Supplies ์ •๋ณด(Supply ๋ฐฐ์—ด)๋„ ํ•จ๊ป˜ ๋‹ด์„ ์ˆ˜ ์žˆ๋„๋ก ์„ ์–ธํ•ด ์ฃผ์—ˆ์–ด์š”.
 

Playgroud

 

{
  "data": {
    "teams": [
      {
        "id": 1,
        "manager": "Mandy Warren",
        "office": "101A",
        "extension_number": "#5709",
        "mascot": "Panda",
        "cleaning_duty": "Monday",
        "project": "Hyperion",
        "supplies": [
          {
            "id": "ergonomic mouse",
            "team": 1
          },
          {
            "id": "mug",
            "team": 1
          }
        ]
      },
      {
        "id": 2,
        "manager": "Stewart Grant",
        "office": "101B",
        "extension_number": "#4012",
        "mascot": "Tadpole",
        "cleaning_duty": "Tuesday",
        "project": "Zen",
        "supplies": [
          {
            "id": "webcam",
            "team": 2
          },
          {
            "id": "hoodie",
            "team": 2
          }
        ]
      },
      {
        "id": 3,
        "manager": "Smantha Wheatly",
        "office": "102A",
        "extension_number": "#3852",
        "mascot": "Falcon",
        "cleaning_duty": "Wednesday",
        "project": "Duranno",
        "supplies": [
          {
            "id": "chair",
            "team": 3
          },
          {
            "id": "usb hub",
            "team": 3
          }
        ]
      },
      {
        "id": 4,
        "manager": "Francis Buckley",
        "office": "103B",
        "extension_number": "#1039",
        "mascot": "Beaver",
        "cleaning_duty": "Thursday",
        "project": "Genghis",
        "supplies": [
          {
            "id": "headphone",
            "team": 4
          },
          {
            "id": "stempler",
            "team": 4
          }
        ]
      },
      {
        "id": 5,
        "manager": "Blake Smith",
        "office": "104A",
        "extension_number": "#7750",
        "mascot": "Wildcat",
        "cleaning_duty": "Friday",
        "project": "Acheron",
        "supplies": [
          {
            "id": "calculator",
            "team": 5
          },
          {
            "id": "t shirt",
            "team": 5
          }
        ]
      }
    ]
  }
}


Playgroud์—์„œ ์œ„์™€ ๊ฐ™์ด teams ์ •๋ณด๋ฅผ ์š”์ฒญํ•  ๋•Œ, supplies์— ๋Œ€ํ•œ ๋‚ด์šฉ๋„ ์ถ”๊ฐ€ํ•ด์„œ ์š”์ฒญํ•˜๋‹ˆ ๊ฐ ํŒ€์— ํ•ด๋‹นํ•˜๋Š” Supplies ์ •๋ณด๋„ ํ•จ๊ป˜ ๊ฐ€์ ธ์˜ค๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

 

        ๐Ÿ“ฆ Mutation์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์‚ญ์ œ API๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•„์š” ๐Ÿ˜€

์œ„์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ’๋“ค์„ ๊ฐ€์ ธ์˜ค๋Š” Query์— ๋Œ€ํ•ด ์‹ค์Šตํ•ด ๋ณด์•˜์–ด์š”.
์‚ฌ์‹ค Query๋ฅผ ์ด์šฉํ•ด์„œ๋„ Resolver๋ฅผ ์–ด๋–ป๊ฒŒ ์งฐ๋Š”๊ฐ€์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์‚ฝ์ž…, ์ˆ˜์ •, ์‚ญ์ œ๋ฅผ ํ•  ์ˆ˜๋„ ์žˆ์–ด์š”.
๋˜ํ•œ, Mutation๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ•  ์ˆ˜๋„ ์žˆ์–ด์š”.

ํ•˜์ง€๋งŒ, REST API์—์„œ๋„ GET, POST, PUT/PATCH, DELETE์— ๋”ฐ๋ผ ํ•˜๋Š” ํ–‰๋™๋“ค์„ ๊ตฌ๋ถ„ํ•˜๋“ฏ์ด Query์™€ Mutation๋„
์ด์™€ ๋™์ผํ•œ ์–ด๋–ค ์•ฝ์†์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹์„ ๊ฑฐ ๊ฐ™์•„์š”.

์ด๋ฒˆ์—๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ, ์ˆ˜์ •, ์‚ญ์ œํ•˜๋Š” Mutation์— ๋Œ€ํ•ด ์‹ค์Šตํ•ด ๋ณผ๊ฒŒ์š”.

์ตœ์ดˆ Query์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Mutaion๋„ Root Mutaion Type์„ ๋งŒ๋“ค์–ด ์ค˜์•ผ ํ•ด์š”.

index.js


11 ~ 13๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด Mutaion์˜ Root Mutaion Type์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.
๊ทธ๋Ÿฐ ๋’ค deleteEquipment()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋Š”๋ฐ, ์ด ๋•Œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ๋ฌธ์ž์—ด Type์˜ ID๋ฅผ ๋ฐ›๊ณ , ๋ฐ˜ํ™˜ ๊ฐ’์€ Equipment ๊ฐ์ฒด๋ผ๊ณ  ์„ ์–ธํ•ด ์ฃผ์—ˆ์–ด์š”.

์ด๋ฒˆ์—๋Š” ํ•ด๋‹น Equipment๋ฅผ ์‚ญ์ œํ•˜๋Š” Resolver๋ฅผ ๋งŒ๋“ค์–ด ๋ณผ๊ฒŒ์š”.

index.js


์œ„์™€ ๊ฐ™์ด Mutaion Resolver๋ฅผ ์„ ์–ธํ•˜๊ณ , deleteEquipment()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.
์‹ค์ œ ์„œ๋น„์Šค ์ฝ”๋“œ์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์š”์ฒญ์„ ์œ„ํ•œ ORM ๊ด€๋ จ ๋‚ด์šฉ์ด๋ผ๋˜์ง€ SQL๋ฌธ์ด ๋“ค์–ด๊ฐˆํ…๋ฐ, ์—ฌ๊ธฐ์„  ํ…Œ์ŠคํŠธ์ด๊ณ , Mock ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์ด ์ง  ๊ฒƒ์ด์—์š”.

์ตœ์ดˆ 58 ~ 61๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๋ถ„์„ํ•ด ๋ณด๋ฉด deleteEquipment()๋Š” ์‚ญ์ œ๋  equipment๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ธ๋ฐ, ํ•ด๋‹น ID๋กœ Filtering๋œ ์‚ญ์ œ๋œ equipment์— ๋Œ€ํ•ด ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด ์ค‘ ์ฒซ๋ฒˆ์งธ ๋ฐฐ์—ด ๊ฐ’์„ deleted ์ƒ์ˆ˜ ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋‹ด๊ณ ,   

63 ~ 65๋ฒˆ์งธ ์ค„๊นŒ์ง€๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์•ˆ์— equipments๋ฅผ ์กฐํšŒํ•˜์—ฌ Filter๋ฅผ ๊ฑธ์–ด ์‚ญ์ œํ•ด ์ค€ ๋’ค 67๋ฒˆ์งธ ์ค„์— deleted ์ƒ์ˆ˜ ๋ณ€์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.


Playground

 

mutation {
  deleteEquipment(id: "notebook") {
    id
    used_by
    count
    new_or_used
  }
}


Playgroud์—์„œ ์œ„์™€ ๊ฐ™์ด deleteEquipment()๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๋ฐ, id๊ฐ€ notebook์ธ ๋‚ด์šฉ์„ ์ง€์šฐ๊ธฐ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•ด์ฃผ๊ฒŒ ๋˜๋ฉด ์‚ญ์ œ๋œ ๋‚ด์šฉ์— ๋Œ€ํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

Playground


Query๋กœ ๋‹ค์‹œ equipments๋ฅผ ์กฐํšŒํ•ด ๋ณด๋ฉด ID๊ฐ€ notebook์ธ ๊ฐ์ฒด๋Š” ์‚ญ์ œ๋˜๊ณ  ์—†๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


 

 

        ๐Ÿ“ฆ Mutation์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์‚ฝ์ž… API๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•„์š” ๐Ÿ˜€

์ด๋ฒˆ์—๋Š” ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ ์ฆ‰, ์‚ฝ์ž…์— ๋Œ€ํ•ด ์•Œ์•„๋ณผ๊ฒŒ์š”.

index.js

 

    insertEquipment (
        id: String,
        used_by: String,
        count: Int,
        new_or_used: String
    ): Equipment


์ตœ์ดˆ 12 ~ 17๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด insertEquipment()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋Š”๋ฐ, ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์•ผ ์ถ”๊ฐ€๋ฅผ ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ๊ณ , ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์ƒ์„ฑ๋œ Equipment ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

์‚ฌ์‹ค ์‹ค์ œ ์„œ๋น„์Šค ๊ตฌ์„ฑ์„ ํ•œ๋‹ค๋ฉด Equipment ID๋งŒ ์ „๋‹ฌํ•ด ์ฃผ๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด์—์š”.

์ด์ œ Resolver๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ๊ฒ ์–ด์š”.

index.js

 

        insertEquipment: (parent, args, context, info) => {
            database.equipments.push(args);
            return args;
        },


์œ„์™€ ๊ฐ™์ด insertEquipment()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”. ์ด ํ•จ์ˆ˜๋Š” ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ parent, args, context, info๋ฅผ ๋ฐ›๊ณ , ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ equipments์— ๊ฐ’์„ ๋„ฃ๋Š”๋ฐ, ํด๋ผ์ด์–ธํŠธ์—์„œ ์š”์ฒญ์œผ๋กœ ๋“ค์–ด์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ฒŒ ํ•ด ์ฃผ์—ˆ๊ณ , ์ด ์ž…๋ ฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์—์š”.


Playgroud

 

mutation {
  insertEquipment (
    id: "macbook",
    used_by: "developer",
    count: 17,
    new_or_used: "new"
  ) {
    id
    used_by
    count
    new_or_used
  }
}


์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” API ์š”์ฒญ์„ ๋ณด๋‚ด๋ณด์•˜๊ณ , ๊ฒฐ๊ณผ๋กœ ์ถ”๊ฐ€๋œ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ ๋ฐ›๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


Playgroud


๋‹ค์‹œ ํ•œ๋ฒˆ Query๋ฅผ ์ด์šฉํ•˜์—ฌ equipments ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ๊ธ์–ด์™€ ๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ฐฉ๊ธˆ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

 

        ๐Ÿ“ฆ Mutation์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ˆ˜์ • API๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•„์š” ๐Ÿ˜€

๋งˆ์ง€๋ง‰์œผ๋กœ ์ˆ˜์ • API์— ๋Œ€ํ•ด์„œ๋„ ์•Œ์•„๋ณผ๊ฒŒ์š”.

index.js


์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด Root Mutaion Type์— updateEquipment()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ˆ˜์ •์„ ํ—ˆ์šฉํ•œ ๋ณ€์ˆ˜์™€ ์ž๋ฃŒํ˜• ํƒ€์ž…์„ ๋ช…์‹œํ•ด ์ฃผ์—ˆ์–ด์š”. ๊ทธ๋Ÿฐ ๋’ค ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ๋Š” ์ˆ˜์ •๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด ์ฃผ์—ˆ์–ด์š”.

๊ทธ๋Ÿผ ์ด์ œ Resolver๋„ ๋งŒ๋“ค์–ด ์ค˜์•ผ๊ฒ ์ฃ ?

index.js


์œ„์™€ ๊ฐ™์ด Resolver์— updateEquipment()๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

 ์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์ตœ์ดˆ 69๋ฒˆ์งธ ์ค„์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์•ˆ์— equiments๋“ค์„ ๋ชจ๋‘ ์ฐพ์€ List์—์„œ ์š”์ฒญ์œผ๋กœ ๋ณด๋‚ด์ง„ ID์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์„ ์ฐพ๊ณ , args ๊ฐ์ฒด๋กœ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋กœ ๋ฎ์–ด ์”Œ์šด ๋’ค ๊ทธ ํ•ญ๋ชฉ(๋ฐฐ์—ด) ์ค‘ 0๋ฒˆ์งธ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์—์š”.

Playgroud


์ด๋ฒˆ ํ…Œ์ŠคํŠธ๋Š” ID๊ฐ€ pen tablet์ธ ๊ฒƒ์„ ์ˆ˜์ •ํ•ด ๋ณด๋ ค๊ณ  ํ•ด์š”.

์ˆ˜์ •์ „์—๋Š” ์œ„์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

Playgroud

 

mutation {
  updateEquipment (
    id: "pen tablet"
    new_or_used: "new",
    count: 30,
    used_by: "designer"
  ) {
    id
    new_or_used
    count
    used_by
  }
}


์œ„์™€ ๊ฐ™์ด ์ˆ˜์ • ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ์ˆ˜์ •ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ฐ›๋Š”๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ ,


์œ„์™€ ๊ฐ™์ด ID๊ฐ€ pen tablet์ธ ๊ฐ์ฒด ๋‚ด์šฉ์ด ๋ฐ”๋€๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



const database = require('./database');
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
  type Query {
    teams: [Team],
    team(id: Int): Team,
    equipments: [Equipment],
    supplies: [Supply]
  }
  
  type Mutation {
    insertEquipment (
        id: String,
        used_by: String,
        count: Int,
        new_or_used: String
    ): Equipment
    
    updateEquipment (
        id: String,
        used_by: String,
        count: Int,
        new_or_used: String
    ): Equipment
    
    deleteEquipment(id: String): Equipment 
  }
   
  type Team {
    id: Int,
    manager: String,
    office: String,
    extension_number: String,
    mascot: String,
    cleaning_duty: String,
    project: String,
    supplies: [Supply]
  }
  
  type Equipment {
    id: String
    used_by: String
    count: Int
    new_or_used: String
  }
  
  type Supply {
    id: String,
    team: Int
  }
`;
const resolvers = {
    Query: {
        teams: () => database.teams.map((team) => {
            team.supplies = database.supplies.filter((supply) => {
                return supply.team === team.id;
            });

            return team;
        }),

        team: (parent, args, context, info) =>
            database.teams.filter((team) => {
                return team.id === args.id;
            }) [0],

        equipments: () => database.equipments,
        supplies: () => database.supplies
    },

    Mutation: {
        insertEquipment: (parent, args, context, info) => {
            database.equipments.push(args);
            return args;
        },

        updateEquipment: (parent, args, context, info) => {
            return database.equipments.filter((equipment) => {
                return equipment.id === args.id;
            }).map((equipment) => {
                Object.assign(equipment, args);
                return equipment;
            }) [0];
        },

        deleteEquipment: (parent, args, context, info) => {
            const deleted = database.equipments.filter((equipment) => {
                return equipment.id === args.id
            })[0]

            database.equipments = database.equipments.filter((equipment) => {
                return equipment.id !== args.id
            })

            return deleted
        }
    }
}

const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
    console.log(`๐Ÿš€ ์ฃผ๋‹ˆ์˜ Apollo ์‹ค์Šต Server ์ค€๋น„ ์™„๋ฃŒ! ${url}`);
})


์ง€๊ธˆ๊นŒ์ง€ ์‹ค์Šตํ•œ ๋‚ด์šฉ์— ๋Œ€ํ•œ index.js ๋‚ด์šฉ์ด์—์š”. ๐Ÿ˜€




 

 

GraphQL๊ณผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๊ฐœ๋ฐœํ•˜๋Š” ์›น ์„œ๋น„์Šค:์„ค๊ณ„๋ถ€ํ„ฐ ๊ฐœ๋ฐœ·๋ฐฐํฌ๊นŒ์ง€ ๋”ฐ๋ผ ํ•˜๋ฉฐ ์™„์„ฑํ•˜๋Š” ์›น ํ’€

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

๐Ÿง ์ฐธ๊ณ  ์ž๋ฃŒ

ํ•ด๋‹น ๋‚ด์šฉ์€ ์•„๋ž˜ ๊ฐ•์˜๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž„์„ ์•Œ๋ ค ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

[๋ฌด๋ฃŒ] ์–„ํŒํ•œ GraphQL๊ณผ Apollo - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

โšก ์งง๊ณ  ๊ตต์€ ์ „์ฒด 90๋ถ„ ๊ฐ•์ขŒ! ์‚ฌ์ดํŠธ์˜ ์ฝ”๋“œ๋“ค์„ ๋ณต๋ถ™ํ•˜๋ฉฐ ๋น ๋ฅด๊ฒŒ GraphQL์„ ๋ฐฐ์šฐ๊ณ  ์•„ํด๋กœ ์‚ฌ์šฉ๋ฒ•์„ ์ตํžˆ์„ธ์š”., โฑ ์—ฌ๋Ÿฌ๋ถ„์˜ ์‹œ๊ฐ„์€ ์†Œ์ค‘ํ•ฉ๋‹ˆ๋‹ค. [์‚ฌ์ง„] ๐ŸŽข  GraphQL์ด ๋ญ”๊ฐ€์š”? ์„œ๋น„์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š”

www.inflearn.com

 

 

 

 

์นด์นด์˜คํŽ˜์ด | ๋งˆ์Œ ๋†“๊ณ  ๊ธˆ์œตํ•˜๋‹ค

์—ฌ๊ธฐ๋ฅผ ๋ˆŒ๋Ÿฌ ๋งํฌ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

qr.kakaopay.com

 

 

 

728x90
๋ฐ˜์‘ํ˜•