Esempio n. 1
0
        private IReadOnlyList <HCError> ValidateQueryDocuments(ISchema schema)
        {
            var errors = new List <HCError>();

            try
            {
                var serviceCollection = new ServiceCollection();
                serviceCollection.AddValidation();
                IDocumentValidator validator = serviceCollection.BuildServiceProvider()
                                               .GetService <IDocumentValidatorFactory>().CreateValidator();

                foreach (DocumentInfo documentInfo in _queries.Values)
                {
                    DocumentValidatorResult validationResult =
                        validator.Validate(schema, documentInfo.Document);

                    if (validationResult.HasErrors)
                    {
                        foreach (HCError error in validationResult.Errors)
                        {
                            errors.Add(HCErrorBuilder.FromError(error)
                                       .SetExtension("fileName", documentInfo.FileName)
                                       .SetExtension("document", documentInfo.Document)
                                       .Build());
                        }
                    }
                }
            }
            catch (GeneratorException ex)
            {
                errors.AddRange(ex.Errors);
            }

            return(errors);
        }
Esempio n. 2
0
        private async Task <IQueryDescriptor> LoadAsync(
            string name, string fileName, DocumentNode document)
        {
            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>()
                .FirstOrDefault(t => t.Name is null);

            if (operation != null)
            {
                throw new GeneratorException(HCErrorBuilder.New()
                                             .SetMessage("All operations have to have a name in order " +
                                                         "to work with Strawberry Shake. Check the specified " +
                                                         "operation and give it a name, then retry generating " +
                                                         "the client.")
                                             .SetCode("OPERATION_NO_NAME")
                                             .AddLocation(operation)
                                             .SetExtension("fileName", fileName)
                                             .Build());
            }

            DocumentNode rewritten = AddTypeNameQueryRewriter.Rewrite(document);

            byte[] rewrittenBuffer;

            var serializer = new QuerySyntaxSerializer(false);

            using (var stream = new MemoryStream())
            {
                using (var sw = new StreamWriter(stream))
                {
                    using (var writer = new DocumentWriter(sw))
                    {
                        serializer.Visit(rewritten, writer);
                    }
                }

                await stream.FlushAsync().ConfigureAwait(false);

                rewrittenBuffer = stream.ToArray();
            }

            string hash = _hashProvider.ComputeHash(rewrittenBuffer);

            var descriptor = new QueryDescriptor(
                name,
                _namespace,
                _hashProvider.Name,
                hash,
                rewrittenBuffer,
                document);

            _descriptors.Add(descriptor);
            return(descriptor);
        }
Esempio n. 3
0
 public GraphQLException(string message)
     : this(ErrorBuilder.New().SetMessage(message).Build())
 {
 }