Example #1
0
 public GraphQLMiddleware(RequestDelegate next, GraphQLSettings settings, IDocumentExecuter executer, IDocumentWriter writer)
 {
     this.next     = next;
     this.settings = settings;
     this.executer = executer;
     this.writer   = writer;
 }
Example #2
0
 public GraphQLMiddleware(RequestDelegate next, GraphQLSettings settings, IDocumentWriter writer, IDocumentExecuter executor)
 {
     _next     = next;
     _settings = settings;
     _writer   = writer;
     _executor = executor;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="GraphQLController"/> class.
 /// </summary>
 /// <param name="schema">The schema.</param>
 /// <param name="graphQLSettings">The graph ql settings.</param>
 /// <param name="documentExecuter">The document executer.</param>
 /// <param name="serviceProvider">The service provider.</param>
 public GraphQLController(ISchema schema,
                          GraphQLSettings graphQLSettings,
                          IDocumentExecuter documentExecuter,
                          IServiceProvider serviceProvider)
 {
     _schema           = schema;
     _graphQLSettings  = graphQLSettings;
     _documentExecuter = documentExecuter;
     _serviceProvider  = serviceProvider;
 }
Example #4
0
        // ReSharper disable once UnusedParameter.Local
        public GraphityMiddleware(RequestDelegate next,
                                  IDocumentExecuter documentExecuter,
                                  ISchema schema,
                                  GraphQLSettings settings,
                                  IEnumerable <IValidationRule> validationRules)
        {
            _documentExecuter = documentExecuter;
            _schema           = schema;
            _settings         = settings;

            _validationRules = validationRules;
        }
Example #5
0
 public GraphQLMiddleware(
     RequestDelegate next,
     GraphQLSettings settings,
     IDocumentExecuter executer,
     IDocumentWriter writer,
     IEnumerable <IValidationRule> rules)
 {
     _next     = next;
     _settings = settings;
     _executer = executer;
     _writer   = writer;
     _rules    = rules;
 }
Example #6
0
 public GraphController
 (
     GraphQLSettings settings,
     IHostingEnvironment env,
     IDocumentExecuter executer,
     IDocumentWriter writer,
     ISchema schema
 )
 {
     _settings = settings;
     _env      = env;
     _executer = executer;
     _writer   = writer;
     _schema   = schema;
 }
Example #7
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            bool.TryParse(Configuration["GraphQL:ExposeExceptions"], out var exposeExceptions);
            bool.TryParse(Configuration["GraphQL:EnableMetrics"], out var enableMetrics);
            int.TryParse(Configuration["GraphQL:MaxQueryDepth"], out var maxQueryDepth);

            var settings = new GraphQLSettings
            {
                Path             = "/graphql",
                BuildUserContext = CreateUserContext,
                EnableMetrics    = exposeExceptions,
                ExposeExceptions = enableMetrics,
                MaxQueryDepth    = maxQueryDepth == 0 ? 15 : maxQueryDepth
            };
            var validationRules = app.ApplicationServices.GetServices <IValidationRule>();

            settings.ValidationRules.AddRange(validationRules);



            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();

            //app.UseAuthentication();
            //app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseMiddleware <GraphQLMiddleware>(settings);

            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions()
            {
                Path = new PathString("/ui/playground")
            });
        }
Example #8
0
    // ReSharper disable once UnusedParameter.Local
    public GraphityMiddleware(RequestDelegate next,
                              IDocumentExecuter documentExecuter,
                              ISchema schema,
                              GraphQLSettings settings,
                              IEnumerable <IValidationRule> validationRules,
                              IHttpContextAccessor httpContextAccessor)
    {
        if (next == null)
        {
            throw new ArgumentNullException(nameof(next));
        }

        _documentExecuter = documentExecuter;
        _schema           = schema;
        _settings         = settings;

        _validationRules = validationRules;

        _httpContextAccessor = httpContextAccessor;
    }
        public static void UseGraphQLWithAuth <TSchema>(this IApplicationBuilder app) where TSchema : Schema
        {
            var settings = new GraphQLSettings
            {
                BuildUserContext = async ctx =>
                {
                    var userContext = new GraphQLUserContext
                    {
                        User = ctx.User
                    };

                    return(await Task.FromResult(userContext).ConfigureAwait(false));
                }
            };

            var rules = app.ApplicationServices.GetServices <IValidationRule>();

            settings.ValidationRules.AddRange(rules);

            app.UseGraphQL <TSchema>();
        }
        public static void UseGraphQLWithAuth(this IApplicationBuilder app)
        {
            var settings = new GraphQLSettings
            {
                BuildUserContext = async ctx =>
                {
                    var userContext = new GraphQLUserContext
                    {
                        User = ctx.User
                    };

                    return(await Task.FromResult(userContext));
                }
            };

            var rules = app.ApplicationServices.GetServices <IValidationRule>();

            settings.ValidationRules.AddRange(rules);

            app.UseMiddleware <PlaygroundMiddleware>(settings);
        }
        public async Task <IActionResult> Post([FromBody] GraphQLRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            //var files = Request.Form.Files;
            var settings = new GraphQLSettings
            {
                Path             = "/api/graphql",
                BuildUserContext = ctx => new GraphQLUserContext
                {
                    User = ctx.User,
                    //Files = request.File
                },
                EnableMetrics = false
            };
            var result = await _documentExecuter.ExecuteAsync(_ =>
            {
                _.Schema        = _schema;
                _.Query         = request?.Query;
                _.OperationName = request?.OperationName;
                _.Inputs        = request?.Variables.ToInputs();
                _.UserContext   = settings.BuildUserContext?.Invoke(this.HttpContext);
                //_.ValidationRules = DocumentValidator.CoreRules.Concat(new[] { new InputValidationRule() });
                _.EnableMetrics = settings.EnableMetrics;
                if (settings.EnableMetrics)
                {
                    _.FieldMiddleware.Use <InstrumentFieldsMiddleware>();
                }
            });

            if (result.Errors?.Count > 0)
            {
                return(BadRequest(result));
            }

            return(Ok(result));
        }
Example #12
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
            app.UseAuthentication();
            app.UseAuthorization();

            //app.UseGraphiQl();

            var settings = new GraphQLSettings
            {
                BuildUserContext = ctx =>
                {
                    var userContext = new GraphQLUserContext
                    {
                        User = ctx.User
                    };

                    return(Task.FromResult(userContext));
                }
            };

            var rules = app.ApplicationServices.GetServices <IValidationRule>();

            settings.ValidationRules.AddRange(rules);

            app.UseMiddleware <GraphQLMiddleware>(settings);

            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions
            {
                Path = "/ui/playground"
            });
        }