All Articles

πŸš€5λΆ„ μ•ˆμ— Google App Engine에 Apollo GraphQL μ„œλ²„ κ΅¬μ„±ν•˜κΈ°!

Design by @imrahelk



ꡬ글 μ•± 엔진(Google App Engine)은 μ„œλ²„ 없이 λ°±μ—”λ“œ API μ„œλ²„λ₯Ό κ΅¬μΆ•ν•˜κΈ° 쒋은 ν”Œλž«νΌμž…λ‹ˆλ‹€. κ°œλ°œμžκ°€ μ˜€λ‘œμ§€ μ•± κ°œλ°œμ—λ§Œ 집쀑할 수 μžˆμŠ΅λ‹ˆλ‹€. μ›Ήμ„œλ²„, λ‘œλ“œλ°ΈλŸ°μŠ€, λ„€νŠΈμ›Œν¬ ꡬ성 λ“± λͺ°λΌλ„ λ©λ‹ˆλ‹€. γ…Žγ…Ž
ν˜„μž¬ GAEλŠ” JAVA, PHP, Node.js, Python, C#, .Net, Ruby, Go λ“±μ˜ μ–Έμ–΄λ₯Ό μ§€μ›ν•©λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ GAE μ‚¬μ΄νŠΈλ₯Ό μ°Έκ³ ν•˜μ„Έμš”.

μ €λŠ” 2014년도에 β€œμ™œ λ ˆμ§„μ½”λ―ΉμŠ€λŠ” ꡬ글 앱엔진을 μ„ νƒν–ˆλ‚˜β€ μŠ¬λΌμ΄λ“œλ₯Ό λ³΄κ³ λ‚˜μ„œ, GAEλ₯Ό 정말 많이 μ΄μš©ν–ˆμŠ΅λ‹ˆλ‹€. GAEλŠ” PaaS(Platform as a service) μ„œλΉ„μŠ€μž…λ‹ˆλ‹€. 파슀(PaaS)말고도 SaaS, IaaS, BaaS λ“± λ‹€μ–‘ν•œ ν˜•νƒœμ˜ ν΄λΌμš°λ“œ μ„œλΉ„μŠ€κ°€ μ‘΄μž¬ν•©λ‹ˆλ‹€.

BaaSκ°€ μ˜ˆμ „μ—λŠ” *Backend as a serviceλ₯Ό μ˜λ―Έν–ˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ μ΄μ œλŠ” *Blockchain as a serviceλ₯Ό μ˜λ―Έν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ΄μ œλŠ” 블둝체인도 ν΄λΌμš°λ“œ μ„œλΉ„μŠ€ ν˜•νƒœλ‘œ μ‚¬μš©ν•˜λŠ” μ‹œλŒ€μž…λ‹ˆλ‹€. μ•„λ§ˆμ‘΄μ€ μž‘λ…„(2018)λΆ€ν„° 블둝체인 ν΄λΌμš°λ“œ μ„œλΉ„μŠ€λ₯Ό μ‹œμž‘ν–ˆμŠ΅λ‹ˆλ‹€. - AWSμ—μ„œμ˜ 블둝체인: https://aws.amazon.com/ko/blockchain/

그리고 BaaS ν•˜λ‹ˆκΉ κ΅­λ‚΄ KTHμ—μ„œ μ„œλΉ„μŠ€ν–ˆλ˜ baas.ioκ°€ μƒκ°λ‚©λ‹ˆλ‹€. baas.io도 정말 잘 μ‚¬μš©ν–ˆμ—ˆμŠ΅λ‹ˆλ‹€. ν•˜μ§€λ§Œ 정식 μ˜€ν”ˆλ„ ν•˜κΈ°μ „μ— μ—­μ‚¬μ˜ λ’€μ•ˆκΈΈλ‘œ μ‚¬λΌμ Έμ„œ λ„ˆλ¬΄ μ•ˆνƒ€κΉŒμ› μŠ΅λ‹ˆλ‹€. 참고둜 baas.ioλŠ” 가격이 무척 μ €λ ΄ν–ˆμŠ΅λ‹ˆλ‹€. baas.ioλ₯Ό λͺ¨λ₯΄μ‹œλŠ” 뢄을 μœ„ν•΄ κ΄€λ ¨ 기사λ₯Ό λ§ν¬ν•©λ‹ˆλ‹€. - KTH, λͺ¨λ°”일 μ•± 개발 λ„μš°λ―Έ μ„œλΉ„μŠ€ μΆœμ‹œ: http://www.bloter.net/archives/132383


*

μ•„λž˜μ˜ 단계λ₯Ό λ”°λ₯΄λ©΄ 5λΆ„ μ•ˆμ— Apollo GraphQL Serverλ₯Ό Google App Engine (GAE)μ—μ„œ μ‹€ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ‚΄μš©μ€ β€œApollo GraphQL Server on Google App Engine in under 5 minutes!” λΈ”λ‘œκ·Έλ₯Ό μ°Έκ³ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.


ν”„λ‘œμ νŠΈ μƒμ„±ν•˜κΈ°

apollo-graphql-server 디렉토리λ₯Ό λ§Œλ“­λ‹ˆλ‹€.

$ mkdir apollo-graphql-server
$ cd apollo-graphql-server


μ•„λž˜ λͺ…령을 μ‹€ν–‰ν•˜κ³  package.json νŒŒμΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.

$ npm init


μ•„λž˜ λͺ…령을 μ‹€ν–‰ν•˜μ—¬ ν•„μš”ν•œ λͺ¨λ“ˆμ„ μ„€μΉ˜ν•©λ‹ˆλ‹€.

$ npm install --save graphql-yoga

graphql-yogaλ₯Ό μ‚¬μš©ν•˜λ©΄ GraphQL Serverλ₯Ό 맀우 μ‰½κ²Œ κ΅¬ν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ graphql-yoga μ‚¬μ΄νŠΈμ—μ„œ ν™•μΈν•΄μ£Όμ„Έμš”.



index.js λ§Œλ“€κΈ°

ndex.js νŒŒμΌμ„ λ§Œλ“€κ³  μ•„λž˜ λ‚΄μš©μ„ μž…λ ₯ν•©λ‹ˆλ‹€.

const { GraphQLServer } = require('graphql-yoga');

const PORT = process.env.PORT || 8080;

const typeDefs = `
	type Query {
		hello: String!
	}
`;

const resolvers = {
	Query: {
		hello: () => "Hi"
	}
} 

const server = new GraphQLServer({ typeDefs, resolvers });
server.start({ port: PORT }, () => {
	console.log(`Server running on http://localhost:${PORT}`)
});

μ„œλ²„λŠ” process.env.PORT λ³€μˆ˜μ—μ„œ 포트λ₯Ό μ§€μ •ν•˜λ„λ‘ ν•©λ‹ˆλ‹€. 이것은 GAE λŸ°νƒ€μž„μ— μ˜ν•΄ μ„€μ •λœ ν™˜κ²½ λ³€μˆ˜μž…λ‹ˆλ‹€. 그리고 PORT 값은 GAE λŸ°νƒ€μž„μ— μ˜ν•΄ 8080으둜 μ„€μ •λ©λ‹ˆλ‹€.



package.json

package.json νŒŒμΌμ—μ„œ μ‹œμž‘ μŠ€ν¬λ¦½νŠΈμ™€ node 및 npm 버전을 μ§€μ •ν•©λ‹ˆλ‹€.

"scripts": { 
  "start": "npx babel-node index"
}
"engines": { 
  "node": "> = 8.16", 
  "npm": "5.x" 
}


app.yaml

GAE에 λ°°ν¬ν•˜κΈ° μœ„ν•΄μ„œλŠ” app.yaml 파일이 ν•„μš”ν•©λ‹ˆλ‹€. app.yaml νŒŒμΌμ„ λ§Œλ“€κ³  μ•„λž˜ λ‚΄μš©μ„ μž…λ ₯ν•©λ‹ˆλ‹€.

runtime: nodejs
env: flex
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10 
automatic_scaling:
  min_num_instances: 1
  max_num_instances: 20
  cool_down_period_sec: 60
  cpu_utilization:
    target_utilization: 0.80

app.yaml 파일 μ •λ³΄λŠ” 곡식 λ¬Έμ„œλ₯Ό μ°Έκ³ ν•˜μ„Έμš”.



μ•± λ°°ν¬ν•˜κΈ°

μ•„λž˜ λͺ…령을 μ‹€ν–‰ν•˜λ©΄ 앱을 λ°°ν¬ν•˜κΈ° μ‹œμž‘ν•©λ‹ˆλ‹€. 배포가 μ™„λ£Œλ˜κΈ°κΉŒμ§€ 1~2λΆ„ 정도 μ†Œμš”λ©λ‹ˆλ‹€.

gcloud app deploy



μ•± μ‹€ν–‰ν•˜κΈ°

μ•„λž˜ λͺ…령을 μ‹€ν–‰ν•˜λ©΄ κΈ°λ³Έ λΈŒλΌμš°μ €μ—μ„œ μ•± URL둜 μ΄λ™ν•©λ‹ˆλ‹€.

gcloud app browse


https://[your-project-id].appspot.com와 같은 URL둜 μ΄λ™ν•˜κ²Œ λ©λ‹ˆλ‹€. 그럼 μ•„λž˜μ™€ 같이 GraphiQL μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.


μ—¬κΈ°κΉŒμ§€ μ½μ–΄μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€.


Published 1 May 2019

μ•ˆν”Όκ³€μ˜ λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.