leto 0.0.1-dev.2 copy "leto: ^0.0.1-dev.2" to clipboard
leto: ^0.0.1-dev.2 copied to clipboard

Base package for implementing GraphQL servers. You might prefer `package:leto_shelf` for building GraphQL web server with leto and shelf.

example/main.dart

import 'package:leto_schema/leto_schema.dart';
import 'package:leto/leto.dart';
import 'package:test/test.dart';

void main() {
  test('single element', () async {
    final todoType = objectType<Todo>('todo', fields: [
      field(
        'text',
        graphQLString,
        resolve: (obj, args) => obj.text,
      ),
      field(
        'completed',
        graphQLBoolean,
        resolve: (obj, args) => obj.completed,
      ),
    ]);

    final schema = GraphQLSchema(
      queryType: objectType('api', fields: [
        field(
          'todos',
          listOf(todoType),
          resolve: (_, __) => [
            Todo(
              text: 'Clean your room!',
              completed: false,
              time: DateTime.now(),
            )
          ],
        ),
      ]),
    );

    final graphql = GraphQL(schema);
    final result = await graphql.parseAndExecute('{ todos { text } }');

    print(result);
    expect(result, {
      'todos': [
        {'text': 'Clean your room!'}
      ]
    });
  });
}

class Todo {
  final String? text;
  final bool? completed;
  final DateTime time;
  final List<Todo>? inner;

  Todo({
    this.text,
    this.completed,
    required this.time,
    this.inner,
  });

  Map<String, Object?> toJson() {
    return {
      'text': text,
      'completed': completed,
      'inner': inner?.map((e) => e.toJson()).toList(),
      'time': time.toIso8601String(),
    };
  }

  factory Todo.fromJson(Map<String, Object?> map) {
    return Todo(
      text: map['text'] as String?,
      completed: map['completed'] as bool?,
      time: DateTime.parse(map['time']! as String),
      inner: (map['inner'] as List<Object?>?)
          ?.map((e) => Todo.fromJson(e! as Map<String, Object?>))
          .toList(),
    );
  }

  static final serializer = SerializerValue(
    fromJson: (ctx, json) => Todo.fromJson(json),
  );
}
11
likes
140
pub points
44%
popularity

Publisher

unverified uploader

Base package for implementing GraphQL servers. You might prefer `package:leto_shelf` for building GraphQL web server with leto and shelf.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

async, clock, collection, crypto, gql, leto_schema, meta, oxidized, source_span, stream_channel

More

Packages that depend on leto