Exemple #1
0
        public async Task <IActionResult> Post([FromBody] GraphQLRequest request)
        {
            var inputs = request.Variables.ToInputs();
            var json   = await _schema.ExecuteAsync(_documentWriter, options =>
            {
                options.Query         = request.Query;
                options.OperationName = request.OperationName;
                options.Inputs        = inputs;
            });

            return(Ok(json));
        }
        public async Task Subscribe_RaiseEvent_ReceiveSubscriptionResult()
        {
            // arrange
            var registry = new InMemoryEventRegistry();

            var services = new Mock <IServiceProvider>();

            services.Setup(t => t.GetService(It.IsAny <Type>()))
            .Returns(new Func <Type, object>(t =>
            {
                if (t == typeof(IEventRegistry) ||
                    t == typeof(IEventSender))
                {
                    return(registry);
                }
                return(null);
            }));

            ISchema schema = Schema.Create(c =>
            {
                c.RegisterServiceProvider(services.Object);
                c.RegisterMutationType <MutationType>();
                c.RegisterSubscriptionType <SubscriptionType>();
            });

            var responseStream =
                await schema.ExecuteAsync("subscription { foo }")
                as IResponseStream;

            // act
            await schema.ExecuteAsync("mutation { foo }");

            // assert
            IQueryExecutionResult result = await responseStream.ReadAsync();

            Assert.False(responseStream.IsCompleted);
            Assert.Equal("bar", result.Data["foo"]);
        }
        public async Task QueryEvents()
        {
            // arrange
            var listener = new TestDiagnosticListener();

            using (DiagnosticListener.AllListeners.Subscribe(
                       new DiagnosticObserver(listener)))
            {
                ISchema schema = CreateSchema();

                // act
                await schema.ExecuteAsync("{ foo }");

                // assert
                Assert.True(listener.QueryStart);
                Assert.True(listener.QueryStop);
            }
        }
        public async Task ResolverEvents()
        {
            // arrange
            var listener = new TestDiagnosticListener();

            using (DiagnosticListener.AllListeners.Subscribe(
                       new DiagnosticObserver(listener)))
            {
                ISchema schema = CreateSchema();

                // act
                await schema.ExecuteAsync("{ foo }");

                // assert
                Assert.True(listener.ResolveFieldStart);
                Assert.True(listener.ResolveFieldStop);
                Assert.Equal("foo", listener.FieldSelection.Name.Value);
                Assert.InRange(listener.Duration,
                               TimeSpan.FromMilliseconds(50),
                               TimeSpan.FromMilliseconds(2000));
            }
        }
Exemple #5
0
 /// <summary>
 /// Configures an <see cref="ExecutionOptions"/> using the given <paramref name="configure"/> action
 /// then executes those options using the <paramref name="schema"/> and a <see cref="DocumentWriter"/>
 /// with indentation turned on.
 /// </summary>
 /// <param name="schema">A schema to use.</param>
 /// <param name="configure">An action that configures something to execute.</param>
 /// <returns>The JSON result as a string.</returns>
 /// <remarks>
 /// Useful for quickly executing something and "getting started".
 /// Part of the public API and should not be removed even if it has no references.
 /// </remarks>
 public static Task <string> ExecuteAsync(this ISchema schema, Action <ExecutionOptions> configure)
 => schema.ExecuteAsync(new DocumentWriter(indent: true), configure);
 public string Handle(string query)
 {
     return(_schema.ExecuteAsync(query));
 }
 /// <summary>
 /// Configures an <see cref="ExecutionOptions"/> using the given <paramref name="configure"/> action
 /// then executes those options using the <paramref name="schema"/> and a <see cref="GraphQLSerializer"/>
 /// with indentation turned on.
 /// </summary>
 /// <param name="schema">A schema to use.</param>
 /// <param name="configure">An action that configures something to execute.</param>
 /// <returns>The JSON result as a string.</returns>
 /// <remarks>
 /// Useful for quickly executing something and "getting started".
 /// Part of the public API and should not be removed even if it has no references.
 /// </remarks>
 public static Task <string> ExecuteAsync(this ISchema schema, Action <ExecutionOptions> configure)
 => schema.ExecuteAsync(new GraphQLSerializer(indent: true), configure);