2018-08-02 13:53:47 +00:00
|
|
|
import 'dart:async';
|
2018-08-02 13:31:54 +00:00
|
|
|
import 'package:graphql_parser/graphql_parser.dart';
|
|
|
|
import 'package:graphql_schema/graphql_schema.dart';
|
2018-08-03 17:26:11 +00:00
|
|
|
import 'introspection.dart';
|
2018-08-02 13:31:54 +00:00
|
|
|
|
2019-04-11 17:46:04 +00:00
|
|
|
/// Transforms any [Map] into `Map<String, dynamic>`.
|
2018-08-04 01:23:45 +00:00
|
|
|
Map<String, dynamic> foldToStringDynamic(Map map) {
|
|
|
|
return map == null
|
|
|
|
? null
|
|
|
|
: map.keys.fold<Map<String, dynamic>>(
|
|
|
|
<String, dynamic>{}, (out, k) => out..[k.toString()] = map[k]);
|
|
|
|
}
|
|
|
|
|
2019-04-11 17:46:04 +00:00
|
|
|
/// A Dart implementation of a GraphQL server.
|
2018-08-02 13:31:54 +00:00
|
|
|
class GraphQL {
|
2019-04-11 17:46:04 +00:00
|
|
|
/// Any custom types to include in introspection information.
|
2018-08-04 00:57:38 +00:00
|
|
|
final List<GraphQLType> customTypes = [];
|
2019-04-11 17:46:04 +00:00
|
|
|
|
|
|
|
/// An optional callback that can be used to resolve fields from objects that are not [Map]s,
|
|
|
|
/// when the related field has no resolver.
|
|
|
|
final FutureOr<T> Function<T>(T, String, Map<String, dynamic>)
|
|
|
|
defaultFieldResolver;
|
|
|
|
|
2018-08-02 18:33:50 +00:00
|
|
|
GraphQLSchema _schema;
|
2018-08-02 13:31:54 +00:00
|
|
|
|
2018-08-04 00:57:38 +00:00
|
|
|
GraphQL(GraphQLSchema schema,
|
2019-04-24 17:06:14 +00:00
|
|
|
{bool introspect = true,
|
2019-04-11 17:46:04 +00:00
|
|
|
this.defaultFieldResolver,
|
2018-08-04 00:57:38 +00:00
|
|
|
List<GraphQLType> customTypes = const <GraphQLType>[]})
|
|
|
|
: _schema = schema {
|
|
|
|
if (customTypes?.isNotEmpty == true) {
|
|
|
|
this.customTypes.addAll(customTypes);
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:33:50 +00:00
|
|
|
if (introspect) {
|
2018-08-02 19:22:16 +00:00
|
|
|
var allTypes = <GraphQLType>[];
|
2018-08-04 00:57:38 +00:00
|
|
|
allTypes.addAll(this.customTypes);
|
2018-08-02 19:22:16 +00:00
|
|
|
_schema = reflectSchema(_schema, allTypes);
|
|
|
|
|
2018-08-04 19:18:53 +00:00
|
|
|
for (var type in allTypes.toSet()) {
|
2018-08-04 00:57:38 +00:00
|
|
|
if (!this.customTypes.contains(type)) {
|
|
|
|
this.customTypes.add(type);
|
|
|
|
}
|
2018-08-02 19:22:16 +00:00
|
|
|
}
|
2018-08-02 18:33:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 19:18:53 +00:00
|
|
|
if (_schema.queryType != null) this.customTypes.add(_schema.queryType);
|
|
|
|
if (_schema.mutationType != null)
|
|
|
|
this.customTypes.add(_schema.mutationType);
|
2019-04-18 00:52:26 +00:00
|
|
|
if (_schema.subscriptionType != null)
|
|
|
|
this.customTypes.add(_schema.subscriptionType);
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GraphQLType convertType(TypeContext ctx) {
|
|
|
|
if (ctx.listType != null) {
|
|
|
|
return new GraphQLListType(convertType(ctx.listType.type));
|
|
|
|
} else if (ctx.typeName != null) {
|
|
|
|
switch (ctx.typeName.name) {
|
|
|
|
case 'Int':
|
|
|
|
return graphQLString;
|
|
|
|
case 'Float':
|
|
|
|
return graphQLFloat;
|
|
|
|
case 'String':
|
|
|
|
return graphQLString;
|
|
|
|
case 'Boolean':
|
|
|
|
return graphQLBoolean;
|
|
|
|
case 'ID':
|
|
|
|
return graphQLId;
|
|
|
|
case 'Date':
|
|
|
|
case 'DateTime':
|
|
|
|
return graphQLDate;
|
|
|
|
default:
|
2018-08-04 00:57:38 +00:00
|
|
|
return customTypes.firstWhere((t) => t.name == ctx.typeName.name,
|
|
|
|
orElse: () => throw new ArgumentError(
|
|
|
|
'Unknown GraphQL type: "${ctx.typeName.name}"'));
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-08-02 18:33:50 +00:00
|
|
|
throw new ArgumentError('Invalid GraphQL type: "${ctx.span.text}"');
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 19:34:27 +00:00
|
|
|
Future parseAndExecute(String text,
|
2018-08-02 15:17:14 +00:00
|
|
|
{String operationName,
|
|
|
|
sourceUrl,
|
2019-04-24 17:06:14 +00:00
|
|
|
Map<String, dynamic> variableValues = const {},
|
2018-08-04 19:54:18 +00:00
|
|
|
initialValue,
|
|
|
|
Map<String, dynamic> globalVariables}) {
|
2018-08-02 15:17:14 +00:00
|
|
|
var tokens = scan(text, sourceUrl: sourceUrl);
|
|
|
|
var parser = new Parser(tokens);
|
|
|
|
var document = parser.parseDocument();
|
2018-08-04 19:18:53 +00:00
|
|
|
|
|
|
|
if (parser.errors.isNotEmpty) {
|
|
|
|
throw new GraphQLException(parser.errors
|
|
|
|
.map((e) => new GraphQLExceptionError(e.message, locations: [
|
|
|
|
new GraphExceptionErrorLocation.fromSourceLocation(e.span.start)
|
|
|
|
]))
|
|
|
|
.toList());
|
|
|
|
}
|
|
|
|
|
2018-08-04 19:54:18 +00:00
|
|
|
return executeRequest(
|
|
|
|
_schema,
|
|
|
|
document,
|
|
|
|
operationName: operationName,
|
|
|
|
initialValue: initialValue,
|
|
|
|
variableValues: variableValues,
|
|
|
|
globalVariables: globalVariables,
|
|
|
|
);
|
2018-08-02 15:17:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:34:27 +00:00
|
|
|
Future executeRequest(GraphQLSchema schema, DocumentContext document,
|
2018-08-02 15:17:14 +00:00
|
|
|
{String operationName,
|
2019-04-24 17:06:14 +00:00
|
|
|
Map<String, dynamic> variableValues = const <String, dynamic>{},
|
2018-08-04 19:54:18 +00:00
|
|
|
initialValue,
|
2019-04-24 17:06:14 +00:00
|
|
|
Map<String, dynamic> globalVariables = const <String, dynamic>{}}) async {
|
2018-08-02 13:31:54 +00:00
|
|
|
var operation = getOperation(document, operationName);
|
2018-08-04 20:14:26 +00:00
|
|
|
var coercedVariableValues = coerceVariableValues(
|
|
|
|
schema, operation, variableValues ?? <String, dynamic>{});
|
2018-08-02 13:31:54 +00:00
|
|
|
if (operation.isQuery)
|
2018-08-04 19:54:18 +00:00
|
|
|
return await executeQuery(document, operation, schema,
|
|
|
|
coercedVariableValues, initialValue, globalVariables);
|
2019-03-29 19:34:27 +00:00
|
|
|
else if (operation.isSubscription) {
|
|
|
|
return await subscribe(document, operation, schema, coercedVariableValues,
|
|
|
|
globalVariables, initialValue);
|
|
|
|
} else {
|
2018-08-04 19:54:18 +00:00
|
|
|
return executeMutation(document, operation, schema, coercedVariableValues,
|
|
|
|
initialValue, globalVariables);
|
2018-08-02 14:16:46 +00:00
|
|
|
}
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OperationDefinitionContext getOperation(
|
|
|
|
DocumentContext document, String operationName) {
|
2018-08-02 15:17:14 +00:00
|
|
|
var ops =
|
|
|
|
document.definitions.where((d) => d is OperationDefinitionContext);
|
2018-08-02 13:31:54 +00:00
|
|
|
|
|
|
|
if (operationName == null) {
|
|
|
|
return ops.length == 1
|
2018-08-02 15:17:14 +00:00
|
|
|
? ops.first as OperationDefinitionContext
|
2018-08-03 21:07:08 +00:00
|
|
|
: throw new GraphQLException.fromMessage(
|
2018-08-02 15:17:14 +00:00
|
|
|
'This document does not define any operations.');
|
2018-08-02 13:31:54 +00:00
|
|
|
} else {
|
2018-08-02 15:17:14 +00:00
|
|
|
return ops.firstWhere(
|
|
|
|
(d) => (d as OperationDefinitionContext).name == operationName,
|
2018-08-03 21:07:08 +00:00
|
|
|
orElse: () => throw new GraphQLException.fromMessage(
|
2018-08-02 15:17:14 +00:00
|
|
|
'Missing required operation "$operationName".'))
|
|
|
|
as OperationDefinitionContext;
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> coerceVariableValues(
|
|
|
|
GraphQLSchema schema,
|
|
|
|
OperationDefinitionContext operation,
|
|
|
|
Map<String, dynamic> variableValues) {
|
|
|
|
var coercedValues = <String, dynamic>{};
|
|
|
|
var variableDefinitions =
|
|
|
|
operation.variableDefinitions?.variableDefinitions ?? [];
|
|
|
|
|
|
|
|
for (var variableDefinition in variableDefinitions) {
|
|
|
|
var variableName = variableDefinition.variable.name;
|
|
|
|
var variableType = variableDefinition.type;
|
|
|
|
var defaultValue = variableDefinition.defaultValue;
|
|
|
|
var value = variableValues[variableName];
|
|
|
|
|
|
|
|
if (value == null) {
|
|
|
|
if (defaultValue != null) {
|
|
|
|
coercedValues[variableName] = defaultValue.value.value;
|
|
|
|
} else if (!variableType.isNullable) {
|
2018-08-03 21:07:08 +00:00
|
|
|
throw new GraphQLException.fromSourceSpan(
|
|
|
|
'Missing required variable "$variableName".',
|
|
|
|
variableDefinition.span);
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var type = convertType(variableType);
|
|
|
|
var validation = type.validate(variableName, value);
|
|
|
|
|
|
|
|
if (!validation.successful) {
|
2018-08-03 21:07:08 +00:00
|
|
|
throw new GraphQLException(validation.errors
|
|
|
|
.map((e) => new GraphQLExceptionError(e, locations: [
|
|
|
|
new GraphExceptionErrorLocation.fromSourceLocation(
|
|
|
|
variableDefinition.span.start)
|
|
|
|
]))
|
|
|
|
.toList());
|
2018-08-02 13:31:54 +00:00
|
|
|
} else {
|
|
|
|
coercedValues[variableName] = type.deserialize(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return coercedValues;
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:16:46 +00:00
|
|
|
Future<Map<String, dynamic>> executeQuery(
|
2018-08-02 13:31:54 +00:00
|
|
|
DocumentContext document,
|
|
|
|
OperationDefinitionContext query,
|
|
|
|
GraphQLSchema schema,
|
|
|
|
Map<String, dynamic> variableValues,
|
2018-08-04 19:54:18 +00:00
|
|
|
initialValue,
|
|
|
|
Map<String, dynamic> globalVariables) async {
|
2018-08-04 19:18:53 +00:00
|
|
|
var queryType = schema.queryType;
|
2018-08-02 13:31:54 +00:00
|
|
|
var selectionSet = query.selectionSet;
|
2018-08-04 19:54:18 +00:00
|
|
|
return await executeSelectionSet(document, selectionSet, queryType,
|
|
|
|
initialValue, variableValues, globalVariables);
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 01:29:53 +00:00
|
|
|
Future<Map<String, dynamic>> executeMutation(
|
|
|
|
DocumentContext document,
|
|
|
|
OperationDefinitionContext mutation,
|
|
|
|
GraphQLSchema schema,
|
|
|
|
Map<String, dynamic> variableValues,
|
2018-08-04 19:54:18 +00:00
|
|
|
initialValue,
|
|
|
|
Map<String, dynamic> globalVariables) async {
|
2018-08-04 19:18:53 +00:00
|
|
|
var mutationType = schema.mutationType;
|
2018-08-04 01:29:53 +00:00
|
|
|
|
|
|
|
if (mutationType == null) {
|
2018-08-04 15:01:49 +00:00
|
|
|
throw new GraphQLException.fromMessage(
|
|
|
|
'The schema does not define a mutation type.');
|
2018-08-04 01:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var selectionSet = mutation.selectionSet;
|
2018-08-04 19:54:18 +00:00
|
|
|
return await executeSelectionSet(document, selectionSet, mutationType,
|
|
|
|
initialValue, variableValues, globalVariables);
|
2018-08-04 01:29:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:34:27 +00:00
|
|
|
Future<Stream<Map<String, dynamic>>> subscribe(
|
|
|
|
DocumentContext document,
|
|
|
|
OperationDefinitionContext subscription,
|
|
|
|
GraphQLSchema schema,
|
|
|
|
Map<String, dynamic> variableValues,
|
|
|
|
Map<String, dynamic> globalVariables,
|
|
|
|
initialValue) async {
|
|
|
|
var sourceStream = await createSourceEventStream(
|
|
|
|
document, subscription, schema, variableValues, initialValue);
|
|
|
|
return mapSourceToResponseEvent(sourceStream, subscription, schema,
|
|
|
|
document, initialValue, variableValues, globalVariables);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Stream> createSourceEventStream(
|
|
|
|
DocumentContext document,
|
|
|
|
OperationDefinitionContext subscription,
|
|
|
|
GraphQLSchema schema,
|
|
|
|
Map<String, dynamic> variableValues,
|
|
|
|
initialValue) {
|
|
|
|
var selectionSet = subscription.selectionSet;
|
|
|
|
var subscriptionType = schema.subscriptionType;
|
|
|
|
if (subscriptionType == null)
|
|
|
|
throw GraphQLException.fromSourceSpan(
|
|
|
|
'The schema does not define a subscription type.', subscription.span);
|
|
|
|
var groupedFieldSet =
|
|
|
|
collectFields(document, subscriptionType, selectionSet, variableValues);
|
|
|
|
if (groupedFieldSet.length != 1)
|
|
|
|
throw GraphQLException.fromSourceSpan(
|
|
|
|
'The grouped field set from this query must have exactly one entry.',
|
|
|
|
selectionSet.span);
|
|
|
|
var fields = groupedFieldSet.entries.first.value;
|
2019-04-11 16:49:21 +00:00
|
|
|
var fieldName = fields.first.field.fieldName.alias?.name ??
|
|
|
|
fields.first.field.fieldName.name;
|
2019-03-29 19:34:27 +00:00
|
|
|
var field = fields.first;
|
|
|
|
var argumentValues =
|
|
|
|
coerceArgumentValues(subscriptionType, field, variableValues);
|
|
|
|
return resolveFieldEventStream(
|
|
|
|
subscriptionType, initialValue, fieldName, argumentValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<Map<String, dynamic>> mapSourceToResponseEvent(
|
|
|
|
Stream sourceStream,
|
|
|
|
OperationDefinitionContext subscription,
|
|
|
|
GraphQLSchema schema,
|
|
|
|
DocumentContext document,
|
|
|
|
initialValue,
|
|
|
|
Map<String, dynamic> variableValues,
|
|
|
|
Map<String, dynamic> globalVariables,
|
|
|
|
) async* {
|
|
|
|
await for (var event in sourceStream) {
|
|
|
|
yield await executeSubscriptionEvent(document, subscription, schema,
|
2019-04-18 00:52:26 +00:00
|
|
|
event, variableValues, globalVariables);
|
2019-03-29 19:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>> executeSubscriptionEvent(
|
|
|
|
DocumentContext document,
|
|
|
|
OperationDefinitionContext subscription,
|
|
|
|
GraphQLSchema schema,
|
|
|
|
initialValue,
|
|
|
|
Map<String, dynamic> variableValues,
|
2019-04-18 00:52:26 +00:00
|
|
|
Map<String, dynamic> globalVariables) async {
|
2019-03-29 19:34:27 +00:00
|
|
|
var selectionSet = subscription.selectionSet;
|
|
|
|
var subscriptionType = schema.subscriptionType;
|
|
|
|
if (subscriptionType == null)
|
|
|
|
throw GraphQLException.fromSourceSpan(
|
|
|
|
'The schema does not define a subscription type.', subscription.span);
|
|
|
|
|
|
|
|
try {
|
|
|
|
var data = await executeSelectionSet(document, selectionSet,
|
|
|
|
subscriptionType, initialValue, variableValues, globalVariables);
|
2019-04-18 00:52:26 +00:00
|
|
|
return {'data': data};
|
2019-03-29 19:34:27 +00:00
|
|
|
} on GraphQLException catch (e) {
|
|
|
|
return {
|
|
|
|
'data': null,
|
|
|
|
'errors': [e.errors.map((e) => e.toJson()).toList()]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Stream> resolveFieldEventStream(GraphQLObjectType subscriptionType,
|
|
|
|
rootValue, String fieldName, Map<String, dynamic> argumentValues) async {
|
|
|
|
var field = subscriptionType.fields.firstWhere((f) => f.name == fieldName,
|
|
|
|
orElse: () {
|
|
|
|
throw GraphQLException.fromMessage(
|
|
|
|
'No subscription field named "$fieldName" is defined.');
|
|
|
|
});
|
|
|
|
var resolver = field.resolve;
|
|
|
|
var result = await resolver(rootValue, argumentValues);
|
|
|
|
if (result is Stream)
|
|
|
|
return result;
|
|
|
|
else
|
|
|
|
return Stream.fromIterable([result]);
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:53:47 +00:00
|
|
|
Future<Map<String, dynamic>> executeSelectionSet(
|
2018-08-02 13:31:54 +00:00
|
|
|
DocumentContext document,
|
|
|
|
SelectionSetContext selectionSet,
|
|
|
|
GraphQLObjectType objectType,
|
|
|
|
objectValue,
|
2018-08-04 19:54:18 +00:00
|
|
|
Map<String, dynamic> variableValues,
|
|
|
|
Map<String, dynamic> globalVariables) async {
|
2018-08-02 13:31:54 +00:00
|
|
|
var groupedFieldSet =
|
2018-08-02 13:50:31 +00:00
|
|
|
collectFields(document, objectType, selectionSet, variableValues);
|
2018-08-02 13:31:54 +00:00
|
|
|
var resultMap = <String, dynamic>{};
|
|
|
|
|
|
|
|
for (var responseKey in groupedFieldSet.keys) {
|
|
|
|
var fields = groupedFieldSet[responseKey];
|
|
|
|
|
|
|
|
for (var field in fields) {
|
2019-04-11 16:49:21 +00:00
|
|
|
var fieldName =
|
|
|
|
field.field.fieldName.alias?.name ?? field.field.fieldName.name;
|
2018-08-04 19:41:40 +00:00
|
|
|
var responseValue;
|
|
|
|
|
|
|
|
if (fieldName == '__typename') {
|
|
|
|
responseValue = objectType.name;
|
|
|
|
} else {
|
|
|
|
var fieldType = objectType.fields
|
|
|
|
.firstWhere((f) => f.name == fieldName, orElse: () => null)
|
|
|
|
?.type;
|
|
|
|
if (fieldType == null) continue;
|
2018-08-04 19:54:18 +00:00
|
|
|
responseValue = await executeField(
|
|
|
|
document,
|
|
|
|
fieldName,
|
|
|
|
objectType,
|
|
|
|
objectValue,
|
|
|
|
fields,
|
|
|
|
fieldType,
|
2018-08-05 00:47:23 +00:00
|
|
|
new Map<String, dynamic>.from(
|
|
|
|
globalVariables ?? <String, dynamic>{})
|
2018-08-04 19:54:18 +00:00
|
|
|
..addAll(variableValues),
|
|
|
|
globalVariables);
|
2018-08-04 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 13:31:54 +00:00
|
|
|
resultMap[responseKey] = responseValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultMap;
|
|
|
|
}
|
|
|
|
|
2018-08-02 17:02:00 +00:00
|
|
|
Future executeField(
|
2018-08-02 14:16:46 +00:00
|
|
|
DocumentContext document,
|
|
|
|
String fieldName,
|
2018-08-02 13:31:54 +00:00
|
|
|
GraphQLObjectType objectType,
|
|
|
|
objectValue,
|
|
|
|
List<SelectionContext> fields,
|
|
|
|
GraphQLType fieldType,
|
2018-08-04 19:54:18 +00:00
|
|
|
Map<String, dynamic> variableValues,
|
|
|
|
Map<String, dynamic> globalVariables) async {
|
2018-08-02 13:31:54 +00:00
|
|
|
var field = fields[0];
|
|
|
|
var argumentValues =
|
2018-08-02 13:50:31 +00:00
|
|
|
coerceArgumentValues(objectType, field, variableValues);
|
2018-08-02 13:53:47 +00:00
|
|
|
var resolvedValue = await resolveFieldValue(
|
2019-08-14 04:43:19 +00:00
|
|
|
objectType,
|
|
|
|
objectValue,
|
|
|
|
fieldName,
|
|
|
|
Map<String, dynamic>.from(globalVariables)..addAll(argumentValues));
|
2018-08-04 19:54:18 +00:00
|
|
|
return completeValue(document, fieldName, fieldType, fields, resolvedValue,
|
|
|
|
variableValues, globalVariables);
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, dynamic> coerceArgumentValues(GraphQLObjectType objectType,
|
|
|
|
SelectionContext field, Map<String, dynamic> variableValues) {
|
|
|
|
var coercedValues = <String, dynamic>{};
|
|
|
|
var argumentValues = field.field.arguments;
|
2019-04-11 16:49:21 +00:00
|
|
|
var fieldName =
|
|
|
|
field.field.fieldName.alias?.name ?? field.field.fieldName.name;
|
2019-04-18 00:52:26 +00:00
|
|
|
var desiredField = objectType.fields.firstWhere((f) => f.name == fieldName,
|
|
|
|
orElse: () => throw FormatException(
|
|
|
|
'${objectType.name} has no field named "$fieldName".'));
|
2018-08-04 19:18:53 +00:00
|
|
|
var argumentDefinitions = desiredField.inputs;
|
2018-08-02 13:50:31 +00:00
|
|
|
|
|
|
|
for (var argumentDefinition in argumentDefinitions) {
|
|
|
|
var argumentName = argumentDefinition.name;
|
|
|
|
var argumentType = argumentDefinition.type;
|
|
|
|
var defaultValue = argumentDefinition.defaultValue;
|
2018-08-04 20:14:26 +00:00
|
|
|
|
2018-08-02 13:50:31 +00:00
|
|
|
var value = argumentValues.firstWhere((a) => a.name == argumentName,
|
|
|
|
orElse: () => null);
|
|
|
|
|
2018-08-02 18:33:50 +00:00
|
|
|
if (value?.valueOrVariable?.variable != null) {
|
|
|
|
var variableName = value.valueOrVariable.variable.name;
|
2018-08-02 13:50:31 +00:00
|
|
|
var variableValue = variableValues[variableName];
|
|
|
|
|
|
|
|
if (variableValues.containsKey(variableName)) {
|
|
|
|
coercedValues[argumentName] = variableValue;
|
|
|
|
} else if (defaultValue != null || argumentDefinition.defaultsToNull) {
|
|
|
|
coercedValues[argumentName] = defaultValue;
|
2018-08-02 14:16:46 +00:00
|
|
|
} else if (argumentType is GraphQLNonNullableType) {
|
2018-08-03 21:07:08 +00:00
|
|
|
throw new GraphQLException.fromSourceSpan(
|
2018-08-04 19:18:53 +00:00
|
|
|
'Missing value for argument "$argumentName" of field "$fieldName".',
|
2018-08-03 21:07:08 +00:00
|
|
|
value.valueOrVariable.span);
|
2018-08-02 18:33:50 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
2018-08-02 13:50:31 +00:00
|
|
|
}
|
2018-08-02 18:33:50 +00:00
|
|
|
} else if (value == null) {
|
2018-08-02 13:50:31 +00:00
|
|
|
if (defaultValue != null || argumentDefinition.defaultsToNull) {
|
|
|
|
coercedValues[argumentName] = defaultValue;
|
2018-08-02 14:16:46 +00:00
|
|
|
} else if (argumentType is GraphQLNonNullableType) {
|
2018-08-03 23:30:19 +00:00
|
|
|
throw new GraphQLException.fromMessage(
|
2018-08-04 19:18:53 +00:00
|
|
|
'Missing value for argument "$argumentName" of field "$fieldName".');
|
2018-08-02 18:33:50 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-04 19:18:53 +00:00
|
|
|
try {
|
|
|
|
var validation = argumentType.validate(
|
|
|
|
fieldName, value.valueOrVariable.value.value);
|
|
|
|
|
|
|
|
if (!validation.successful) {
|
|
|
|
var errors = <GraphQLExceptionError>[
|
|
|
|
new GraphQLExceptionError(
|
|
|
|
'Type coercion error for value of argument "$argumentName" of field "$fieldName".',
|
|
|
|
locations: [
|
|
|
|
new GraphExceptionErrorLocation.fromSourceLocation(
|
|
|
|
value.valueOrVariable.span.start)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
for (var error in validation.errors) {
|
|
|
|
errors.add(
|
|
|
|
new GraphQLExceptionError(
|
|
|
|
error,
|
|
|
|
locations: [
|
|
|
|
new GraphExceptionErrorLocation.fromSourceLocation(
|
|
|
|
value.valueOrVariable.span.start)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new GraphQLException(errors);
|
|
|
|
} else {
|
|
|
|
var coercedValue = validation.value;
|
|
|
|
coercedValues[argumentName] = coercedValue;
|
|
|
|
}
|
|
|
|
} on TypeError catch (e) {
|
|
|
|
throw new GraphQLException(<GraphQLExceptionError>[
|
|
|
|
new GraphQLExceptionError(
|
|
|
|
'Type coercion error for value of argument "$argumentName" of field "$fieldName".',
|
|
|
|
locations: [
|
|
|
|
new GraphExceptionErrorLocation.fromSourceLocation(
|
|
|
|
value.valueOrVariable.span.start)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
new GraphQLExceptionError(
|
|
|
|
e.message.toString(),
|
|
|
|
locations: [
|
|
|
|
new GraphExceptionErrorLocation.fromSourceLocation(
|
|
|
|
value.valueOrVariable.span.start)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]);
|
2018-08-02 13:50:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 13:31:54 +00:00
|
|
|
|
|
|
|
return coercedValues;
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:53:47 +00:00
|
|
|
Future<T> resolveFieldValue<T>(GraphQLObjectType objectType, T objectValue,
|
2018-08-02 13:56:00 +00:00
|
|
|
String fieldName, Map<String, dynamic> argumentValues) async {
|
|
|
|
var field = objectType.fields.firstWhere((f) => f.name == fieldName);
|
2018-08-02 15:17:14 +00:00
|
|
|
|
2019-04-18 00:52:26 +00:00
|
|
|
if (objectValue is Map) {
|
|
|
|
return objectValue[fieldName] as T;
|
|
|
|
} else if (field.resolve == null) {
|
2019-04-11 17:46:04 +00:00
|
|
|
if (defaultFieldResolver != null)
|
|
|
|
return await defaultFieldResolver(
|
|
|
|
objectValue, fieldName, argumentValues);
|
|
|
|
|
2018-08-02 15:17:14 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return await field.resolve(objectValue, argumentValues) as T;
|
|
|
|
}
|
2018-08-02 13:56:00 +00:00
|
|
|
}
|
2018-08-02 13:53:47 +00:00
|
|
|
|
2018-08-02 14:16:46 +00:00
|
|
|
Future completeValue(
|
|
|
|
DocumentContext document,
|
|
|
|
String fieldName,
|
|
|
|
GraphQLType fieldType,
|
|
|
|
List<SelectionContext> fields,
|
|
|
|
result,
|
2018-08-04 19:54:18 +00:00
|
|
|
Map<String, dynamic> variableValues,
|
|
|
|
Map<String, dynamic> globalVariables) async {
|
2018-08-02 14:16:46 +00:00
|
|
|
if (fieldType is GraphQLNonNullableType) {
|
2018-08-04 19:18:53 +00:00
|
|
|
var innerType = fieldType.ofType;
|
2019-04-18 00:52:26 +00:00
|
|
|
var completedResult = await completeValue(document, fieldName, innerType,
|
2018-08-04 19:54:18 +00:00
|
|
|
fields, result, variableValues, globalVariables);
|
2018-08-02 14:16:46 +00:00
|
|
|
|
|
|
|
if (completedResult == null) {
|
2018-08-03 21:07:08 +00:00
|
|
|
throw new GraphQLException.fromMessage(
|
2018-08-02 14:16:46 +00:00
|
|
|
'Null value provided for non-nullable field "$fieldName".');
|
|
|
|
} else {
|
|
|
|
return completedResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fieldType is GraphQLListType) {
|
|
|
|
if (result is! Iterable) {
|
2018-08-03 21:07:08 +00:00
|
|
|
throw new GraphQLException.fromMessage(
|
2018-08-03 17:26:11 +00:00
|
|
|
'Value of field "$fieldName" must be a list or iterable, got $result instead.');
|
2018-08-02 14:16:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 19:18:53 +00:00
|
|
|
var innerType = fieldType.ofType;
|
2018-08-02 17:02:00 +00:00
|
|
|
var out = [];
|
|
|
|
|
|
|
|
for (var resultItem in (result as Iterable)) {
|
|
|
|
out.add(await completeValue(document, '(item in "$fieldName")',
|
2018-08-04 19:54:18 +00:00
|
|
|
innerType, fields, resultItem, variableValues, globalVariables));
|
2018-08-02 17:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2018-08-02 14:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fieldType is GraphQLScalarType) {
|
2018-08-03 17:45:40 +00:00
|
|
|
try {
|
|
|
|
var validation = fieldType.validate(fieldName, result);
|
2018-08-02 14:16:46 +00:00
|
|
|
|
2018-08-03 17:45:40 +00:00
|
|
|
if (!validation.successful) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return validation.value;
|
|
|
|
}
|
|
|
|
} on TypeError {
|
2018-08-03 21:07:08 +00:00
|
|
|
throw new GraphQLException.fromMessage(
|
2018-08-03 17:45:40 +00:00
|
|
|
'Value of field "$fieldName" must be ${fieldType.valueType}, got $result instead.');
|
2018-08-02 14:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 01:23:45 +00:00
|
|
|
if (fieldType is GraphQLObjectType || fieldType is GraphQLUnionType) {
|
|
|
|
GraphQLObjectType objectType;
|
|
|
|
|
|
|
|
if (fieldType is GraphQLObjectType && !fieldType.isInterface) {
|
|
|
|
objectType = fieldType;
|
|
|
|
} else {
|
2018-08-04 15:01:49 +00:00
|
|
|
objectType = resolveAbstractType(fieldName, fieldType, result);
|
2018-08-04 01:23:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:02:00 +00:00
|
|
|
var subSelectionSet = mergeSelectionSets(fields);
|
2018-08-04 19:54:18 +00:00
|
|
|
return await executeSelectionSet(document, subSelectionSet, objectType,
|
|
|
|
result, variableValues, globalVariables);
|
2018-08-02 14:16:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new UnsupportedError('Unsupported type: $fieldType');
|
|
|
|
}
|
|
|
|
|
2018-08-04 19:18:53 +00:00
|
|
|
GraphQLObjectType resolveAbstractType(
|
|
|
|
String fieldName, GraphQLType type, result) {
|
2018-08-04 01:23:45 +00:00
|
|
|
List<GraphQLObjectType> possibleTypes;
|
|
|
|
|
|
|
|
if (type is GraphQLObjectType) {
|
2018-08-04 15:01:49 +00:00
|
|
|
if (type.isInterface) {
|
|
|
|
possibleTypes = type.possibleTypes;
|
|
|
|
} else {
|
|
|
|
return type;
|
|
|
|
}
|
2018-08-04 01:23:45 +00:00
|
|
|
} else if (type is GraphQLUnionType) {
|
|
|
|
possibleTypes = type.possibleTypes;
|
|
|
|
} else {
|
|
|
|
throw new ArgumentError();
|
|
|
|
}
|
|
|
|
|
2018-08-04 15:01:49 +00:00
|
|
|
var errors = <GraphQLExceptionError>[];
|
|
|
|
|
2018-08-04 01:23:45 +00:00
|
|
|
for (var t in possibleTypes) {
|
|
|
|
try {
|
|
|
|
var validation =
|
2018-08-04 15:01:49 +00:00
|
|
|
t.validate(fieldName, foldToStringDynamic(result as Map));
|
2018-08-04 01:23:45 +00:00
|
|
|
|
|
|
|
if (validation.successful) {
|
|
|
|
return t;
|
|
|
|
}
|
2018-08-04 15:01:49 +00:00
|
|
|
|
|
|
|
errors
|
|
|
|
.addAll(validation.errors.map((m) => new GraphQLExceptionError(m)));
|
2019-04-11 17:05:47 +00:00
|
|
|
} on GraphQLException catch (e) {
|
|
|
|
errors.addAll(e.errors);
|
|
|
|
}
|
2018-08-04 01:23:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 15:01:49 +00:00
|
|
|
errors.insert(
|
|
|
|
0,
|
|
|
|
new GraphQLExceptionError(
|
|
|
|
'Cannot convert value $result to type $type.'));
|
|
|
|
|
|
|
|
throw new GraphQLException(errors);
|
2018-08-04 01:23:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:02:00 +00:00
|
|
|
SelectionSetContext mergeSelectionSets(List<SelectionContext> fields) {
|
|
|
|
var selections = <SelectionContext>[];
|
|
|
|
|
|
|
|
for (var field in fields) {
|
|
|
|
if (field.field?.selectionSet != null) {
|
|
|
|
selections.addAll(field.field.selectionSet.selections);
|
|
|
|
} else if (field.inlineFragment?.selectionSet != null) {
|
|
|
|
selections.addAll(field.inlineFragment.selectionSet.selections);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SelectionSetContext.merged(selections);
|
|
|
|
}
|
|
|
|
|
2018-08-02 13:31:54 +00:00
|
|
|
Map<String, List<SelectionContext>> collectFields(
|
|
|
|
DocumentContext document,
|
|
|
|
GraphQLObjectType objectType,
|
|
|
|
SelectionSetContext selectionSet,
|
|
|
|
Map<String, dynamic> variableValues,
|
2018-08-02 18:33:50 +00:00
|
|
|
{List visitedFragments}) {
|
2018-08-02 13:31:54 +00:00
|
|
|
var groupedFields = <String, List<SelectionContext>>{};
|
2018-08-02 18:33:50 +00:00
|
|
|
visitedFragments ??= [];
|
2018-08-02 13:31:54 +00:00
|
|
|
|
|
|
|
for (var selection in selectionSet.selections) {
|
|
|
|
if (getDirectiveValue('skip', 'if', selection, variableValues) == true)
|
|
|
|
continue;
|
|
|
|
if (getDirectiveValue('include', 'if', selection, variableValues) ==
|
|
|
|
false) continue;
|
|
|
|
|
|
|
|
if (selection.field != null) {
|
2019-04-11 16:49:21 +00:00
|
|
|
var responseKey = selection.field.fieldName.alias?.alias ??
|
|
|
|
selection.field.fieldName.name;
|
2018-08-02 13:31:54 +00:00
|
|
|
var groupForResponseKey =
|
2018-08-02 13:50:31 +00:00
|
|
|
groupedFields.putIfAbsent(responseKey, () => []);
|
2018-08-02 13:31:54 +00:00
|
|
|
groupForResponseKey.add(selection);
|
|
|
|
} else if (selection.fragmentSpread != null) {
|
|
|
|
var fragmentSpreadName = selection.fragmentSpread.name;
|
|
|
|
if (visitedFragments.contains(fragmentSpreadName)) continue;
|
|
|
|
visitedFragments.add(fragmentSpreadName);
|
|
|
|
var fragment = document.definitions
|
2018-08-02 18:33:50 +00:00
|
|
|
.where((d) => d is FragmentDefinitionContext)
|
|
|
|
.firstWhere(
|
|
|
|
(f) =>
|
|
|
|
(f as FragmentDefinitionContext).name == fragmentSpreadName,
|
|
|
|
orElse: () => null) as FragmentDefinitionContext;
|
2018-08-02 13:31:54 +00:00
|
|
|
|
|
|
|
if (fragment == null) continue;
|
|
|
|
var fragmentType = fragment.typeCondition;
|
|
|
|
if (!doesFragmentTypeApply(objectType, fragmentType)) continue;
|
|
|
|
var fragmentSelectionSet = fragment.selectionSet;
|
|
|
|
var fragmentGroupFieldSet = collectFields(
|
|
|
|
document, objectType, fragmentSelectionSet, variableValues);
|
|
|
|
|
|
|
|
for (var responseKey in fragmentGroupFieldSet.keys) {
|
|
|
|
var fragmentGroup = fragmentGroupFieldSet[responseKey];
|
|
|
|
var groupForResponseKey =
|
2018-08-02 13:50:31 +00:00
|
|
|
groupedFields.putIfAbsent(responseKey, () => []);
|
2018-08-02 13:31:54 +00:00
|
|
|
groupForResponseKey.addAll(fragmentGroup);
|
|
|
|
}
|
|
|
|
} else if (selection.inlineFragment != null) {
|
|
|
|
var fragmentType = selection.inlineFragment.typeCondition;
|
|
|
|
if (fragmentType != null &&
|
|
|
|
!doesFragmentTypeApply(objectType, fragmentType)) continue;
|
|
|
|
var fragmentSelectionSet = selection.inlineFragment.selectionSet;
|
|
|
|
var fragmentGroupFieldSet = collectFields(
|
|
|
|
document, objectType, fragmentSelectionSet, variableValues);
|
|
|
|
|
|
|
|
for (var responseKey in fragmentGroupFieldSet.keys) {
|
|
|
|
var fragmentGroup = fragmentGroupFieldSet[responseKey];
|
|
|
|
var groupForResponseKey =
|
2018-08-02 13:50:31 +00:00
|
|
|
groupedFields.putIfAbsent(responseKey, () => []);
|
2018-08-02 13:31:54 +00:00
|
|
|
groupForResponseKey.addAll(fragmentGroup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return groupedFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDirectiveValue(String name, String argumentName,
|
|
|
|
SelectionContext selection, Map<String, dynamic> variableValues) {
|
|
|
|
if (selection.field == null) return null;
|
|
|
|
var directive = selection.field.directives.firstWhere((d) {
|
|
|
|
var vv = d.valueOrVariable;
|
|
|
|
if (vv.value != null) return vv.value.value == name;
|
|
|
|
return vv.variable.name == name;
|
|
|
|
}, orElse: () => null);
|
|
|
|
|
|
|
|
if (directive == null) return null;
|
|
|
|
if (directive.argument?.name != argumentName) return null;
|
|
|
|
|
|
|
|
var vv = directive.argument.valueOrVariable;
|
|
|
|
|
|
|
|
if (vv.value != null) return vv.value.value;
|
|
|
|
|
|
|
|
var vname = vv.variable.name;
|
|
|
|
if (!variableValues.containsKey(vname))
|
2018-08-04 00:57:38 +00:00
|
|
|
throw new GraphQLException.fromSourceSpan(
|
|
|
|
'Unknown variable: "$vname"', vv.span);
|
2018-08-02 13:31:54 +00:00
|
|
|
|
|
|
|
return variableValues[vname];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool doesFragmentTypeApply(
|
|
|
|
GraphQLObjectType objectType, TypeConditionContext fragmentType) {
|
|
|
|
var type = convertType(new TypeContext(fragmentType.typeName, null));
|
2018-08-04 01:23:45 +00:00
|
|
|
if (type is GraphQLObjectType && !type.isInterface) {
|
2018-08-02 13:31:54 +00:00
|
|
|
for (var field in type.fields)
|
|
|
|
if (!objectType.fields.any((f) => f.name == field.name)) return false;
|
|
|
|
return true;
|
2018-08-04 01:23:45 +00:00
|
|
|
} else if (type is GraphQLObjectType && type.isInterface) {
|
|
|
|
return objectType.isImplementationOf(type);
|
|
|
|
} else if (type is GraphQLUnionType) {
|
|
|
|
return type.possibleTypes.any((t) => objectType.isImplementationOf(t));
|
2018-08-02 13:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|