|
1 | 1 | # graphql-codegen-flutter-artemis-hooks |
2 | 2 |
|
3 | | -Generate Flutter artemis hooks |
| 3 | +this is graphql-codegen plugin to generate Flutter artemis hooks. |
4 | 4 |
|
5 | | -## Motivations |
| 5 | +## How to use |
6 | 6 |
|
7 | | -## Configuration |
| 7 | +### Install |
8 | 8 |
|
9 | | -- `isNonNullSafety` (default: null): regexp to exclude operation names |
| 9 | +``` |
| 10 | +npm i graphql-codegen-flutter-artemis-hooks |
| 11 | +``` |
10 | 12 |
|
11 | | -## Example config |
| 13 | +### Edit codegen.yml |
| 14 | + |
| 15 | +you need to specify path for generated file by artemis |
12 | 16 |
|
13 | 17 | ``` |
14 | | -overwrite: true |
15 | | -schema: |
16 | | - - 'https://myschema/graphql' |
17 | | -documents: |
18 | | - - 'src/**/*.graphql' |
| 18 | +schema: your_schema_file.graphql |
| 19 | +documents: './your_project/**/*.graphql' |
19 | 20 | generates: |
20 | | - src/@types/codegen/page.tsx: |
21 | | - config: |
22 | | - documentMode: external |
23 | | - importDocumentNodeExternallyFrom: ./graphql |
24 | | - preset: import-types |
25 | | - presetConfig: |
26 | | - typesPath: ./graphql |
27 | | - plugins: |
28 | | - - ./build/src/index.js |
| 21 | + test/output.dart: |
| 22 | + config: |
| 23 | + artemisImportPath: package:your_project/your_artemis_generated/graphql_api.dart |
| 24 | + plugins: |
| 25 | + - graphql-codegen-flutter-artemis-hooks |
| 26 | +``` |
| 27 | + |
| 28 | +then run the plugin!! |
| 29 | + |
| 30 | +## What it generates |
| 31 | + |
| 32 | +for instance, if you have defined GraphQL as following |
| 33 | + |
29 | 34 | ``` |
| 35 | +query ExampleQuery { |
| 36 | + objects { |
| 37 | + id |
| 38 | + name |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +mutation TestMutation($variable: String!) { |
| 43 | + testMutation(variable: $variable) { |
| 44 | + result |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +and using this plugin would generate code like this. |
| 50 | + |
| 51 | +``` |
| 52 | +import 'package:flutter/material.dart'; |
| 53 | +import 'package:flutter_hooks/flutter_hooks.dart'; |
| 54 | +import 'package:graphql_flutter/graphql_flutter.dart'; |
| 55 | +import 'package:gql/ast.dart'; |
| 56 | +import 'package:your_project/your_artemis_generated/graphql_api.dart'; |
| 57 | +
|
| 58 | +QueryResult useQuery<DataType>(BuildContext context, DocumentNode query, |
| 59 | + [Map<String, dynamic>? variables]) { |
| 60 | + final client = GraphQLProvider.of(context).value; |
| 61 | + final state = |
| 62 | + useState<QueryResult>(QueryResult(source: QueryResultSource.network)); |
| 63 | +
|
| 64 | + useEffect(() { |
| 65 | + late Future<QueryResult> promise; |
| 66 | +
|
| 67 | + if (variables != null) { |
| 68 | + promise = client.query( |
| 69 | + QueryOptions(document: query, variables: variables), |
| 70 | + ); |
| 71 | + } else { |
| 72 | + promise = client.query( |
| 73 | + QueryOptions(document: query), |
| 74 | + ); |
| 75 | + } |
| 76 | + promise.then((result) { |
| 77 | + state.value = result; |
| 78 | + }); |
| 79 | +
|
| 80 | + return () {}; |
| 81 | + }, []); |
| 82 | + return state.value; |
| 83 | +} |
| 84 | +
|
| 85 | +class ExampleQuery$QueryReturnType { |
| 86 | + bool isLoading; |
| 87 | + OperationException? exception; |
| 88 | + ExampleQuery$Query? data; |
| 89 | +
|
| 90 | + ExampleQuery$QueryReturnType(this.isLoading, this.exception, this.data); |
| 91 | +} |
| 92 | +
|
| 93 | +ExampleQuery$QueryReturnType useExampleQueryQuery<DataType>(BuildContext context) { |
| 94 | + final result = useQuery<ExampleQuery$Query>(context, EXAMPLE_QUERY_QUERY_DOCUMENT); |
| 95 | + return ExampleQuery$QueryReturnType(result.isLoading, result.exception, result.data == null ? null : ExampleQuery$Query.fromJson(result.data!)); |
| 96 | +} |
| 97 | +
|
| 98 | + class TestMutation$MutationReturnType { |
| 99 | + bool isLoading; |
| 100 | + OperationException? exception; |
| 101 | + TestMutation$Mutation? data; |
| 102 | +
|
| 103 | + TestMutation$MutationReturnType(this.isLoading, this.exception, this.data); |
| 104 | +} |
| 105 | +
|
| 106 | +TestMutation$MutationReturnType useTestMutationQuery<DataType>(BuildContext context, TestMutationArguments variables) { |
| 107 | + final result = useQuery<TestMutation$Mutation>(context, TEST_MUTATION_MUTATION_DOCUMENT, variables.toJson()); |
| 108 | + return TestMutation$MutationReturnType(result.isLoading, result.exception, result.data == null ? null : TestMutation$Mutation.fromJson(result.data!)); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Then you can use the generated type-safe hooks as following |
| 113 | + |
| 114 | +``` |
| 115 | +class PageWidget extends HookWidget { |
| 116 | + const PageWidget({Key key}) : super(key: key); |
| 117 | +
|
| 118 | + @override |
| 119 | + Widget build(BuildContext context) { |
| 120 | + final queryResult = useExampleQueryQuery(context); |
| 121 | + final mutationResult = useTestMutationQuery(context, TestMutationArguments(variable: "")); |
| 122 | +
|
| 123 | + return ... |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## Prerequisite |
| 129 | + |
| 130 | +This project is assumes you would use the following packages |
| 131 | + |
| 132 | +- [flutter_hooks](https://pub.dev/packages/flutter_hooks) |
| 133 | +- [graphql_flutter](https://pub.dev/packages/graphql_flutter) |
| 134 | +- [artemis](https://pub.dev/packages/artemis) |
| 135 | + |
| 136 | +## Configuration |
| 137 | + |
| 138 | +- `artemisImportPath`: |
| 139 | +- `isNonNullSafety` (default: null): set this to `true` if you want to generate code that is not null safety |
0 commit comments