Esempio n. 1
0
 public DefaultHttpRequestParser(
     IDocumentCache documentCache,
     IDocumentHashProvider documentHashProvider,
     int maxRequestSize,
     ParserOptions parserOptions)
 {
     _documentCache = documentCache ??
                      throw new ArgumentNullException(nameof(documentCache));
     _documentHashProvider = documentHashProvider ??
                             throw new ArgumentNullException(nameof(documentHashProvider));
     _maxRequestSize = maxRequestSize < _minRequestSize
         ? _minRequestSize
         : maxRequestSize;
     _parserOptions = parserOptions ??
                      throw new ArgumentNullException(nameof(parserOptions));
 }
Esempio n. 2
0
        public GraphQLFunctions(IQueryExecutor executor, IDocumentCache documentCache,
                                IDocumentHashProvider documentHashProvider, IAzureFunctionsMiddlewareOptions azureFunctionsOptions)
        {
            Executor              = executor;
            DocumentCache         = documentCache;
            DocumentHashProvider  = documentHashProvider;
            AzureFunctionsOptions = azureFunctionsOptions;

            _jsonQueryResultSerializer = new JsonQueryResultSerializer();

            _requestHelper = new RequestHelper(
                DocumentCache,
                DocumentHashProvider,
                AzureFunctionsOptions.MaxRequestSize,
                AzureFunctionsOptions.ParserOptions);
        }
Esempio n. 3
0
        private async Task <IReadOnlyList <IQueryDescriptor> > ParseQueriesAsync(
            IDocumentHashProvider hashProvider)
        {
            var queryCollection = new QueryCollection(hashProvider, _namespace !);

            foreach (DocumentInfo documentInfo in _queries.Values)
            {
                await queryCollection.LoadFromDocumentAsync(
                    documentInfo.Name,
                    documentInfo.FileName,
                    documentInfo.Document)
                .ConfigureAwait(false);
            }

            return(queryCollection.ToList());
        }
        public WritePersistedQueryMiddleware(
            QueryDelegate next,
            IWriteStoredQueries writeStoredQueries,
            IDocumentHashProvider documentHashProvider)
        {
            if (documentHashProvider is null)
            {
                throw new ArgumentNullException(nameof(documentHashProvider));
            }

            _next = next
                    ?? throw new ArgumentNullException(nameof(next));
            _writeStoredQueries = writeStoredQueries
                                  ?? throw new ArgumentNullException(nameof(writeStoredQueries));
            _hashName = documentHashProvider.Name;
        }
Esempio n. 5
0
        public static IApplicationBuilder UseGraphQLHttpPost(
            this IApplicationBuilder applicationBuilder,
            IServiceProvider serviceProvider,
            IHttpPostMiddlewareOptions options)
        {
            if (applicationBuilder == null)
            {
                throw new ArgumentNullException(nameof(applicationBuilder));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            IQueryExecutor executor = serviceProvider
                                      .GetRequiredService <IQueryExecutor>();

            IBatchQueryExecutor batchQueryExecutor = serviceProvider
                                                     .GetRequiredService <IBatchQueryExecutor>();

            IQueryResultSerializer resultSerializer = serviceProvider
                                                      .GetRequiredService <IQueryResultSerializer>();

            IResponseStreamSerializer streamSerializer = serviceProvider
                                                         .GetRequiredService <IResponseStreamSerializer>();

            IDocumentCache documentCache = serviceProvider
                                           .GetRequiredService <IDocumentCache>();

            IDocumentHashProvider documentHashProvider = serviceProvider
                                                         .GetRequiredService <IDocumentHashProvider>();

            OwinContextAccessor contextAccessor =
                serviceProvider.GetService <OwinContextAccessor>();

            return(applicationBuilder.Use <HttpPostMiddleware>(
                       options, contextAccessor,
                       executor, batchQueryExecutor,
                       resultSerializer, streamSerializer,
                       documentCache, documentHashProvider));
        }
 public ParseQueryMiddleware(
     QueryDelegate next,
     IQueryParser parser,
     Cache <ICachedQuery> queryCache,
     QueryExecutionDiagnostics diagnosticEvents,
     IDocumentHashProvider documentHashProvider)
 {
     _next = next
             ?? throw new ArgumentNullException(nameof(next));
     _parser = parser
               ?? throw new ArgumentNullException(nameof(parser));
     _queryCache = queryCache
                   ?? throw new ArgumentNullException(nameof(queryCache));
     _diagnosticEvents = diagnosticEvents
                         ?? throw new ArgumentNullException(nameof(diagnosticEvents));
     _documentHashProvider = documentHashProvider
                             ?? new MD5DocumentHashProvider();
 }
Esempio n. 7
0
        private static DocumentModel CreateDocumentModel(
            IDocumentAnalyzerContext context,
            string name,
            DocumentNode original,
            IDocumentHashProvider hashProvider)
        {
            DocumentNode optimized = TypeNameQueryRewriter.Rewrite(original);

            string serialized = QuerySyntaxSerializer.Serialize(optimized, false);

            byte[] buffer = Encoding.UTF8.GetBytes(serialized);
            string hash   = hashProvider.ComputeHash(buffer);

            return(new DocumentModel(
                       name,
                       context.Operations.Where(t => t.Document == original).ToArray(),
                       original,
                       optimized,
                       buffer,
                       hashProvider.Name,
                       hash));
        }
Esempio n. 8
0
        public static IApplicationBuilder UseGraphQL(
            this IApplicationBuilder applicationBuilder,
            IServiceProvider serviceProvider,
            QueryMiddlewareOptions options)
        {
            if (applicationBuilder == null)
            {
                throw new ArgumentNullException(nameof(applicationBuilder));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            IQueryExecutor executor = serviceProvider
                                      .GetService <IQueryExecutor>();

            IQueryResultSerializer serializer = serviceProvider
                                                .GetService <IQueryResultSerializer>()
                                                ?? new JsonQueryResultSerializer();

            IDocumentCache cache = serviceProvider
                                   .GetRequiredService <IDocumentCache>();

            IDocumentHashProvider hashProvider = serviceProvider
                                                 .GetRequiredService <IDocumentHashProvider>();

            return(applicationBuilder
                   .Use <PostQueryMiddleware>(executor, serializer, cache, hashProvider, options)
                   .Use <GetQueryMiddleware>(executor, serializer, options)
                   .Use <SchemaMiddleware>(executor, options));
        }
Esempio n. 9
0
 public ClientGenerator SetHashProvider(
     IDocumentHashProvider hashProvider)
 {
     _hashProvider = hashProvider;
     return(this);
 }
Esempio n. 10
0
        public async Task BuildAsync()
        {
            if (_output is null)
            {
                throw new InvalidOperationException(
                          "You have to specify a field output handler before you " +
                          "can generate any client APIs.");
            }

            if (_schemas.Count == 0)
            {
                throw new InvalidOperationException(
                          "You have to specify at least one schema file before you " +
                          "can generate any client APIs.");
            }

            if (_queries.Count == 0)
            {
                throw new InvalidOperationException(
                          "You have to specify at least one query file before you " +
                          "can generate any client APIs.");
            }

            IDocumentHashProvider hashProvider = _hashProvider
                                                 ?? new MD5DocumentHashProvider();

            _namespace = _namespace ?? "StrawberryShake.Client";

            // create schema
            DocumentNode mergedSchema = MergeSchema();

            mergedSchema = MergeSchemaExtensions(mergedSchema);
            ISchema schema = CreateSchema(mergedSchema);

            InitializeScalarTypes(schema);

            // parse queries
            IReadOnlyList <HCError> errors = ValidateQueryDocuments(schema);

            if (errors.Count > 0)
            {
                throw new GeneratorException(errors);
            }

            IReadOnlyList <IQueryDescriptor> queries =
                await ParseQueriesAsync(schema, hashProvider);

            // generate abstarct client models
            var usedNames   = new HashSet <string>();
            var descriptors = new List <ICodeDescriptor>();
            var fieldTypes  = new Dictionary <FieldNode, string>();

            GenerateModels(schema, queries, usedNames, descriptors, fieldTypes);

            var typeLookup = new TypeLookup(
                _options.LanguageVersion,
                _leafTypes.Values,
                fieldTypes);

            // generate code from models
            foreach (ICodeGenerator generator in CreateGenerators(_options))
            {
                foreach (ICodeDescriptor descriptor in descriptors)
                {
                    if (generator.CanHandle(descriptor))
                    {
                        _output.Register(descriptor, generator);
                    }
                }
            }

            await _output.WriteAllAsync(typeLookup);
        }
Esempio n. 11
0
 public DocumentAnalyzer SetHashProvider(IDocumentHashProvider hashProvider)
 {
     _hashProvider = hashProvider;
     return(this);
 }
Esempio n. 12
0
 public QueryCollection(IDocumentHashProvider hashProvider, string ns)
 {
     _hashProvider = hashProvider
                     ?? throw new ArgumentNullException(nameof(hashProvider));
     _namespace = ns ?? throw new ArgumentNullException(nameof(ns));
 }
Esempio n. 13
0
 public QueryCollection(string ns)
 {
     _hashProvider = new MD5DocumentHashProvider();
     _namespace    = ns ?? throw new ArgumentNullException(nameof(ns));
 }
 public GraphHttpRequestHandler(IServiceProvider serviceProvider, IQueryExecutor queryExecutor, IDocumentCache documentCache, IDocumentHashProvider documentHashProvider, IGraphHttpRequestHandlerOptions graphHttpRequestHanlderOptions)
     : base(documentCache, documentHashProvider, graphHttpRequestHanlderOptions.MaxRequestSize, graphHttpRequestHanlderOptions.ParserOptions)
 {
     Executor        = queryExecutor;
     ServiceProvider = serviceProvider;
 }