Skip to main content

Lists

GraphQLListType allows you to represent a collection of values.

GraphQL Specification

This values can be of any GraphQLType and List types can be Output or Input Types if the Wrapped type is an Output or Input type. For example, a List of Union types is an Output type while a List of Strings (scalar types) can be an Output or Input type. You can use the <type>.list() method present in each GraphQLType or the listOf(<type>) global utility function to create a GraphQLListType. For example, graphQLString.list() will be a [String] in GraphQL.

Example

In GraphQL, you can represent it like this:

type Model {
listField(listInput: [String]!): [InterfaceModel!]
}

interface InterfaceModel {
name: String
}

Using Dart:

import 'package:leto_schema/leto_schema.dart';

abstract class InterfaceModel {
String get name;
}

class Model {
List<InterfaceModel>? list(List<String?> listInput) {
throw Unimplemented();
}
}

final interfaceModel = objectType<InterfaceModel>(
'InterfaceModel',
fields: [
graphQLString.field(
'name',
resolve: (InterfaceModel obj, Ctx ctx) => obj.name,
)
],
isInterface: true,
);

final model = objectType<Model>(
'Model',
fields: [
interfaceModel.nonNull().list().field(
'listField',
inputs: [
listOf(graphQLString).nonNull().inputField('listInput'),
],
resolve: (Model obj, Ctx ctx) =>
obj.listField(ctx.args['listInput'] as List<String?>)
)
]
);

With code generation, you just annotate the different classes with @GraphQLObject() (or the expected annotation) and the fields and models containing Dart Lists or non-nullable types will be generated using GraphQLListType or GraphQLNonNullType as required.

import 'package:leto_schema/leto_schema.dart';

()
abstract class InterfaceModel {
String get name;
}

()
class Model {
List<InterfaceModel>? list(List<String?> listInput) {
throw Unimplemented();
}
}