2018-08-02 17:02:00 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_validate/server.dart';
|
|
|
|
import 'package:dart2_constant/convert.dart';
|
2018-08-03 21:07:08 +00:00
|
|
|
import 'package:graphql_parser/graphql_parser.dart';
|
|
|
|
import 'package:graphql_schema/graphql_schema.dart';
|
2018-08-02 17:02:00 +00:00
|
|
|
import 'package:graphql_server/graphql_server.dart';
|
|
|
|
|
|
|
|
final ContentType graphQlContentType =
|
|
|
|
new ContentType('application', 'graphql');
|
|
|
|
|
|
|
|
final Validator graphQlPostBody = new Validator({
|
|
|
|
'query*': isNonEmptyString,
|
|
|
|
'operation_name': isNonEmptyString,
|
2018-08-04 01:41:16 +00:00
|
|
|
'variables': predicate((v) => v == null || v is String || v is Map),
|
2018-08-02 17:02:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
RequestHandler graphQLHttp(GraphQL graphQl) {
|
|
|
|
return (req, res) async {
|
2018-08-04 01:41:16 +00:00
|
|
|
executeMap(Map map) async {
|
|
|
|
var text = req.body['query'] as String;
|
|
|
|
var operationName = req.body['operation_name'] as String;
|
|
|
|
var variables = req.body['variables'];
|
|
|
|
|
|
|
|
if (variables is String) {
|
|
|
|
variables = json.decode(variables as String);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'data': await graphQl.parseAndExecute(
|
|
|
|
text,
|
|
|
|
sourceUrl: 'input',
|
|
|
|
operationName: operationName,
|
|
|
|
variableValues: foldToStringDynamic(variables as Map),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:07:08 +00:00
|
|
|
try {
|
2018-08-04 01:41:16 +00:00
|
|
|
if (req.method == 'GET') {
|
|
|
|
if (await validateQuery(graphQlPostBody)(req, res)) {
|
|
|
|
return await executeMap(req.query);
|
|
|
|
}
|
|
|
|
} else if (req.method == 'POST') {
|
|
|
|
if (req.headers.contentType?.mimeType == graphQlContentType.mimeType) {
|
|
|
|
var text = utf8.decode(await req.lazyOriginalBuffer());
|
2018-08-03 21:07:08 +00:00
|
|
|
return {
|
2018-08-04 01:41:16 +00:00
|
|
|
'data': await graphQl.parseAndExecute(text, sourceUrl: 'input')
|
2018-08-03 21:07:08 +00:00
|
|
|
};
|
2018-08-04 01:41:16 +00:00
|
|
|
} else if (req.headers.contentType?.mimeType == 'application/json') {
|
|
|
|
if (await validate(graphQlPostBody)(req, res)) {
|
|
|
|
return await executeMap(req.body);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new AngelHttpException.badRequest();
|
2018-08-03 21:07:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new AngelHttpException.badRequest();
|
2018-08-02 17:02:00 +00:00
|
|
|
}
|
2018-08-04 00:16:08 +00:00
|
|
|
} on ValidationException catch (e) {
|
|
|
|
var errors = <GraphQLExceptionError>[
|
|
|
|
new GraphQLExceptionError(e.message)
|
|
|
|
];
|
|
|
|
|
|
|
|
errors
|
|
|
|
.addAll(e.errors.map((ee) => new GraphQLExceptionError(ee)).toList());
|
|
|
|
return new GraphQLException(errors).toJson();
|
2018-08-03 21:07:08 +00:00
|
|
|
} on AngelHttpException catch (e) {
|
|
|
|
var errors = <GraphQLExceptionError>[
|
|
|
|
new GraphQLExceptionError(e.message)
|
|
|
|
];
|
|
|
|
|
|
|
|
errors
|
|
|
|
.addAll(e.errors.map((ee) => new GraphQLExceptionError(ee)).toList());
|
2018-08-03 23:30:19 +00:00
|
|
|
return new GraphQLException(errors).toJson();
|
2018-08-03 21:07:08 +00:00
|
|
|
} on SyntaxError catch (e) {
|
|
|
|
return new GraphQLException.fromSourceSpan(e.message, e.span);
|
|
|
|
} on GraphQLException catch (e) {
|
|
|
|
return e.toJson();
|
|
|
|
} catch (e) {
|
|
|
|
return new GraphQLException.fromMessage(e.toString()).toJson();
|
2018-08-02 17:02:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|