예제 #1
0
        public async Task <QueryStream> QueryAsync(
            Query query,
            CancellationToken cancellationToken)
        {
            try
            {
                _logger.Query(query);
                var schemaOptions = _options;
                var document      = query.Document;
                var schema        = await schemaOptions.GetSchema(query);

                var executionOptions = new ExecutionOptions
                {
                    Schema         = schema,
                    Document       = document,
                    OperationName  = query.OperationName,
                    VariableValues = query.Variables,
                    InitialValue   = null,
                    LoggerFactory  = _loggerFactory,
                    Extensions     = _extensions,
                    Validate       = (s, d, v) => ExecutionOptions.DefaultValidate(
                        schemaOptions.ValidationRules,
                        s,
                        d,
                        v)
                };

                // is subscription
                if (document.OperationDefinitions
                    ?.Any(op => op.Operation == OperationType.Subscription) ?? false)
                {
                    return(await SubscribeAsync(
                               executionOptions,
                               cancellationToken));
                }


                // is query or mutation
                return(await ExecuteAsync(
                           executionOptions,
                           cancellationToken));
            }
            catch (Exception e)
            {
                _logger.LogError($"Failed to execute query '{query.Document.ToGraphQL()}'. Error. '{e}'");
                var channel = Channel.CreateBounded <ExecutionResult>(1);
                channel.Writer.TryComplete(e);
                return(new QueryStream(channel));
            }
        }