next.jsã§ã®ãã¡ã¤ã«ãã£ã³ã¯æé©åã®ä¸ä¾
ä»åã¯graphql-codegenã使ã説æãã¾ããä»åã®ä¾ã¯ãgraphql-codegen以å¤ã§ãçºçããå¯è½æ§ãããã¾ããèªåçæç³»ãä¸çªé¡èã«å½±é¿ãããããããã§ãã
graphql-codegenã¯ãããgraphqlã®ã¹ãã¼ãããtypescriptã®åå®ç¾©/reactã®hooksçãèªåçæããã®ã«ä½¿ããã¾ãããããã¯next.jsã¨çµã¿åãããå ´åãå°ãããªããã¼ãªé¨åãããã¾ãã
www.graphql-code-generator.com
graphql-codegenã¯ããã©ã«ãã§ã¯1ãã¡ã¤ã«ã«ãã¹ã¦åºåããã¾ãããããã«å¯¾ãnext.jsã¯åãã¼ã¸ãchunksã¨ãã¦åãããä½ãèããã«å®è£ ããã¨ããã³ãã«ããããã¡ã¤ã«éãè¨å¤§ã«ãªãå¯è½æ§ãããã¾ããnext.config.jsããwebpackã®è¨å®ãä¸æ¸ãã§ãã¾ãããoptimazationã¯ããªãä¸æ¸ããã¥ããããããä¸æ¸ãã¯åºæ¬é¿ããã¹ããªã®ã§ãã®ææ³ã¯åãã¹ãã§ã¯ãªãã§ãã
ä¾ãã°ãA
, B
, C
query ãä¾ã«ä»¥ä¸ãè¦ã¦ã¿ã¾ãããã
// generated/hooks.ts <- codegenã«ãã£ã¦ä½ããããã¡ã¤ã« import { gql } from '@apollo/client'; import * as Apollo from '@apollo/client'; export const ADocument = gql` query A { hero(episode: "JEDI") { name } droid(id: "2000") { name } } `; export function useAQuery(baseOptions?: Apollo.QueryHookOptions<AQuery, AQueryVariables>) { const options = {...defaultOptions, ...baseOptions} return Apollo.useQuery<AQuery, AQueryVariables>(ADocument, options); } export function useALazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<AQuery, AQueryVariables>) { const options = {...defaultOptions, ...baseOptions} return Apollo.useLazyQuery<AQuery, AQueryVariables>(ADocument, options); } export const BDocument = gql` query B { hero(episode: "JEDI") { name } droid(id: "2000") { name } } `; export function useBQuery(baseOptions?: Apollo.QueryHookOptions<BQuery, BQueryVariables>) { const options = {...defaultOptions, ...baseOptions} return Apollo.useQuery<BQuery, BQueryVariables>(BDocument, options); } export function useBLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<BQuery, BQueryVariables>) { const options = {...defaultOptions, ...baseOptions} return Apollo.useLazyQuery<BQuery, BQueryVariables>(BDocument, options); } export const CDocument = gql` query C { hero(episode: "JEDI") { name } droid(id: "2000") { name } } `; export function useCQuery(baseOptions?: Apollo.QueryHookOptions<CQuery, CQueryVariables>) { const options = {...defaultOptions, ...baseOptions} return Apollo.useQuery<CQuery, CQueryVariables>(CDocument, options); } export function useCLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<CQuery, CQueryVariables>) { const options = {...defaultOptions, ...baseOptions} return Apollo.useLazyQuery<CQuery, CQueryVariables>(CDocument, options); }
ä¸è¨ã®ãã¡ã¤ã«ããã³ãã«ããã¨ä»¥ä¸ã®ããã«åºåããã¾ããããã¯webpackãæé©åãè¡ãã¨ãã«æåã«tree shakingãè¡ããã¹ã¦ã®JSãã¡ã¤ã«ããimportããã¦ãããã®ã®ã¿ãå®è¡é¢æ°(ä»åã®ä¾ã ã¨e.d
ã®å¼æ°ã®ãªãã¸ã§ã¯ãå
ã®YW
çãããªã¬ã¼)ã¨ãã¦åæãã¾ããã¤ã¾ããå
¨ãã¼ã¸ã§ä½¿ããã¦ããªãqueryã¯ãã®e.d
ã®ç®æã«ã¯åæãããªããã¨ã¨ãªãã¾ãã ãã ãã使ããã¦ãªãqueryãå®ç¾©èªä½ã¯ããããã¨ã«æ³¨æãã¦ãã ããã
// query: A, query: B ã®ã¿ã誰ãã使ãCã¯ä½¿ã£ã¦ãªãåºåçµæ 7943: function (n, r, e) { "use strict"; e.d(r, { YW: function () { return b; }, $m: function () { return y; }, }); var t = e(6156), o = e(2465), u = e(7450), i = e(4569); function c() { var n = (0, o.Z)([ '\n query C {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (c = function () { return n; }), n ); } function a() { var n = (0, o.Z)([ '\n query B {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (a = function () { return n; }), n ); } function p() { var n = (0, o.Z)([ '\n query A {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (p = function () { return n; }), n ); } var d = {}, // <-- default options O = (0, u.Ps)(p()); function b(n) { // <-- query: A var r = s(s({}, d), n); return i.a(O, r); } var v = (0, u.Ps)(a()); function y(n) { // <-- query: B var r = s(s({}, d), n); return i.a(v, r); } (0, u.Ps)(c()); // <-- query: Cã¯ä½¿ããã¦ãªãã®ã§å¤æ°åãããªã },
ãã¨ãã©ãã®ãã¡ã¤ã«ããã使ããã¦ããªãå ´åã§ããçæãã¡ã¤ã«ã«query(doc)ãããã°ä»¥ä¸ã¯åºåã«å«ã¾ãã¾ãã使ããã¦ããå ´åãä¸è¨ã®exportã®é¨åã«å«ã¾ããã ãã§ãã¨ã¯åãã§ããããã¯æãç¡é§ãªé¨åã§ãããæ´ã«gqlãæååã ã£ããããã®ã§ãã¡ã¤ã«ãµã¤ãºãå§è¿«ãã¾ãã
function d() { var n = (0, u.Z)([ '\n query A {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (d = function () { return n; }), n ); }
ãã¦ãåé¡ç¹ã¨ãã¦ãã£ãè¨ã£ãnext.jsã¯åãã¼ã¸ã®ã¨ã³ããã¤ã³ããæã¤ç¹ã«æ»ã£ãå ´åãçæããããã¡ã¤ã«ãã°ãã¼ãã«ãªå ±éåãããç¶æ ã§å ¨ã¦ã®ã¨ã³ããã¤ã³ãã«ãã®ã³ã¼ã(chunkã®numberãä¸ç·) ãæ¿å ¥ãã¾ãã
ã¤ã¾ã以ä¸ã®ãããªãã¨ãçºçãã¾ãã
- Aãã¼ã¸ãquery: Aãåç § -> generatedãã¡ã¤ã«(7943)ãAã®chunkã«å«ã¾ãã
- Bãã¼ã¸ãquery: Bãåç § -> generatedãã¡ã¤ã«(7943)ãBã®chunkã«å«ã¾ãã
- Cãã¼ã¸ãquery: Cãåç § -> generatedãã¡ã¤ã«(7943)ãCã®chunkã«å«ã¾ãã
- Dãã¼ã¸ã¯ä½ããã°ãªã -> generatedãã¡ã¤ã«(7943)ãDã®chunkã«å«ã¾ããªã
ããã¯ãA, B, Cãã¼ã¸ã«ç¡é§ãªã³ã¼ããå¿ ãå«ã¾ãã¦ããã¨ãããã¨ã§ãã Aãã¼ã¸ã§query: Aãå¼ãã ã ãã«ãé¢ãããä»ã®ãã¼ã¸ã§ä½¿ããã¦ããqueryããã£ã³ã¯ã«åå¨ããããã¯Aããããã¨ä¸è¦ã§ããæ¬æ¥ããã¯ãçæãã¡ã¤ã«ãä¸ã¤ã«çµåããã¹ãã§ã¯ãªããã®è¦ªãã£ã³ã¯ã¨çµåãçæãã¡ã¤ã«ã¯å解ãããã¹ãã§ãã
以ä¸ã®ãããªã³ã¼ããæ¸ããæç¹ã§../generated/hooks
ãåç
§ãã¦ãããã¹ã¦ã®ãã£ã³ã¯ã¯ããããã«æé©åå¾ã®../generated/hooks
ãæã¤ãã¨ã«ãªãã¾ãã
import { useAQuery } from '../generated/hooks'
ãã®åé¡ã解決ããã«ã¯ï¼
2種é¡åé¿çãããã¾ãã
- _appã«å¯ãã
- 大è¦æ¨¡çæãã¡ã¤ã«ãåå²ãã
èªåã®çµè«ã¨ãã¦ã¯ã2ã¤ç®ãããªãã§ãããææª1ã¤ç®ã§ããã£ãã·ã¥ã®è¦³ç¹ããããã°åã®ä¾ããã¯ãã·ã¨ãªãã¾ãã
_appã«å¯ãã
appã¯ç¹æ®ãªãã¡ã¤ã«ã¨ãã¦ä½ç½®ã¥ãããããã¹ã¦ã®ãã¡ã¤ã«ã§å¼ã³åºããã¾ããã¤ã¾ããappã§ãã®å¤§è¦æ¨¡ãª1ãã¡ã¤ã«ãå¼ã¶ã¨åã¨ã³ããªã¼ãã¤ã³ãã«ãã../generated/hooks
ãææ ¼ãã_appã®ä¸ã«å
¥ãåã¨ã³ããªã¼ãã¤ã³ãããããªããªãã¾ããåã¨ã³ããªã¼ãã¤ã³ãããããã¨ãappãèªã¿è¾¼ãã ã¨ãã«ä¸è¦ãªqueryã大éã«å
¥ããã¨ã¯å¤ãããªãã§ãããappã¯ã©ãã§ã使ããã¡ã¤ã«ãªã®ã§ä¸åèªã¿è¾¼ãã°ãã®ã³ã¼ãèªä½ããã£ãã·ã¥ãå¹ããããããã¯ã¼ã¯å¹çã¯ä¸ããã¾ãã
大è¦æ¨¡çæãã¡ã¤ã«ãåå²ãã
queryãå¢ãã¦ããã¨æ°å/ä¸è¡ã«ãªã£ã¦ã¨ãã£ã¿ã§è¦ãã®ã大å¤ã«ãªãã®ã§æé©å以å¤ã®çç±ã§ãåããã»ããããã¨æãã¾ãã
幸ãã«ããgraphql-codegenã¯near-operation-fileãæä¾ãã¦ãããããããè¨å®ããã°å®äºã§ãã
www.graphql-code-generator.com
# codegen.yml schema: src/schema.json documents: 'src/**/*.gql' generates: src/types.ts: # åå®ç¾©ãéãã - typescript src/: # hooksã¨ãã¯ãã£ã¡ preset: near-operation-file presetConfig: baseTypesPath: types.ts # ä¸è¨ã®typesãã¤ãªãã plugins: - typescript-operations - typescript-react-apollo
ãããå®è¡ããã¨ãå.gql
ãã¡ã¤ã«ã®é£ã«tsã®ã³ã¼ããçæããã¾ãã
documents/ âââ a.generated.ts âââ a.gql âââ b.generated.ts âââ b.gql âââ c.generated.ts âââ c.gql
ãããåã¨ã³ããªã¼ãã¤ã³ããimportããã°ãã®ãã¡ã¤ã«ã ããèªã¿è¾¼ã¾ãããããã¡ã¤ã«ãµã¤ãºã¯æå°éã¨ãªããä¸è¦ãªqueryã®å®ç¾©ãå ¥ããã¨ã¯ããã¾ãããã¾ãå®å ¨ã«ç¡é§ããªããªãscope hoistingãããããçµåããç¡é§ãªé¢æ°å®è¡ãæ¸ãã¾ãã
concatenatedã¨æ¸ããã¦ããå ´åã¯ãscope hoistingãå¹ãã¦ããã¨ããããããã®ä¾ã ã¨è¦ªã®ã¨ã³ããªã¼ãã¤ã³ãã¨ã®çµåãããã¦ãã¾ãã
ãããã«
çµè«ã¨ãã¦ãã®ã±ã¼ã¹ã®å ´åãæåãªæé©åã¯ãã¡ã¤ã«ãé©åã«åå²ãããã¨ã§ãã
next.jsã¯ãä½ãæ°ã«ããã¨ãé«å質ãªã¢ããªã±ã¼ã·ã§ã³ãä½ãã¾ããããã®åæ±ç¨çãªãã®ã§ããããå¿ ãããæé©åãæ£ãããªãã¨ã¯éãã¾ããããã ãã³ãã©ã®ä¸æ¸ãã¯ãã¾ãè¯ãæ¹æ³ã§ã¯ãªããããã¥ã¼ãã³ã°ãããå ´åãä¸æ¸ã以å¤ã®æ¹æ³ã模索ããå¿ è¦ãããã¾ãããã®ä¾ã¯graphql-codegenãç¨ãã話ã§ããããããã«éãã大è¦æ¨¡ãªãã¡ã¤ã«ãæ±ã£ãå ´åã«çºçããããã©ã¼ãã³ã¹ã«å½±é¿ããå¯è½æ§ããããã注æãå¿ è¦ã§ãã
ãã¹ã¦ã®åºåãã¡ã¤ã«ã³ã¼ã
ä½ãããªãå ´åã®ã¨ã³ããªã¼ãã¤ã³ãã®ãã£ã³ã¯(Aãã¼ã¸)
(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ [9], { 3242: function (n, r, e) { "use strict"; e.r(r), e.d(r, { default: function () { return y; }, }); var t = e(5893), o = e(6156), u = e(2465), i = e(7450), c = e(4569); function a() { var n = (0, u.Z)([ '\n query C {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (a = function () { return n; }), n ); } function f() { var n = (0, u.Z)([ '\n query B {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (f = function () { return n; }), n ); } function s(n, r) { var e = Object.keys(n); if (Object.getOwnPropertySymbols) { var t = Object.getOwnPropertySymbols(n); r && (t = t.filter(function (r) { return Object.getOwnPropertyDescriptor(n, r).enumerable; })), e.push.apply(e, t); } return e; } function p(n) { for (var r = 1; r < arguments.length; r++) { var e = null != arguments[r] ? arguments[r] : {}; r % 2 ? s(Object(e), !0).forEach(function (r) { (0, o.Z)(n, r, e[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(n, Object.getOwnPropertyDescriptors(e)) : s(Object(e)).forEach(function (r) { Object.defineProperty( n, r, Object.getOwnPropertyDescriptor(e, r) ); }); } return n; } function d() { var n = (0, u.Z)([ '\n query A {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (d = function () { return n; }), n ); } var O = {}, b = (0, i.Ps)(d()); (0, i.Ps)(f()); (0, i.Ps)(a()); var y = function () { (function (n) { var r = p(p({}, O), n); return c.a(b, r); })().data; return (0, t.jsx)("h1", { children: "top" }); }; }, 7878: function (n, r, e) { (window.__NEXT_P = window.__NEXT_P || []).push([ "/a", function () { return e(3242); }, ]); }, }, function (n) { n.O(0, [971, 774, 888, 179], function () { return (r = 7878), n((n.s = r)); var r; }); var r = n.O(); _N_E = r; }, ]);
_appã«å¯ããå ´åã®ã¨ã³ããªã¼ãã¤ã³ãã®ãã£ã³ã¯(Aãã¼ã¸)
_appã«ãã¹ã¦ã®queryãæ¸ããã¦ããç¶æ
(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ [9], { 9217: function (n, u, t) { "use strict"; t.r(u); var r = t(5893), _ = t(7943); u.default = function () { (0, _.YW)().data; return (0, r.jsx)("h1", { children: "top" }); }; }, 7878: function (n, u, t) { (window.__NEXT_P = window.__NEXT_P || []).push([ "/a", function () { return t(9217); }, ]); }, }, function (n) { n.O(0, [774, 888, 179], function () { return (u = 7878), n((n.s = u)); var u; }); var u = n.O(); _N_E = u; }, ]);
scope hoistingãããã¨ã³ããªã¼ãã¤ã³ãã®ãã£ã³ã¯(Aãã¼ã¸)
(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ [9], { 4462: function (n, r, e) { "use strict"; e.r(r), e.d(r, { default: function () { return b; }, }); var t = e(5893), o = e(6156), c = e(2465), u = e(7450), i = e(4569); function f(n, r) { var e = Object.keys(n); if (Object.getOwnPropertySymbols) { var t = Object.getOwnPropertySymbols(n); r && (t = t.filter(function (r) { return Object.getOwnPropertyDescriptor(n, r).enumerable; })), e.push.apply(e, t); } return e; } function a(n) { for (var r = 1; r < arguments.length; r++) { var e = null != arguments[r] ? arguments[r] : {}; r % 2 ? f(Object(e), !0).forEach(function (r) { (0, o.Z)(n, r, e[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(n, Object.getOwnPropertyDescriptors(e)) : f(Object(e)).forEach(function (r) { Object.defineProperty( n, r, Object.getOwnPropertyDescriptor(e, r) ); }); } return n; } function p() { var n = (0, c.Z)([ '\n query A {\n hero(episode: "JEDI") {\n name\n }\n droid(id: "2000") {\n name\n }\n}\n ', ]); return ( (p = function () { return n; }), n ); } var s = {}, O = (0, u.Ps)(p()); var b = function () { (function (n) { var r = a(a({}, s), n); return i.a(O, r); })().data; return (0, t.jsx)("h1", { children: "top" }); }; }, 7878: function (n, r, e) { (window.__NEXT_P = window.__NEXT_P || []).push([ "/a", function () { return e(4462); }, ]); }, }, function (n) { n.O(0, [971, 774, 888, 179], function () { return (r = 7878), n((n.s = r)); var r; }); var r = n.O(); _N_E = r; }, ]);