gqlgen ã§ã®ãã£ãã·ã¥æ§è½åä¸ãèãã¦ã¿ã
NewsDigest ã§ã¯ã¢ããªã® BFF ã¨ã㦠GraphQL ã使ã£ã¦ãã¦ãã©ã¤ãã©ãªã¨ãã¦ã¯ gqlgen ã使ã£ã¦ã¾ããã§ããªãã¹ã CDN ã§ã®ãã£ãã·ã¥ãããçãä¸ããããªãã¨ãããã¨ã§ã gqlgen ã§ã§ãããã¨ããããã¿ã¤ããã¦ã¿ã¾ããã
åæã¨ãã¦ã
- Apollo ã¨ãã¯ä½¿ã£ã¦ãªãã¦ãcurl ã§ã®ã³ã³ã»ããã¬ãã«ã®æ¤è¨¼
- å㯠Go ãæ¸ãåãå¼±ã
- é ã®ä½æã§ãã£ã¦ãå®æ¦æå ¥ãããã®ã§ã¯ãªã
1: Persisted Query ãã¤ãã
ã¾ããPersisted Query ã使ãã¾ãã
Add File-based Persisted Query · yamitzky/example-gqlgen-cached@5917721 · GitHub
Persisted Query ã¯ãç°¡åã«è¨ãã¨ãã¯ã¨ãªèªä½ãããã·ã¥æåå(0123456789abcdefã®å)ã«ç½®æãã¦éä¿¡ãããã®ã§ããGraphQL ã®ãªã¯ã¨ã¹ããµã¤ãºã¯çµæ§ãã«ã(åé·)ãæ°kb ã¨ãã«ãªã£ããããã®ã§ããããæ°æåã®ããã·ã¥æååã«ç½®æã§ããã°ãéä¿¡ãµã¤ãºãå¤§å¹ ã«å§ç¸®ã§ãã¾ããä¾ãã°ã次ã®ãã㪠curl ã§ãã
curl -g 'http://localhost:8080/query?extensions={"persistedQuery":{"version":1,"sha256Hash":"688a6c"}}'
çè«ä¸ãã©ããªã«å¤§ããªãµã¤ãºã®ã¯ã¨ãªã§ãã£ã¦ããä¸è¨ã®ãããªå®æ°é·ã®ã¯ã¨ãªã«ç½®æã§ãã¾ããã¾ããä¸è¨ã®ããã« GET ã§ãªã¯ã¨ã¹ãããã°ãCDN ã«ãã£ã¦ãã£ãã·ã¥ããããã§ãã
ä¸è¬è«ã¨ãã¦ãgqlgen ã«æè¼ããã¦ãã APQ ã®æ©è½ã¯ãã¯ã©ã¤ã¢ã³ãå´ãããã·ã¥å¤ãåæã«ç»é²ãããã¨ãæå¾ ããã¦ãã¾ããGraphQL ã®ã¯ã¨ãªã¯ç¡éã®ãã¿ã¼ã³ãããå¾ãããã§ããä¸æ¹ã§ãGitHub ã«ä¸ããä¾ã§ã¯ãäºåã«ã¯ã¨ãªæååãã³ãããããªããã°ãªããªã ã¨ããåæãç½®ãã¾ãã(Cache.Add ãä½ãããªã)ããã©ã¤ãã¼ã㪠BFF ã¨ãã¦ä½ãã®ã§ããã°ãã©ãããã¯ã¨ãªãéããããã¯äºåã«ã³ã³ããã¼ã«ã§ããããã§ããããããã®ããã«äºåã«ãã¡ã¤ã«ã«ã³ããããã¦ãããã¨ã§ãæå³ããªãã¯ã¨ãªæååã®éãã«ãããã£ãã·ã¥ãããçã®ä½ä¸ãé²ãã ããã¯ã¨ãªç»é²ã®ãã´ã·ã¨ã¼ã·ã§ã³*1ãæ¸ãããã¨ãã§ãã¾ããã¾ã BFF ã®ãããªãã¼ã éçºãããã®ãªã許容ã§ããã§ãããã
ä½è«ã§ããã http_get.go ã模ãã¦*2 /queries/abcdef
ã¿ãããªæã㧠RESTful ã£ã½ããªã¯ã¨ã¹ãã§ãããã㪠GraphQL API ãä½ããã¨ãã§ããã¨æãã¾ã*3ã
2: Cache-Control ãè¨å®ãã
Persisted Query ã使ãã ãã ã¨ãCDN ã®ãã£ãã·ã¥æéã¯åºå®å¤ã§ããè¨å®ã§ãã¾ãããä¾ãã°é »ç¹ã«æ´æ°ããã TODO ä¸è¦§ã¨ãããã¾ãæ´æ°ãããªãã¦ã¼ã¶ã¼ä¸è¦§ãåããã£ãã·ã¥æéã ã¨ä¸ä¾¿ã§ããããã§ããªã¾ã«ãã¼å´ãã Cache-Control ãããã¼ããªã¯ã¨ã¹ãã§ããããã«ãã¦ã¿ã¾ããã
Add cache-control · yamitzky/example-gqlgen-cached@f4e956f · GitHub
GraphQL ã§ã¯ï¼åº¦ã«è¤æ°ã®ãªã½ã¼ã¹(ä¾ï¼todos 㨠users)ãåå¾ã§ãããã¨ã«ãªãã®ã§ãæçµçã«ã¯æããã£ãã·ã¥æéãçããã®ã«ãã¾ããä¾ãã°ãtodos ã¯60ç§ã®ãã£ãã·ã¥ãusers ãï¼æ¥ã®ãã£ãã·ã¥ã§è¯ãã®ã§ããã°ã
{ todos { id } }
ã»ã»ã»60ç§{ users { id } }
ã»ã»ã»86400ç§{ todos { id }, users { id } }
ã»ã»ã» 60ç§
ã¨ãããã¨ã§ããdirectiveã§è¨å®ããæ¹æ³ãããã¨æãã¾ãããåçã«å¤ããããã¢ããã¼ãã®ã»ããè¯ãã¨æãã¾ãã
ã¡ãªã¿ã«ãåããããªã³ã³ã»ããã§ããããã£ã¨è¯ãæ¹æ³ããã«ãªã¯ããã¦ã¾ã*4ã
3: resolver ã¬ãã«ã®ãã£ãã·ã¥
2 çªç®ã®æ¹æ³ã§ã¯ãï¼åº¦ã«è¤æ°ã®ãªã½ã¼ã¹ãåå¾ããå ´åãæçµçã«æãçããã£ãã·ã¥æéãæ¡ç¨ãã¦ãã¾ããããã㯠CDN ã®ãã£ãã·ã¥ã¨ãã¦ã¯é©åã§ãããCDN ã®ãã£ãã·ã¥ãåããã¨ãã«å°ããã£ãããªãã§ããä¾ãã°ã { todos { id }, users { id } }
ã«å¯¾ãã¦60ç§ã®ãã£ãã·ã¥ãè¨å®ããã¦ãã¾ããã60ç§çµéããå¾ã1æ¥ç¨åº¦ã¯ãã£ãã·ã¥ãã¦ããã¯ãã® users ãåè¨ç®ãããã¨ã«ãªãã¾ã(å®éã«ã¯ DB ã¸ã®ã¢ã¯ã»ã¹ã«ãªãã§ããã)ã
ããã§ãgqlgen ã® extension ã®ä»çµã¿ã使ã£ã¦ãresolver ã¬ãã«ã§ãã£ãã·ã¥ã§ããããã«ãã¦ã¿ã¾ãã*5ãããã§ããã°ãã§ããã ã CDN ã§ãã£ãã·ã¥ãããCDN ã®ãã£ãã·ã¥ãåããã¨ãã¦ãé©åãªç¯å²ã§ resolver ã¬ãã«ã§ã¡ã¢ãªã Redis ãªã©ã«ãã£ãã·ã¥ã§ãã¾ãã
Add resolver-level cache · yamitzky/example-gqlgen-cached@05c6e5e · GitHub
ãã®ä»ã®æ¡ç¨ããªãã£ãã¢ããã¼ã
Akamai ã¨ãã® GraphQL 対å¿ãã CDN ãã¤ããã¨ã2 㨠3 ãããå°ã綺éºã«å¯¾å¿ã§ããããªæãããã¾ãã
*1:APQ 㯠Optimistic Request ãªã®ã§ãã¡ã¢ãªãã£ãã·ã¥ã§ããã°åæ£ãããµã¼ãã¼åãã´ã·ã¨ã¼ã·ã§ã³ãå¢ãããããé¿ããã«ã¯ Redis ã®ãããªãã£ãã·ã¥ãµã¼ãã¼ã¸ã®ä½åãªãªã¯ã¨ã¹ããå¿ è¦ã«ãªãã¾ã
*2:ãããä½è«ã§ãããæè¿ã® gqlgen ã¯ãªãã¡ã¯ã¿ããããããã§ããã¾ãã³ããããã«ãã³ãã©ã¼ä½ããã®ã§ãã
*3:RESTful åæã®ã¡ããªã¯ã¹ãã¼ã«ã使ããã¨ãã®ã¡ãªãããããã¾ãããã GraphQL ã§ããå¿ è¦ãªããããã¨ããæè¦ãããããã§ããããªãã¸ããªã«ã¯ã¨ãªæååãä¿åããããã°ã¯ã©ã¤ã¢ã³ãå´ã¯ã»ãããã¼ã¿ãæè»ã«ã¨ã£ã¦ãããã®ã§ãååã« GraphQL ã®ã¡ãªããã¯äº«åã§ãã¦ãã¨æãã¾ããApollo ã¿ãããªä¾¿å© GraphQL ã¯ã©ã¤ã¢ã³ããåããã¯ãããã
*4:æ¸ãçµãããã¨ã«æ°ã¥ãã... gqlgen ã¯ããã«ã¦ã§ã¢ãããããã¼ãåçã«æ¸ããããæ¹æ³ããªãã®ã§ãã³ã¢ãªãã¸ããªãå¤æ´ããã protocol ã³ãããããã¨ã«ãªãã¾ã
*5:é¢åãããã£ãã®ã§ãTTL ã¯è¨å®ãã¦ã¾ãã