{"id":448,"date":"2022-10-29T18:46:02","date_gmt":"2022-10-29T10:46:02","guid":{"rendered":"https:\/\/blog.osvlabs.com\/?p=448"},"modified":"2022-10-29T18:46:02","modified_gmt":"2022-10-29T10:46:02","slug":"flutter-%e4%b8%ad%e4%bd%bf%e7%94%a8-graphql-graphql_flutter","status":"publish","type":"post","link":"https:\/\/blog.osvlabs.com\/?p=448","title":{"rendered":"Flutter \u4e2d\u4f7f\u7528 GraphQL (graphql_flutter)"},"content":{"rendered":"<h1>Flutter \u4e2d\u4f7f\u7528 GraphQL (graphql_flutter)<\/h1>\n<h2>\u5b89\u88c5<\/h2>\n<p>\u8fd9\u91cc\u4f7f\u7528 <a href=\"https:\/\/pub.dev\/packages\/graphql_flutter\">graphql_flutter 5.1.0<\/a><br \/>\n<code>flutter pub add graphql_flutter<\/code><\/p>\n<blockquote>\n<p>\u7248\u672c\u53d8\u5316\u633a\u5927\uff0c\u6240\u4ee5\u8fd9\u91cc\u7528\u6700\u65b0\u7684 5.1.0<\/p>\n<\/blockquote>\n<h2>\u4f7f\u7528<\/h2>\n<p>\u5728 Flutter \u4e2d\u4f7f\u7528 <a href=\"https:\/\/graphql.org\/\">GraphQL<\/a> \u8ddf\u4f7f\u7528 Restful API \u533a\u522b\u5f88\u5927\uff0cgraphql_flutter \u662f\u7528 Widget \u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <a href=\"https:\/\/pub.dev\/packages\/flutter_hooks\">flutter_hooks<\/a> \u7684\u65b9\u5f0f\uff0c\u8fd9\u91cc\u4ecb\u7ecd\u5e38\u89c4 Widget Query \u548c Mutation \u65b9\u5f0f<\/p>\n<ol>\n<li>\u521b\u5efa Client<\/li>\n<\/ol>\n<pre><code>import &#039;package:graphql_flutter\/graphql_flutter.dart&#039;;\nimport &#039;package:flutter\/material.dart&#039;;\n\nValueNotifier&lt;GraphQLClient&gt; clientFor({\n  @required String uri,\n  String subscriptionUri,\n}) {\n  Link httpLink = HttpLink(\n    uri,\n    \/\/ TODO: add headers here\n    defaultHeaders: {\n      &#039;date&#039;: DateTime.now().toString(),\n      &#039;content-type&#039;: &#039;application\/json; charset=utf-8&#039;,\n    },\n  );\n  if (subscriptionUri != null) {\n    final WebSocketLink websocketLink = WebSocketLink(\n      subscriptionUri,\n    );\n    httpLink = Link.split((request) =&gt; request.isSubscription, websocketLink, httpLink);\n  }\n\n  final AuthLink authLink = AuthLink(\n    getToken: () =&gt; &#039;Bearer ${your_token}&#039;,\n  );\n  final Link link = authLink.concat(httpLink);\n  return ValueNotifier&lt;GraphQLClient&gt;(\n    GraphQLClient(\n      cache: GraphQLCache(store: HiveStore()),\n      link: link,\n    ),\n  );\n}<\/code><\/pre>\n<ol start=\"2\">\n<li>\u521b\u5efa Provider<\/li>\n<\/ol>\n<pre><code>import &#039;package:flutter\/material.dart&#039;;\nimport &#039;package:graphql_flutter\/graphql_flutter.dart&#039;;\nimport &#039;package:xxxx\/graphql_provider.dart&#039;;\n\n\/\/\/ Wraps the root application with the `graphql_flutter` client.\n\/\/\/ We use the cache for all state management.\nclass ClientProvider extends StatelessWidget {\n  ClientProvider({\n    @required this.child,\n    @required String uri,\n    String subscriptionUri,\n  }) : client = clientFor(\n          uri: uri,\n          subscriptionUri: subscriptionUri,\n        );\n\n  final Widget child;\n  final ValueNotifier&lt;GraphQLClient&gt; client;\n\n  @override\n  Widget build(BuildContext context) {\n    return GraphQLProvider(\n      client: client,\n      child: child,\n    );\n  }\n}<\/code><\/pre>\n<ol start=\"3\">\n<li>\u5728 <code>main.dart<\/code> \u91cc <code>runApp(App());<\/code> \u524d\u52a0\u5165<code>await initHiveForFlutter(); \/\/ \u7f13\u5b58\u7528<\/code><\/li>\n<li>\u5728 <code>app.dart<\/code> \u91cc<\/li>\n<\/ol>\n<pre><code>return Builder(\n      builder: (BuildContext context) {\n        return ClientProvider(\n          child: buildApp(context),\n          uri: serverUrl,\n        );\n      },\n    )<\/code><\/pre>\n<ol start=\"5\">\n<li><strong>\u8bf7\u6c42<\/strong><br \/>\n\u8fd9\u91cc\u548c\u5e73\u5e38\u4f7f\u7528\u7684 Restful API \u6709\u5f88\u5927\u7684\u533a\u522b\uff0c \u5728\u9700\u8981\u53d1\u9001\u8bf7\u6c42\u7684\u5730\u65b9(\u6bd4\u5982 Button) \u4e0a<\/li>\n<\/ol>\n<pre><code>Mutation(\n                            options: MutationOptions(\n                              document: gql(r&#039;&#039;&#039;\n                  mutation ($phoneNumber: String!) {\n                  sendVerificationCode(phoneNumber: $phoneNumber) {\n                    ... on ErrorResult {\n                      errorCode\n                      message\n                    }\n                    ... on Other {\n                    success\n                    status\n                   result\n                  }\n                    __name\n                  }\n                }\n        &#039;&#039;&#039;),\n                              onCompleted: (dynamic resultData) {\n\n                                print(resultData);\n\n                              },\n                            ),\n                            builder: (\n                              RunMutation runMutation,\n                              QueryResult result,\n                            ) {\n\n                              if (result.hasException) {\n                                print(result.exception);\n\n                              }\n                              return Button(\n                                  onPressed: _canClick != true\n                                      ? null\n                                      : () {\n                                          runMutation({&#039;phoneNumber&#039;: &#039;123456789&#039;});\n                                        },\n                                  borderRadius: BorderRadius.circular(8),\n                                  width: 182,\n                                  height: 40,\n                                  text: &#039;Button,\n                                  fontSize: 15,\n                                  weight: FontWeight.w600,\n                                  textColor: Colors.white,\n                                  color: style.primaryColor);\n                            },\n                          ),<\/code><\/pre>\n<blockquote>\n<p><code>onCompleted<\/code> \u91cc\u8fd4\u56de\u7684\u662f\u63a5\u53e3\u6b63\u5e38\u8fd4\u56de\u7684\u4fe1\u606f<br \/>\n<code>runMutation<\/code> \u5c31\u662f\u53d1\u9001\u8bf7\u6c42, <code>runMutation({\u53c2\u6570: value})<\/code><br \/>\n<code>document<\/code> \u662f\u9700\u8981\u53d1\u9001\u7684 <code>GraphQL<\/code><br \/>\n<code>Query<\/code> \u7684\u548c <code>Mutations<\/code> \u7c7b\u4f3c\uff0c\u628a <code>Mutations<\/code> \u6539\u6210 <code>Query<\/code><\/p>\n<\/blockquote>\n<h2>\u7ed3\u5c3e<\/h2>\n<p>\u57fa\u7840\u7684\u7b80\u5355\u4f7f\u7528\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5982\u679c\u6709\u66f4\u597d\u7684\u65b9\u6cd5\u6216\u8005\u6df1\u5165\u7684\u7528\u6cd5\uff0c\u6b22\u8fce\u5206\u4eab\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flutter \u4e2d\u4f7f\u7528 GraphQL (graphql_flutter) \u5b89\u88c5 \u8fd9\u91cc\u4f7f\u7528 graphql_flutter 5.1.0 flutter pub add graphql_flutter \u2026<\/p>\n","protected":false},"author":4,"featured_media":250,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"_links":{"self":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/448"}],"collection":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=448"}],"version-history":[{"count":1,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/posts\/448\/revisions\/449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=\/wp\/v2\/media\/250"}],"wp:attachment":[{"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.osvlabs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}