parseAndExecute method

Future<GraphQLResult> parseAndExecute(
  1. String query, {
  2. String? operationName,
  3. dynamic sourceUrl,
  4. Map<String, Object?>? variableValues,
  5. Map<String, Object?>? extensions,
  6. Object? rootValue,
  7. List<ScopedOverride>? scopeOverrides,
  8. List<OperationType> validOperationTypes = OperationType.values,
})

Parses the GraphQL document in query and executes operationName or the only operation in the document if not given.

Throws InvalidOperationType if the operation for the given query and operationName is not in validOperationTypes. May throw other exceptions if a GraphQLExtension in extensions throws.

Implementation

Future<GraphQLResult> parseAndExecute(
  String query, {
  String? operationName,

  /// The text document's source
  /// may be either a [String], a [Uri], or `null`.
  dynamic sourceUrl,
  Map<String, Object?>? variableValues,
  Map<String, Object?>? extensions,
  Object? rootValue,
  List<ScopedOverride>? scopeOverrides,
  List<OperationType> validOperationTypes = OperationType.values,
}) async {
  final _globalVariables = ScopedMap(
    parent: baseGlobalVariables,
    overrides: [
      if (scopeOverrides != null) ...scopeOverrides,
      _graphQLExecutorRef.override((_) => this)
    ],
  );

  final preExecuteCtx = RequestCtx(
    extensions: extensions,
    scope: _globalVariables,
    operationName: operationName,
    query: query,
    rootValue: rootValue ?? _globalVariables,
    rawVariableValues: variableValues,
    schema: schema,
  );

  try {
    return withExtensions(
      (next, p1) => p1.executeRequest(
        next,
        preExecuteCtx,
      ),
      () async {
        try {
          final document = getDocumentNode(
            query,
            sourceUrl: sourceUrl,
            extensions: extensions,
            globals: _globalVariables,
            ctx: preExecuteCtx,
          );

          final operation = getOperation(document, operationName);
          if (!validOperationTypes.contains(operation.type)) {
            throw InvalidOperationType(
              preExecuteCtx,
              document,
              operation,
              validOperationTypes,
            );
          }
          final result = await executeRequest(
            schema,
            document,
            rootValue: rootValue ?? _globalVariables,
            variableValues: variableValues,
            globalVariables: _globalVariables,
            extensions: extensions,
            baseCtx: preExecuteCtx,
            operation: operation,
          );

          return result;
        } on GraphQLException catch (e) {
          return GraphQLResult(
            null,
            errors: e.errors,
            didExecute: false,
          );
        }
      },
    );
  } finally {
    // ignore: unawaited_futures
    _globalVariables.dispose();
  }
}