Global Configuration (build.yaml)
You can make global configuration for the code generation with the build.yaml
file at the root of your project (next to your pubspec.yaml
). The following configurations can be specified:
Config | Description | Type | Default |
---|---|---|---|
nullableFields | Whether to make all fields nullable by default | bool | false |
omitFields | Whether to omit all fields from the Schema. You will need to annotate each one with GraphQLField | bool | false |
omitPrivateFields | Whether to omit private fields from the Schema | bool | true |
omitFieldsNamed | A list of field names to omit by default | List<String> | ['toJson', 'toString', 'compareTo', 'toMap'] |
instantiateCode | The dependency injection code used for all Class Resolvers | String? | null |
customTypes | Types for which a custom GraphQLType is provided | List<CustomTypes> | [] |
enumValuesCase | The Enum values case type (CamelCase, snake_case, ...) | EnumNameCase? | null |
Fields
Global configurations for fields
TODO: 1G Name for ID GraphQLType (default: "id")
nullableFields (default: false)
Available in: build.yaml
, GraphQLObject
, GraphQLField
.
When true
, this will make all fields nullable by default. If you want to make a field non-nullable, you will need to configure it in the class' GraphQLObject
annotation, which applies to all the class' fields, or in the field's GraphQLField
annotation.
omitFields (default: false)
Available in: build.yaml
, GraphQLObject
, GraphQLField
.
When true
, this will omit all fields from being generated by default. If you want to generate a specific field, you will need to configure it in the class' GraphQLObject
annotation, which applies to all the class' fields, or in the field's GraphQLField
annotation.
omitPrivateFields (default: true)
Available in: build.yaml
, GraphQLObject
.
When true
, this will omit all fields that start with a underscore "_".
Following the GraphQL spec, fields that start with double underscore "__" are not allowed, they will be always be omitted.
Resolvers
Global configurations for resolvers
instantiateCode (default: null)
Available in: build.yaml
, ClassResolver
.
You can globally configure the instantiateCode
argument for ClassResolvers.
customTypes
This will allow you to specify a custom mapping from given Dart type to a GraphQLType
. The mapping is done with Dart type name
provided as a field in the build.yaml
configuration. This will override all other mapping discussed in coercing types section. It is a list of objects with the following properties:
- String name;
The name of the Dart type, if the name matches a Dart type's name during code generation, the getter
(next configuration) will be used as it's GraphQLType
.
- String getter;
The getter is the name of the Dart getter or property of returns the GraphQLType
. It should be located in the file pointed by the import
property.
- String import;
The file path where the GraphQLType
's getter
can be found.
Example
To support the Decimal
type from https://github.com/a14n/dart-decimal you can use the following code:
import 'package:decimal/decimal.dart';
import 'package:leto_schema/leto_schema.dart';
export 'package:decimal/decimal.dart';
final decimalGraphQLType = GraphQLScalarTypeValue<Decimal, String>(
name: 'Decimal',
deserialize: (_, serialized) => decimalFromJson(serialized)!,
serialize: (value) => decimalToJson(value)!,
validate: (key, input) => (input is num || input is String) &&
Decimal.tryParse(input.toString()) != null
? ValidationResult.ok(input.toString())
: ValidationResult.failure(
['Expected $key to be a number or a numeric String.'],
),
description: 'A number that allows computation without losing precision.',
specifiedByURL: null,
);
Decimal? decimalFromJson(Object? value) =>
value == null ? null : Decimal.parse(value as String);
String? decimalToJson(Decimal? value) => value?.toString();
And specify the following config in the build.yaml file.
target:
default:
builders:
leto_generator:graphql_types:
options:
customTypes:
- name: "Decimal"
import: "package:<your_package_name>/<path_to_implementation>.dart"
getter: "decimalGraphQLType"
leto_generator:graphql_resolvers:
options:
customTypes:
- name: "Decimal"
import: "package:<your_package_name>/<path_to_implementation>.dart"
getter: "decimalGraphQLType"