platform/packages/graphql/graphql_server/test/subscription_test.dart
Tobe O 4e69153e3e Add 'packages/graphql/' from commit '33e2f86ba73d559197b6270df036256104726aca'
git-subtree-dir: packages/graphql
git-subtree-mainline: ac29392d7f
git-subtree-split: 33e2f86ba7
2020-02-15 18:22:07 -05:00

51 lines
1.3 KiB
Dart

import 'dart:async';
import 'package:graphql_schema/graphql_schema.dart';
import 'package:graphql_server/graphql_server.dart';
import 'package:test/test.dart';
void main() {
var episodes = [
{'name': 'The Phantom Menace'},
{'name': 'Attack of the Clones'},
{'name': 'Attack of the Clones'}
];
var episodesAsData = episodes.map((ep) {
return {
'data': {'prequels': ep}
};
});
Stream<Map<String, dynamic>> resolveEpisodes(_, __) =>
Stream.fromIterable(episodes)
.map((ep) => {'prequels': ep, 'not_selected': 1337});
var episodeType = objectType('Episode', fields: [
field('name', graphQLString.nonNullable()),
field('not_selected', graphQLInt),
]);
var schema = graphQLSchema(
queryType: objectType('TestQuery', fields: [
field('episodes', graphQLInt, resolve: (_, __) => episodes),
]),
subscriptionType: objectType('TestSubscription', fields: [
field('prequels', episodeType, resolve: resolveEpisodes),
]),
);
var graphQL = GraphQL(schema);
test('subscribe with selection', () async {
var stream = await graphQL.parseAndExecute('''
subscription {
prequels {
name
}
}
''') as Stream<Map<String, dynamic>>;
var asList = await stream.toList();
print(asList);
expect(asList, episodesAsData);
});
}