{"id":18067,"date":"2022-10-06T08:22:58","date_gmt":"2022-10-05T23:22:58","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=18067"},"modified":"2023-05-27T20:25:44","modified_gmt":"2023-05-27T11:25:44","slug":"%e3%80%90graphql%e3%80%91%e3%80%8cgraphql-codege%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=18067","title":{"rendered":"\u3010GraphQL\u3011\u300cgraphql-codegen\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>graphql-codegen\u3068\u306f\uff1f<\/h2>\n<p>GraphQL\u306e\u30c7\u30fc\u30bf\u578b\u3092\u81ea\u52d5\u751f\u6210\u3059\u308b\u30d1\u30c3\u30b1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<h2>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n<pre class=\"lang:default decode:true \">yarn graphql-codegen init<\/pre>\n<h3>What type of application are you building?<\/h3>\n<p>\u4f7f\u3063\u3066\u3044\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u9078\u629e\u3057\u307e\u3059\u3002\u3082\u3057React\u3067\u3042\u308c\u3070\u300cApplication built with React\u300d\u3092\u9078\u629e\u3057\u307e\u3059\u3002<\/p>\n<h3>Where is your schema?<\/h3>\n<p>graphQL\u30b5\u30fc\u30d0\u30fc\u306eURL\u30d1\u30b9\uff08\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\uff09\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>Where are your operations and fragments?<\/h3>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u4e2d\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u30af\u30a8\u30ea\u306e\u5185\u5bb9\u3092\u89e3\u6790\u3057\u3066\u81ea\u52d5\u3067\u5909\u63db\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u306e\u30af\u30a8\u30ea\u306e\u5834\u6240\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\uff08\u4f8b\uff1aqueries\/**\/*.ts)<\/p>\n<h3>Where to write the output<\/h3>\n<p>\u5909\u63db\u5f8c\u30d5\u30a1\u30a4\u30eb\u306e\u51fa\u529b\u5148\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<h3>Do you want to generate an introspection file?<\/h3>\n<p>\u300cintrospection file\u3092\u4f5c\u308a\u307e\u3059\u304b\uff1f\u300d\u3068\u8a00\u308f\u308c\u3066\u3044\u308b\u306e\u3067\u57fa\u672c\u3053\u308c\u306fNo\u3067\u554f\u984c\u306a\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h3>How to name the config file?<\/h3>\n<p>codegen\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u3069\u3046\u3059\u308b\u304b\u805e\u304b\u308c\u3066\u3044\u307e\u3059\u3002\u57fa\u672c\u4f55\u3067\u3082\u826f\u3044\u3067\u3059\u304c\u3001\u308f\u304b\u308a\u3084\u3059\u3044\u3088\u3046\u306b\u300ccodegen.yml\u300d\u306a\u3069\u306b\u3057\u3066\u304a\u304f\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h3>What script in package.json should run the codegen?<\/h3>\n<p>\u81ea\u52d5\u751f\u6210\u3092\u5b9f\u884c\u3059\u308b\u30b3\u30de\u30f3\u30c9\u306e\u540d\u524d\u3067\u3059\u3002\u300cgen-types\u300d\u306a\u3069\u3068\u6307\u5b9a\u3059\u308b\u3068\u826f\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u751f\u6210\u3059\u308b\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb<\/h2>\n<pre class=\"lang:default decode:true\">overwrite: true\r\nschema: 'https:\/\/key-horse-81.hasura.app\/v1\/graphql'\r\ndocuments: 'queries\/**\/*.ts'\r\ngenerates:\r\n  types\/generated\/graphql.tsx:\r\n    plugins:\r\n      - 'typescript'\r\n      - 'typescript-operations'\r\n      - 'typescript-react-apollo'\r\n<\/pre>\n<h3>package.json<\/h3>\n<p>\u4e0a\u8a18\u8a2d\u5b9a\u306e\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u4ed8\u8fd1\u3067\u306a\u3044\u3068\u52d5\u4f5c\u3057\u306a\u3044\u306e\u3067\u6ce8\u610f\u3067\u3059\u3002<span style=\"color: #ff0000;\"><strong>\uff08\u6700\u65b0\u7248\u3068\u304b\u3060\u3068\u8907\u6570\u30d5\u30a1\u30a4\u30eb\u306b\u5206\u304b\u308c\u3066\u5909\u63db\u3055\u308c\u3066\u3057\u307e\u3044\u671f\u5f85\u3057\u305f\u52d5\u4f5c\u306b\u306a\u3089\u306a\u304f\u306a\u308a\u307e\u3059\u3002\uff09<\/strong><\/span><\/p>\n<pre class=\"lang:default decode:true\">  \"dependencies\": {\r\n    \"graphql\": \"^15.5.0\",\r\n  },\r\n  \"devDependencies\": {\r\n    \"@graphql-codegen\/cli\": \"1.21.3\",\r\n    \"@graphql-codegen\/typescript\": \"^1.21.1\",\r\n    \"@graphql-codegen\/typescript-operations\": \"1.17.15\",\r\n    \"@graphql-codegen\/typescript-react-apollo\": \"2.2.3\",\r\n  }<\/pre>\n<h3>\u6700\u7d42\u7684\u306b\u30d7\u30e9\u30b0\u30a4\u30f3\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">yarn<\/pre>\n<h2>\u30af\u30a8\u30ea\u306e\u5b9a\u7fa9<\/h2>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30af\u30a8\u30ea\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">import { gql } from '@apollo\/client'\r\n\r\nexport const GET_USERS = gql`\r\n  query GetUsers {\r\n    users(order_by: { created_at: desc }) {\r\n      id\r\n      name\r\n      created_at\r\n    }\r\n  }\r\n`<\/pre>\n<h2>\u5909\u63db\u306e\u5b9f\u884c<\/h2>\n<pre class=\"lang:default decode:true\">yarn gen-types<\/pre>\n<h3>\u6ce8\u610f\u70b9<\/h3>\n<ul>\n<li>\u4e0a\u3067\u6c7a\u3081\u305f\u30b3\u30de\u30f3\u30c9\u540d\u3092\u305d\u306e\u307e\u307e\u4f7f\u3063\u3066\u3044\u308b\u3060\u3051\u306a\u306e\u3067<span style=\"color: #ff0000;\"><strong>\u300cgen-types\u300d<\/strong><\/span>\u306f\u4efb\u610f\u306e\u30b3\u30de\u30f3\u30c9\u540d\u306b\u306a\u308a\u307e\u3059\u3002<\/li>\n<li>\u540c\u3058\u30af\u30a8\u30ea\u540d\u304c\u3042\u308b\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\u306e\u3067\u4e00\u610f\u306e\u30af\u30a8\u30ea\u540d\u306b\u3059\u308b\u3068\u826f\u3044\u3067\u3059\u3002<\/li>\n<\/ul>\n<h3>\u81ea\u52d5\u751f\u6210\u5f8c\u306e\u30d5\u30a1\u30a4\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">types\/generated\/graphql.tsx<\/pre>\n<p>\u4e0a\u8a18\u306b\u81ea\u52d5\u751f\u6210\u3055\u308c\u305f\u30c7\u30fc\u30bf\u578b\u304c\u5168\u3066\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304b\u3089\u5fc5\u8981\u306a\u578b\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u4f7f\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h3>\u751f\u6210\u3055\u308c\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u4ed5\u7d44\u307f<\/h3>\n<pre class=\"lang:default decode:true\">export type GetUsersQuery = (\r\n  { __typename?: 'query_root' }\r\n  &amp; { users: Array&lt;(\r\n    { __typename?: 'users' }\r\n    &amp; Pick&lt;Users, 'id' | 'name' | 'created_at'&gt;\r\n  )&gt; }\r\n);<\/pre>\n<h4>\u30af\u30a8\u30ea\u306e\u547d\u540d\u898f\u5247<\/h4>\n<div>\n<div>\u300c{\u5b9a\u7fa9\u3057\u305f\u30af\u30a8\u30ea\u540d}Query\u300d\u3068\u3044\u3046\u578b\u304c\u81ea\u52d5\u751f\u6210\u3055\u308c\u307e\u3059\u3002<\/div>\n<\/div>\n<h4>__typename<\/h4>\n<p>Hasura\u4e0a\u306e\u5bfe\u8c61\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u540d\u524d\u304c\u6765\u308b\u3002\u30af\u30a8\u30ea\u304c\u30eb\u30fc\u30c8\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f\u300cquery_root\u300d\u3001\u305d\u308c\u4ee5\u5916\u306e\u30c6\u30fc\u30d6\u30eb\u540d\u3068\u304b\u306e\u5834\u5408\u306f\u300cusers\u300d\u306a\u3069\u3068\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>Pick<\/h3>\n<p>\u5b9f\u969b\u306b\u53d6\u5f97\u3059\u308b\u30c7\u30fc\u30bf\u3084\u30ab\u30e9\u30e0\u540d\u306a\u3069\u306f\u3053\u3053\u306b\u5165\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"graphql-codegen\u3068\u306f\uff1f GraphQL\u306e\u30c7\u30fc\u30bf\u578b\u3092\u81ea\u52d5\u751f\u6210\u3059\u308b\u30d1\u30c3\u30b1\u30fc\u30b8\u3067\u3059\u3002 \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb yarn graphql-codegen init What type of application are y [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[125],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18067"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18067"}],"version-history":[{"count":10,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18067\/revisions"}],"predecessor-version":[{"id":20212,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18067\/revisions\/20212"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18067"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}