{"id":18167,"date":"2022-10-09T21:58:09","date_gmt":"2022-10-09T12:58:09","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=18167"},"modified":"2023-05-27T17:41:18","modified_gmt":"2023-05-27T08:41:18","slug":"%e3%80%90graphql%e3%80%91%e3%80%8capollo%e3%80%8d%e3%81%a8%e3%81%af%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=18167","title":{"rendered":"\u3010GraphQL\u3011\u300cApollo Server\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>Apollo\u3068\u306f\uff1f<\/h2>\n<p>GraphQL\u3092\u7c21\u5358\u306b\u6271\u3048\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306e\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u3001\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<h3>ApolloServer\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<pre class=\"lang:default decode:true\">npm i apollo-server@^2 graphql@^14.6.0<\/pre>\n<h2>Query\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"lang:default decode:true \">const { ApolloServer, gql } = require(\"apollo-server\");\r\nconst prisma = new PrismaClient();\r\n\r\n\/\/ GraphQL\u306e\u30b9\u30ad\u30fc\u30de\u5b9a\u7fa9\r\nconst typeDefs = gql`\r\n  type Query {\r\n    info: String!\r\n  }\r\n`;\r\n\r\n\/\/ \u30ea\u30be\u30eb\u30d0\u95a2\u6570\r\nconst resolvers = {\r\n  Query: {\r\n    info: () =&gt; \"\u30ea\u30bf\u30fc\u30f3info\",\r\n  },\r\n};\r\n\r\nconst server = new ApolloServer({\r\n  typeDefs,\r\n  resolvers,\r\n  context: ({ req }) =&gt; {\r\n    return {\r\n      ...req,\r\n      prisma,\r\n      \/\/\u8a8d\u8a3c\u30c8\u30fc\u30af\u30f3\u304c\u306a\u3044\u3068userId\u3092\u53d6\u5f97\u3067\u304d\u306a\u3044\u21e8post\u6295\u7a3f\u3067\u304d\u306a\u3044\u3002\r\n      userId: req &amp;&amp; req.headers.authorization ? getUserId(req) : null,\r\n    };\r\n  },\r\n  csrfPrevention: true,\r\n  cache: \"bounded\",\r\n});\r\n\r\nserver.listen().then(({ url }) =&gt; {\r\n  console.log(`&lt;img draggable=\"false\" data-mce-resize=\"false\" data-mce-placeholder=\"1\" data-wp-emoji=\"1\" class=\"emoji\" alt=\"\ud83d\ude80\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/11\/svg\/1f680.svg\"&gt;  Server ready at ${url}`);\r\n});\r\n<\/pre>\n<h3>\u30b9\u30ad\u30fc\u30de\u5b9a\u7fa9<\/h3>\n<p>info\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u6301\u3063\u3066\u3044\u3066String\u3068\u3044\u3046\u30c7\u30fc\u30bf\u578b\u306b\u306a\u3063\u3066\u3044\u308b\u3002GraphQL\u306e\u96db\u5f62\u3067\u3059\u3002<\/p>\n<h4>Query<\/h4>\n<p>Apollo Server\u3067\u4e8b\u524d\u306b\u6c7a\u3081\u3089\u308c\u3066\u3044\u308b\u6587\u5b57\u5217\u306b\u306a\u308b\u306e\u3067\u3057\u3063\u304b\u308a\u3068\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>\u30ea\u30be\u30eb\u30d0\u95a2\u6570<\/h3>\n<p>\u30b9\u30ad\u30fc\u30de\u306e\u578b\u306b\u5bfe\u3057\u3066\u4f55\u304b\u60c5\u5831\u3092\u5165\u308c\u308b\u3068\u3044\u3046\u610f\u5473\u304c\u3042\u308a\u307e\u3059\u3002\u4e0a\u306e\u4f8b\u3067\u8a00\u3048\u3070info\u3068\u3044\u3046\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u65b9\u306b\u5bfe\u3057\u3066\u60c5\u5831\u3092\u5165\u308c\u3066\u3044\u307e\u3059\u3002\u96db\u5f62\uff08\u30b9\u30ad\u30fc\u30de\u5b9a\u7fa9\uff09\u306b\u5bfe\u3057\u3066\u4f55\u304b\u60c5\u5831\u3092\u5165\u308c\u3066\u3042\u3052\u308b\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u8d77\u52d5<\/h2>\n<pre class=\"lang:default decode:true \">node server.js<\/pre>\n<p>PlayGround\u3068\u3044\u3046Apollo\u306e\u7d71\u5408\u958b\u767a\u74b0\u5883\u304c\u958b\u304d\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u305f\u3089<\/p>\n<pre class=\"lang:default decode:true \">query {\r\n  info\r\n}<\/pre>\n<p>\u4ee5\u4e0b\u306e\u7d50\u679c\u304c\u8fd4\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">{\r\n  \"data\": {\r\n    \"info\": \"\u30ea\u30bf\u30fc\u30f3info\"\r\n  }\r\n}<\/pre>\n<h2>Mutation\u30b5\u30f3\u30d7\u30eb<\/h2>\n<pre class=\"lang:default decode:true\">let links = [\r\n  {\r\n    id: \"link a\",\r\n    description: \"graphlq\u3050\",\r\n    url: \"fajko\",\r\n  },\r\n];\r\n\r\nconst typeDefs = gql`\r\n  type Mutation {\r\n    post(url: String!, description: String!): Link!\r\n  }\r\n\r\n  type Link {\r\n    id: ID!\r\n    description: String!\r\n    url: String!\r\n  }\r\n`;\r\n\r\n\r\nconst resolvers = {\r\n  Mutation: {\r\n    post: (parent, args) =&gt; {\r\n      const link = {\r\n        id: `link-a`,\r\n        description: args.description,\r\n        url: args.url,\r\n      };\r\n\r\n      links.push(link);\r\n      return link;\r\n    },\r\n  },\r\n};<\/pre>\n<p>\u305f\u3060\u3001<span style=\"color: #ff0000;\"><strong>\u3053\u306e\u30b5\u30f3\u30d7\u30eb\u3060\u3068\u3042\u304f\u307e\u3067\u30ed\u30fc\u30ab\u30ebPC\u306e\u30e1\u30e2\u30ea\u4e0a\u306b\u4fdd\u5b58\u3057\u3066\u3044\u308b(links\u3068\u3044\u3046\u914d\u5217\u306bpush\u3059\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3057\u3066\u3044\u308b)\u306e\u3067\u3001\u30c7\u30fc\u30bf\u3092\u6c38\u7d9a\u5316\u3057\u305f\u3044\u5834\u5408\u306f\u5225\u9014DB\u30b5\u30fc\u30d0\u30fc\u69cb\u7bc9\u306a\u3069\u306e\u5bfe\u5fdc\u304c\u5fc5\u8981<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>parent<\/h3>\n<p>\u30b9\u30ad\u30fc\u30de\u5b9a\u7fa9\u3067\u8a00\u3046\u3068\u3053\u308d\u306e\u89aa\u30ea\u30be\u30eb\u30d0\u95a2\u6570\u306e\u623b\u308a\u5024\u306b\u306a\u308a\u307e\u3059\u3002\u89aa\u30ea\u30be\u30eb\u30d0\u95a2\u6570\u304c\u306a\u3044\u5834\u5408\u306f<span style=\"color: #ff0000;\"><strong>null<\/strong><\/span>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>args<\/h3>\n<p>\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u6e21\u3055\u308c\u305f\u5f15\u6570\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>return link;<\/h3>\n<p>\u6700\u5f8c\u30ea\u30be\u30eb\u30d0\u95a2\u6570\uff08\u4e0a\u306e\u4f8b\u3067\u8a00\u3048\u3070\u3001post)\u3067return\u3057\u3066\u3044\u308b\u306e\u306f\u4e0a\u3067\u5b9a\u7fa9\u3057\u305f\u30b9\u30ad\u30fc\u30de\u5b9a\u7fa9\u3068\u540c\u3058\u3088\u3046\u306b\u8fd4\u3057\u3066\u3042\u3052\u3066\u307e\u3059\u3088\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u554f\u3044\u5408\u308f\u305b<\/h3>\n<pre class=\"lang:default decode:true \">mutation{\r\n  post(url:\"\u3042\u3042\u3042\",description: \"\u3042\u3042\u3042\") {\r\n    id,\r\n    description,\r\n    url\r\n  }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"Apollo\u3068\u306f\uff1f GraphQL\u3092\u7c21\u5358\u306b\u6271\u3048\u308b\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u306e\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u3001\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002 ApolloServer\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb npm i apollo-server@^2 graphql@^14. [&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\/18167"}],"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=18167"}],"version-history":[{"count":11,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18167\/revisions"}],"predecessor-version":[{"id":20059,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/18167\/revisions\/20059"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18167"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}