Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# https://github.com/dart-lang/setup-dart/blob/main/README.md
- uses: dart-lang/setup-dart@v1
with:
sdk: "3.6.1"
sdk: "3.8.3"

# Graphql Schema
- id: graphql_schema2_upgrade
Expand Down
52 changes: 29 additions & 23 deletions packages/angel_graphql/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import 'package:logging/logging.dart';
void main() async {
var logger = Logger('angel3_graphql');
var app = Angel(
reflector: MirrorsReflector(),
logger: logger
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
}));
reflector: MirrorsReflector(),
logger: logger
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
}),
);
var http = AngelHttp(app);

var todoService = app.use('api/todos', MapService());
Expand All @@ -36,9 +37,7 @@ void main() async {
'todo',
convertDartType(Todo)!,
resolve: resolveViaServiceRead(todoService),
inputs: [
GraphQLFieldInput('id', graphQLId.nonNullable()),
],
inputs: [GraphQLFieldInput('id', graphQLId.nonNullable())],
),
],
);
Expand All @@ -52,33 +51,39 @@ void main() async {
convertDartType(Todo)!,
inputs: [
GraphQLFieldInput(
'data', convertDartType(Todo)!.coerceToInputObject()),
'data',
convertDartType(Todo)!.coerceToInputObject(),
),
],
resolve: resolveViaServiceCreate(todoService),
),
],
);

var schema = graphQLSchema(
queryType: queryType,
mutationType: mutationType,
);
var schema = graphQLSchema(queryType: queryType, mutationType: mutationType);

app.all('/graphql', graphQLHttp(GraphQL(schema)));
app.get('/graphiql', graphiQL());

await todoService
.create({'text': 'Clean your room!', 'completion_status': 'COMPLETE'});
await todoService.create(
{'text': 'Take out the trash', 'completion_status': 'INCOMPLETE'});
await todoService.create({
'text': 'Clean your room!',
'completion_status': 'COMPLETE',
});
await todoService.create({
'text': 'Take out the trash',
'completion_status': 'INCOMPLETE',
});
await todoService.create({
'text': 'Become a billionaire at the age of 5',
'completion_status': 'INCOMPLETE'
'completion_status': 'INCOMPLETE',
});

var server = await http.startServer('127.0.0.1', 3000);
var uri =
Uri(scheme: 'http', host: server.address.address, port: server.port);
var uri = Uri(
scheme: 'http',
host: server.address.address,
port: server.port,
);
var graphiqlUri = uri.replace(path: 'graphiql');
print('Listening at $uri');
print('Access graphiql at $graphiqlUri');
Expand All @@ -91,7 +96,8 @@ abstract class HasText {

@serializable
@GraphQLDocumentation(
description: 'A task that might not be completed yet. **Yay! Markdown!**')
description: 'A task that might not be completed yet. **Yay! Markdown!**',
)
class Todo extends Model implements HasText {
@override
String? text;
Expand Down
41 changes: 25 additions & 16 deletions packages/angel_graphql/example/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ void main() async {

// Create an in-memory service.
var fs = LocalFileSystem();
var postService =
app.use('/api/posts', JsonFileService(fs.file('posts.json')));
var postService = app.use(
'/api/posts',
JsonFileService(fs.file('posts.json')),
);

// Also get a [Stream] of item creation events.
var postAdded = postService.afterCreated
Expand All @@ -33,10 +35,10 @@ void main() async {
.asBroadcastStream();

// GraphQL setup.
var postType = objectType('Post', fields: [
field('author', graphQLString),
field('comment', graphQLString),
]);
var postType = objectType(
'Post',
fields: [field('author', graphQLString), field('comment', graphQLString)],
);

var schema = graphQLSchema(
// Hooked up to the postService:
Expand Down Expand Up @@ -64,7 +66,9 @@ void main() async {
postType,
inputs: [
GraphQLFieldInput(
'data', postType.toInputObject('PostInput').nonNullable()),
'data',
postType.toInputObject('PostInput').nonNullable(),
),
],
resolve: resolveViaServiceCreate(postService),
),
Expand All @@ -75,22 +79,27 @@ void main() async {
// type Subscription { postAdded: Post }
subscriptionType: objectType(
'Subscription',
fields: [
field('postAdded', postType, resolve: (_, __) => postAdded),
],
fields: [field('postAdded', postType, resolve: (_, __) => postAdded)],
),
);

// Mount GraphQL routes; we'll support HTTP and WebSockets transports.
app.all('/graphql', graphQLHttp(GraphQL(schema)));
app.get('/subscriptions',
graphQLWS(GraphQL(schema), keepAliveInterval: Duration(seconds: 3)));
app.get('/graphiql',
graphiQL(subscriptionsEndpoint: 'ws://localhost:3000/subscriptions'));
app.get(
'/subscriptions',
graphQLWS(GraphQL(schema), keepAliveInterval: Duration(seconds: 3)),
);
app.get(
'/graphiql',
graphiQL(subscriptionsEndpoint: 'ws://localhost:3000/subscriptions'),
);

var server = await http.startServer('127.0.0.1', 3000);
var uri =
Uri(scheme: 'http', host: server.address.address, port: server.port);
var uri = Uri(
scheme: 'http',
host: server.address.address,
port: server.port,
);
var graphiqlUri = uri.replace(path: 'graphiql');
var postsUri = uri.replace(pathSegments: ['api', 'posts']);
print('Listening at $uri');
Expand Down
22 changes: 15 additions & 7 deletions packages/angel_graphql/lib/src/graphiql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import 'package:http_parser/http_parser.dart';
///
/// By default, the interface expects your backend to be mounted at `/graphql`; this is configurable
/// via [graphQLEndpoint].
RequestHandler graphiQL(
{String graphQLEndpoint = '/graphql', String? subscriptionsEndpoint}) {
RequestHandler graphiQL({
String graphQLEndpoint = '/graphql',
String? subscriptionsEndpoint,
}) {
return (req, res) {
res
..contentType = MediaType('text', 'html')
..write(renderGraphiql(
..write(
renderGraphiql(
graphqlEndpoint: graphQLEndpoint,
subscriptionsEndpoint: subscriptionsEndpoint))
subscriptionsEndpoint: subscriptionsEndpoint,
),
)
..close();
};
}

String renderGraphiql(
{String graphqlEndpoint = '/graphql', String? subscriptionsEndpoint}) {
String renderGraphiql({
String graphqlEndpoint = '/graphql',
String? subscriptionsEndpoint,
}) {
var subscriptionsScripts = '',
subscriptionsFetcher = '',
fetcherName = 'graphQLFetcher';
Expand All @@ -29,7 +36,8 @@ String renderGraphiql(
<script src="//unpkg.com/subscriptions-transport-ws@0.8.3/browser/client.js"></script>
<script src="//unpkg.com/graphiql-subscriptions-fetcher@0.0.2/browser/client.js"></script>
''';
subscriptionsFetcher = '''
subscriptionsFetcher =
'''
let subscriptionsClient = window.SubscriptionsTransportWs.SubscriptionClient('$subscriptionsEndpoint', {
reconnect: true
});
Expand Down
Loading