data_loader 1.0.0

data_loader #

Pub build status

Batch and cache database lookups. Works well with GraphQL. Ported from the original JS version: https://github.com/graphql/dataloader

Installation #

In your pubspec.yaml:

dependencies:
  data_loader: ^1.0.0

Usage #

Complete example: https://github.com/angel-dart/graphql/blob/master/data_loader/example/main.dart

var userLoader = new DataLoader((key) => myBatchGetUsers(keys));
var invitedBy = await userLoader.load(1)then(user => userLoader.load(user.invitedByID))
print('User 1 was invited by $invitedBy'));

1.0.0 #

  • Initial version.

example/main.dart

import 'dart:async';
import 'package:data_loader/data_loader.dart';
import 'package:graphql_schema/graphql_schema.dart';

external Future<List<Todo>> fetchTodos(Iterable<int> ids);

main() async {
  // Create a DataLoader. By default, it caches lookups.
  var todoLoader = DataLoader(fetchTodos); // DataLoader<int, Todo>

  // type Todo { id: Int, text: String, is_complete: Boolean }
  var todoType = objectType(
    'Todo',
    fields: [
      field('id', graphQLInt),
      field('text', graphQLString),
      field('is_complete', graphQLBoolean),
    ],
  );

  // type Query { todo($id: Int!) Todo }
  // ignore: unused_local_variable
  var schema = graphQLSchema(
    queryType: objectType(
      'Query',
      fields: [
        field(
          'todo',
          listOf(todoType),
          inputs: [GraphQLFieldInput('id', graphQLInt.nonNullable())],
          resolve: (_, args) => todoLoader.load(args['id'] as int),
        ),
      ],
    ),
  );

  // Do something with your schema...
}

abstract class Todo {
  int get id;
  String get text;
  bool get isComplete;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  data_loader: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:data_loader/data_loader.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
29
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Aug 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:data_loader/data_loader.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
Dev dependencies
graphql_schema ^1.0.0
pedantic ^1.0.0
test >=0.12.0 <2.0.0