diff --git a/graphql_server/CHANGELOG.md b/graphql_server/CHANGELOG.md index a3d3ae6e..435668fb 100644 --- a/graphql_server/CHANGELOG.md +++ b/graphql_server/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.0.0 +* Finish testing. +* Add `package:pedantic` fixes. + # 1.0.0-rc.0 * Get the Apollo support working with the latest version of `subscriptions-transport-ws`. diff --git a/graphql_server/README.md b/graphql_server/README.md index b86c3122..2846dc57 100644 --- a/graphql_server/README.md +++ b/graphql_server/README.md @@ -1,4 +1,7 @@ # graphql_server +[![Pub](https://img.shields.io/pub/v/graphql_server.svg)](https://pub.dartlang.org/packages/graphql_server) +[![build status](https://travis-ci.org/angel-dart/graphql.svg)](https://travis-ci.org/angel-dart/graphql) + Base package for implementing GraphQL servers. You might prefer [`package:angel_graphql`](https://github.com/angel-dart/graphql), the fastest way to implement GraphQL backends in Dart. @@ -44,6 +47,34 @@ a `Stream`. Ultimately, the transport for relaying subscription events to clients is not specified in the GraphQL spec, so it's up to you. +Note that in a schema like this: + +```graphql +type TodoSubscription { + onTodo: TodoAdded! +} + +type TodoAdded { + id: ID! + text: String! + isComplete: Bool +} +``` + +Your Dart schema's resolver for `onTodo` should be +a `Map` *containing an `onTodo` key*: + +```dart +field( + 'onTodo', + todoAddedType, + resolve: (_, __) { + return someStreamOfTodos() + .map((todo) => {'onTodo': todo}); + }, +); +``` + For the purposes of reusing existing tooling (i.e. JS clients, etc.), `package:graphql_server` rolls with an implementation of Apollo's `subscriptions-transport-ws` spec. diff --git a/graphql_server/pubspec.yaml b/graphql_server/pubspec.yaml index cae28cfa..ea32cb83 100644 --- a/graphql_server/pubspec.yaml +++ b/graphql_server/pubspec.yaml @@ -1,5 +1,5 @@ name: graphql_server -version: 1.0.0-rc.0 +version: 1.0.0 author: Tobe O description: Base package for implementing GraphQL servers. You might prefer `package:angel_graphql`, the fastest way to implement GraphQL backends in Dart. homepage: https://github.com/angel-dart/graphql diff --git a/graphql_server/test/subscription_test.dart b/graphql_server/test/subscription_test.dart new file mode 100644 index 00000000..5885e03b --- /dev/null +++ b/graphql_server/test/subscription_test.dart @@ -0,0 +1,51 @@ +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> 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>; + + var asList = await stream.toList(); + print(asList); + expect(asList, episodesAsData); + }); +}