예제 #1
0
 public GraphQLMiddleware(
     OwinMiddleware next,
     ApplicationContext applicationContext,
     GraphQLServerOptions options) : base(next)
 {
     _applicationContext = applicationContext ?? throw new ArgumentNullException(nameof(applicationContext));
     _options            = options ?? throw new ArgumentNullException(nameof(options));
 }
        public static IAppBuilder UseUmbracoGraphQL(this IAppBuilder app,
                                                    ApplicationContext applicationContext, GraphQLServerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            //var jsonSerializerSettings = new JsonSerializerSettings
            //{
            //    DateFormatHandling = DateFormatHandling.IsoDateFormat,
            //    DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFF'Z'",
            //    ContractResolver = new DefaultContractResolver()
            //};
            //var documentExecuter = new DocumentExecuter();
            //var documentWriter = new DocumentWriter(Formatting.None, jsonSerializerSettings);
            //var requestExecutor = new RequestExecutor(documentExecuter, documentWriter);

            //TODO: Make GraphiQL endpoint configurable
            var graphiQLPath = $"/{GlobalSettings.UmbracoMvcArea}/graphiql";
            var graphQLPath  = $"/{GlobalSettings.UmbracoMvcArea}/graphql";


            return(app.Map(graphiQLPath, subApp =>
            {
                string html;

                using (var stream = typeof(AppBuilderExtensions).Assembly.GetManifestResourceStream("Our.Umbraco.GraphQL.Resources.graphiql.html"))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        html = reader.ReadToEnd().Replace("${endpointURL}", graphQLPath);
                    }
                }
                subApp.Run(async ctx =>
                {
                    ctx.Response.ContentType = "text/html";
                    await ctx.Response.WriteAsync(html);
                });
            })
                   .Map(graphQLPath, subApp =>
            {
                var corsOptions = new CorsOptions
                {
                    PolicyProvider = options.CorsPolicyProvder
                };

                subApp.UseCors(corsOptions)
                .Use <GraphQLRequestParserMiddleware>()
                .Use <GraphQLMiddleware>(applicationContext, options);
            }));
        }