{"id":23956,"date":"2024-11-21T12:29:28","date_gmt":"2024-11-21T04:29:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23956\/"},"modified":"2024-11-21T12:29:28","modified_gmt":"2024-11-21T04:29:28","slug":"%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8graphql%e5%9c%a8%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23956\/","title":{"rendered":"\u5728ThinkPHP6\u4e2d\u4f7f\u7528GraphQL\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u67e5\u8be2"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0cweb\u5e94\u7528\u7a0b\u5e8f\u6b63\u5728\u8d8a\u6765\u8d8a\u53d7\u6b22\u8fce\u3002web\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u7684\u6280\u672f\u4e5f\u5728\u4e0d\u65ad\u5730\u53d1\u5c55\u548c\u6539\u8fdb\u3002\u5176\u4e2d\uff0cgraphql\u662f\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u6280\u672f\uff0c\u5b83\u53ef\u4ee5\u8ba9web\u5e94\u7528\u7a0b\u5e8f\u66f4\u9ad8\u6548\u5730\u67e5\u8be2\u548c\u83b7\u53d6\u6570\u636e\u3002\u672c\u6587\u5c06\u8bb2\u89e3\u5982\u4f55\u57286\u4e2d\u4f7f\u7528graphql\u6765\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u67e5\u8be2\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662fGraphQL<\/p>\n<p>GraphQL\u662f\u4e00\u79cd\u7531Facebook\u5f00\u53d1\u7684\u6570\u636e\u67e5\u8be2\u8bed\u8a00\u548c\u8fd0\u884c\u65f6\u73af\u5883\u3002\u5b83\u5141\u8bb8\u5ba2\u6237\u7aef\u5b9a\u4e49\u6570\u636e\u67e5\u8be2\u7684\u7ed3\u6784\uff0c\u4ee5\u53ca\u67e5\u8be2\u6240\u9700\u7684\u6570\u636e\u3002GraphQL\u901a\u8fc7\u4e00\u4e2a\u5355\u4e00\u7aef\u70b9\u6765\u89e3\u51b3\u591a\u70b9\u67e5\u8be2\u548c\u54cd\u5e94\u7684\u95ee\u9898\u3002GraphQL\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u524d\u7aef\u5f00\u53d1\u4eba\u5458\u8282\u7701\u5e26\u5bbd\u548c\u67e5\u8be2\u65f6\u95f4\uff0c\u56e0\u4e3a\u5b83\u53ea\u8fd4\u56de\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u6570\u636e\u3002<\/p>\n<p>\u4e8c\u3001 ThinkPHP6\u4e2d\u7684GraphQL<\/p>\n<p>ThinkPHP6\u662f\u4e00\u79cd\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u652f\u6301\u5404\u79cd\u6570\u636e\u5e93\u548c\u6570\u636e\u8bbf\u95ee\u65b9\u5f0f\u3002ThinkPHP6\u96c6\u6210\u4e86GraphQL\uff0c\u56e0\u6b64\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528GraphQL\u67e5\u8be2\u6570\u636e\u5e93\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4e09\u3001\u521b\u5efaGraphQL API<\/p>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528GraphQL\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5GraphQL\u6269\u5c55\u5305\uff1a<\/p>\n<pre>composer require overblog\/graphql-bundle<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u5728 config\/app.php \u4e2d\u589e\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre>return [\n   \/\/ ...\n   'graphql-bundle' =&amp;gt; [\n       'enable_listener' =&amp;gt; true,\n       'enable_profiler' =&amp;gt; true\n       \/\/ \u66f4\u591a\u9009\u9879\u53ef\u4ee5\u5728GitHub\u4e0a\u627e\u5230\n   ],\n   \/\/ ...\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u521b\u5efa\u6570\u636e\u5e93\u6a21\u578b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528GraphQL\u67e5\u8be2\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u67e5\u8be2\uff1a<\/p>\n<pre>{\n  user(id: 1) {\n    name\n    email\n    posts {\n      title\n      content\n    }\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u6570\u636e\u5e93\u4e2d\u6709\u4e00\u4e2a users \u8868\u548c\u4e00\u4e2a posts \u8868\uff0c\u90a3\u4e48\u4ee5\u4e0a\u67e5\u8be2\u5c06\u8fd4\u56de\u4e00\u4e2a\u7528\u6237\u53ca\u5176\u6240\u6709\u6587\u7ae0\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u67e5\u8be2\u3002<\/p>\n<p>\u56db\u3001GraphQL\u4e2d\u7684\u67e5\u8be2<\/p>\n<p>\u5728GraphQL\u4e2d\uff0c\u67e5\u8be2\u662f\u5ba2\u6237\u7aef\u5411\u670d\u52a1\u5668\u53d1\u51fa\u7684\u8bf7\u6c42\u3002\u6bcf\u4e2a\u67e5\u8be2\u90fd\u5177\u6709\u4e00\u4e2a\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u5728 users \u8868\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a id \u7684\u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>{\n  user(id: 1) {\n    name\n    email\n    age\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u67e5\u8be2\u4f1a\u8fd4\u56de\u4e00\u4e2a\u7528\u6237\u7684\u540d\u5b57\u3001\u7535\u5b50\u90ae\u4ef6\u548c\u5e74\u9f84\u3002<\/p>\n<p>\u4e94\u3001\u5206\u9875\u67e5\u8be2<\/p>\n<p>\u5728GraphQL\u4e2d\uff0c\u5206\u9875\u67e5\u8be2\u901a\u5e38\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u5728ThinkPHP6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 offset \u548c limit \u65b9\u6cd5\u6765\u5b9e\u73b0\u5206\u9875\u67e5\u8be2\uff1a<\/p>\n<pre>{\n  users(offset: 10, limit: 5) {\n    name\n    email\n    age\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u67e5\u8be2\u5c06\u8fd4\u56de\u4ece\u7b2c11\u9879\u5f00\u59cb\u7684\u4e94\u4e2a\u7528\u6237\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u516d\u3001\u8fc7\u6ee4\u67e5\u8be2<\/p>\n<p>\u5728GraphQL\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 where \u65b9\u6cd5\u6765\u8fc7\u6ee4\u67e5\u8be2\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u627e\u5230\u6240\u6709\u90ae\u4ef6\u5730\u5740\u4ee5 example.com \u7ed3\u5c3e\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>{\n  users(where: { email: { like: \"%example.com\" } }) {\n    name\n    email\n    age\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u67e5\u8be2\u5c06\u8fd4\u56de\u5339\u914d\u6761\u4ef6\u7684\u6240\u6709\u7528\u6237\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u4e03\u3001\u603b\u7ed3<\/p>\n<p>GraphQL\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6570\u636e\u67e5\u8be2\u8bed\u8a00\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u9ad8\u6548\u5730\u67e5\u8be2\u548c\u83b7\u53d6\u6570\u636e\u3002\u5728ThinkPHP6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528GraphQL\u6765\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002<\/p>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u53ef\u4ee5\u4e86\u89e3\u5982\u4f55\u5728ThinkPHP6\u4e2d\u5b89\u88c5\u548c\u914d\u7f6eGraphQL\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528GraphQL\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002\u540c\u65f6\u4e5f\u4ecb\u7ecd\u4e86GraphQL\u4e2d\u7684\u67e5\u8be2\u65b9\u5f0f\uff0c\u4f8b\u5982\u5206\u9875\u67e5\u8be2\u548c\u8fc7\u6ee4\u67e5\u8be2\u3002\u8fd9\u4e9b\u529f\u80fd\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u9ad8\u6548\u5730\u67e5\u8be2\u548c\u83b7\u53d6\u6570\u636e\uff0c\u63d0\u9ad8Web\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528GraphQL\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u67e5\u8be2\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u53d1\u5c55\uff0cweb\u5e94\u7528\u7a0b\u5e8f\u6b63\u5728\u8d8a\u6765\u8d8a\u53d7\u6b22\u8fce\u3002web\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u7684\u6280\u672f\u4e5f\u5728\u4e0d\u65ad\u5730\u53d1\u5c55\u548c\u6539\u8fdb\u3002\u5176\u4e2d\uff0cgraphql\u662f\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u6280\u672f\uff0c\u5b83\u53ef\u4ee5\u8ba9web\u5e94\u7528\u7a0b\u5e8f\u66f4\u9ad8\u6548\u5730\u67e5\u8be2\u548c\u83b7\u53d6\u6570\u636e\u3002\u672c\u6587\u5c06\u8bb2\u89e3\u5982\u4f55\u57286\u4e2d\u4f7f\u7528graphql\u6765\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u67e5\u8be2\u3002 \u4e00\u3001\u4ec0\u4e48\u662fGraphQL GraphQL\u662f\u4e00\u79cd\u7531Facebook\u5f00\u53d1\u7684\u6570\u636e\u67e5\u8be2\u8bed\u8a00\u548c\u8fd0\u884c\u65f6\u73af\u5883\u3002\u5b83\u5141\u8bb8\u5ba2\u6237\u7aef\u5b9a\u4e49\u6570\u636e\u67e5\u8be2\u7684\u7ed3\u6784\uff0c\u4ee5\u53ca\u67e5\u8be2\u6240\u9700\u7684\u6570\u636e\u3002GraphQL\u901a\u8fc7\u4e00\u4e2a\u5355\u4e00\u7aef\u70b9\u6765\u89e3\u51b3\u591a\u70b9\u67e5\u8be2\u548c\u54cd\u5e94\u7684\u95ee\u9898\u3002GraphQL\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u524d\u7aef\u5f00\u53d1\u4eba\u5458\u8282\u7701\u5e26\u5bbd\u548c\u67e5\u8be2\u65f6\u95f4\uff0c\u56e0\u4e3a\u5b83\u53ea\u8fd4\u56de\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u6570\u636e\u3002 \u4e8c\u3001 ThinkPHP6\u4e2d\u7684GraphQL ThinkPHP6\u662f\u4e00\u79cd\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u652f\u6301\u5404\u79cd\u6570\u636e\u5e93\u548c\u6570\u636e\u8bbf\u95ee\u65b9\u5f0f\u3002ThinkPHP6\u96c6\u6210\u4e86GraphQL\uff0c\u56e0\u6b64\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528GraphQL\u67e5\u8be2\u6570\u636e\u5e93\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4e09\u3001\u521b\u5efaGraphQL API \u5728ThinkPHP6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528GraphQL\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u67e5\u8be2\u64cd\u4f5c\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5GraphQL\u6269\u5c55\u5305\uff1a composer require overblog\/graphql-bundle \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u6210\u529f\u540e\uff0c\u5728 config\/app.php \u4e2d\u589e\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a return [ \/\/ &#8230; &#8216;graphql-bundle&#8217; =&amp;gt; [ &#8216;enable_listener&#8217; =&amp;gt; true, &#8216;enable_profiler&#8217; =&amp;gt; true \/\/ \u66f4\u591a\u9009\u9879\u53ef\u4ee5\u5728GitHub\u4e0a\u627e\u5230 ], \/\/ &#8230; ]; \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u6570\u636e\u5e93\u6a21\u578b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528GraphQL\u67e5\u8be2\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u67e5\u8be2\uff1a { user(id: 1) { name email posts { title content } } } \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c\u6570\u636e\u5e93\u4e2d\u6709\u4e00\u4e2a users \u8868\u548c\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23956","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23956"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23956\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}