Skip to main content

Leto Generator

Generates package:leto_schema's GraphQLSchemas from annotated Dart classes and functions. This is a code-first generator which will generate different GraphQL elements based on annotations in Dart code.

Usage

Usage is very simple. You just need @GraphQLObject() annotation on any class you want to generate an object type for. And the @Query(), @Mutation() or Subscription() annotations for resolver functions.

Individual fields can have a @GraphQLDocumentation() or @GraphQLField() annotation, to provide information like descriptions, deprecation reasons, etc.

There are many more annotations that you can explore in the annotations section.

Add the following dependencies to your pubspec.yaml:

dependencies:
leto_schema:
dependencies:
leto_generator:
build_runner:

Annotate your classes, fields and functions:

import 'package:leto_schema/leto_schema.dart';

part 'file_name.g.dart';

()
class ObjectName {
final String fieldName;

const ObjectName({required this.fieldName});
}

()
ObjectName getObject(ReqCtx ctx, String name) {
return ObjectName(fieldName: name);
}

Run the code generator:

dart run build_runner watch --delete-conflicting-outputs

A file_name.g.dart should be generated with the ObjectName's GraphQLObjectType and a field for the getObject query along with a lib/graphql_api.schema.dart file with the GraphQLSchema for your project. This schema will have the getObject resolver in the root Query type.