- Add dependencies to pubspec.yaml
dependencies:
graphql_fetch: any
...
dev_dependencies:
graphql_fetch_generator: any
...
- Create a program tool/build.dart:
import 'dart:async';
import 'package:build_runner/build_runner.dart';
import 'package:graphql_fetch_generator/action.dart';
var action = createBuildAction(
createSetting(
schemaUrl: "$schemaUrl"));
Future main() async {
await build([action], deleteFilesByDefault: true, writeToCache: false);
// or you can watch file changes;
// await watch([action]);
}
change $schemaUrl to your graphql schema url address or specify a schema.json file
-
Put any
*.graphql
file in your source folder -
Run
tool/build
to generate*.graphql.dart
files -
use generated code:
import 'package:graphql_fetch/graphql_fetch.dart';
import './query.graphql.dart'; // generated file
main() {
String endpoint = "...";
GraphqlClient client =new GraphqlClient(endpoint);
// use generated method
GraphqlResponse<TestResult> result =await client.query(test(first: 5));
TestResult data = result; // type-safe result
}