Example #1
0
        public async Task <PublishSchemaPayload> PublishSchemaAsync(
            PublishSchemaInput input,
            [Service] IMessageSender <PublishDocumentMessage> messageSender,
            [Service] IFileStorage fileStorage,
            [Service] ISessionCreator sessionCreator,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            if (input.ExternalId == null && string.IsNullOrEmpty(input.SourceText))
            {
                throw new GraphQLException(
                          Resources.SchemaMutations_HashAndSourceTextAreNull);
            }

            string sessionId = await sessionCreator.CreateSessionAsync(cancellationToken)
                               .ConfigureAwait(false);

            if (input.SourceText is { })
Example #2
0
        public async Task <PublishClientPayload> PublishClientAsync(
            PublishClientInput input,
            [Service] IMessageSender <PublishDocumentMessage> messageSender,
            [Service] IFileStorage fileStorage,
            [Service] ISessionCreator sessionCreator,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] ClientByNameDataLoader clientDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            if (input.Files.Count == 0)
            {
                throw new GraphQLException("You have to provide at least one query file.");
            }

            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Client client = await clientDataLoader.LoadAsync(
                input.ClientName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            string sessionId = await sessionCreator.CreateSessionAsync(cancellationToken)
                               .ConfigureAwait(false);

            IFileContainer container = await fileStorage.CreateContainerAsync(
                sessionId, cancellationToken)
                                       .ConfigureAwait(false);

            foreach (QueryFile file in input.Files)
            {
                await container.CreateTextFileAsync(
                    file.Name, file.SourceText, cancellationToken)
                .ConfigureAwait(false);
            }

            await messageSender.SendAsync(
                new PublishDocumentMessage(
                    sessionId,
                    environment.Id,
                    schema.Id,
                    client.Id,
                    input.ExternalId,
                    input.Format == QueryFileFormat.GraphQL
                        ? DocumentType.Query
                        : DocumentType.Schema,
                    input.Tags is null
                        ? Array.Empty <Tag>()
                        : input.Tags.Select(t => new Tag(t.Key, t.Value)).ToArray()),
                cancellationToken)
            .ConfigureAwait(false);

            return(new PublishClientPayload(sessionId, input.ClientMutationId));
        }
Example #3
0
        public async Task <PublishSchemaPayload> PublishSchemaAsync(
            PublishSchemaInput input,
            [Service] IMessageSender <PublishDocumentMessage> messageSender,
            [Service] IFileStorage fileStorage,
            [Service] ISessionCreator sessionCreator,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            if (string.IsNullOrEmpty(input.SourceText))
            {
                throw new GraphQLException(
                          Resources.SchemaMutations_HashAndSourceTextAreNull);
            }

            string sessionId = await sessionCreator.CreateSessionAsync(cancellationToken)
                               .ConfigureAwait(false);

            IFileContainer container = await fileStorage.CreateContainerAsync(
                sessionId, cancellationToken)
                                       .ConfigureAwait(false);

            using (Stream stream = await container.CreateFileAsync(
                       "schema.graphql", cancellationToken)
                                   .ConfigureAwait(false))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(input.SourceText);
                await stream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
            }

            await messageSender.SendAsync(
                new PublishDocumentMessage(
                    sessionId,
                    environment.Id,
                    schema.Id,
                    input.ExternalId,
                    input.Tags is null
                        ? Array.Empty <Tag>()
                        : input.Tags.Select(t => new Tag(t.Key, t.Value)).ToArray()),
                cancellationToken)
            .ConfigureAwait(false);

            return(new PublishSchemaPayload(sessionId, input.ClientMutationId));
        }
Example #4
0
        public async Task <MarkClientPublishedPayload> MarkClientPublishedAsync(
            MarkClientPublishedInput input,
            [Service] IClientRepository clientRepository,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            ClientVersion?clientVersion = await clientRepository.GetClientVersionByExternalIdAsync(
                input.ExternalId, cancellationToken)
                                          .ConfigureAwait(false);

            if (clientVersion is null)
            {
                throw new GraphQLException(
                          "There is no client version associated with the " +
                          $"specified external ID `{input.ExternalId}`.");
            }

            await clientRepository.SetPublishedClientAsync(
                new PublishedClient(
                    environment.Id,
                    schema.Id,
                    clientVersion.ClientId,
                    clientVersion.Id,
                    new HashSet <Guid>(clientVersion.QueryIds).ToList()))
            .ConfigureAwait(false);

            return(new MarkClientPublishedPayload(
                       environment,
                       schema,
                       clientVersion,
                       input.ClientMutationId));
        }
Example #5
0
        public async Task <QueryDocument?> GetQueryDocumentByHash(
            string environmentName,
            string schemaName,
            string hash,
            [Service] IClientRepository repository,
            [DataLoader] EnvironmentByNameDataLoader environmentByName,
            [DataLoader] SchemaByNameDataLoader schemaByName,
            CancellationToken cancellationToken)
        {
            Environment environment = await environmentByName.LoadAsync(
                environmentName, cancellationToken)
                                      .ConfigureAwait(false);

            Schema schema = await schemaByName.LoadAsync(
                schemaName, cancellationToken)
                            .ConfigureAwait(false);

            return(await repository.GetQueryDocumentAsync(
                       environment.Id, schema.Id, hash, cancellationToken)
                   .ConfigureAwait(false));
        }
Example #6
0
        public async Task <PublishClientPayload> PublishClientAsync(
            PublishClientInput input,
            [Service] IMessageSender <PublishDocumentMessage> messageSender,
            [Service] IFileStorage fileStorage,
            [Service] ISessionCreator sessionCreator,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] ClientByNameDataLoader clientDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Client client = await clientDataLoader.LoadAsync(
                input.ClientName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            string sessionId = await sessionCreator.CreateSessionAsync(
                cancellationToken)
                               .ConfigureAwait(false);

            var documentInfos = new List <DocumentInfo>();

            if (input.Files.Count > 0)
            {
                IFileContainer container = await fileStorage.CreateContainerAsync(
                    sessionId, cancellationToken)
                                           .ConfigureAwait(false);

                foreach (QueryFile file in input.Files)
                {
                    await container.CreateTextFileAsync(
                        file.Name, file.SourceText, cancellationToken)
                    .ConfigureAwait(false);

                    documentInfos.Add(new DocumentInfo(
                                          file.Name,
                                          file.Hash,
                                          file.HashAlgorithm,
                                          file.HashFormat));
                }
            }

            await messageSender.SendAsync(
                new PublishDocumentMessage(
                    sessionId,
                    environment.Id,
                    schema.Id,
                    client.Id,
                    input.ExternalId,
                    input.Format == QueryFileFormat.GraphQL
                        ? DocumentType.Query
                        : DocumentType.Relay,
                    documentInfos,
                    input.Tags is null
                        ? Array.Empty <Tag>()
                        : input.Tags.Select(t => new Tag(t.Key, t.Value)).ToArray()),
                cancellationToken)
            .ConfigureAwait(false);

            return(new PublishClientPayload(sessionId, input.ClientMutationId));
        }
        public async Task <PublishSchemaPayload> PublishSchemaAsync(
            PublishSchemaInput input,
            [Service] ISchemaRepository schemaRepository,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            SchemaVersion?schemaVersion;
            string?       hash = input.Hash;

            if (hash is null)
            {
                if (input.SourceText is null)
                {
                    throw new GraphQLException(
                              Resources.SchemaMutations_HashAndSourceTextAreNull);
                }

                using var sha = SHA256.Create();
                hash          = Convert.ToBase64String(sha.ComputeHash(
                                                           Encoding.UTF8.GetBytes(input.SourceText)));
            }

            schemaVersion = await schemaRepository.GetSchemaVersionAsync(
                hash, cancellationToken)
                            .ConfigureAwait(false);

            if (schemaVersion is null && input.SourceText is null)
            {
                throw new GraphQLException(
                          Resources.SchemaMutations_HashNotFound);
            }

            if (schemaVersion is null)
            {
                schemaVersion = await CreateSchemaVersionAsync(
                    input.SourceText !,
                    input.Tags ?? Array.Empty <TagInput>(),
                    schema,
                    schemaRepository,
                    cancellationToken)
                                .ConfigureAwait(false);
            }
            else
            {
                schemaVersion = await UpdateSchemaVersionAsync(
                    schemaVersion,
                    input.Tags ?? Array.Empty <TagInput>(),
                    schemaRepository,
                    cancellationToken)
                                .ConfigureAwait(false);
            }

            SchemaPublishReport report = await TryCreateReportAsync(
                schemaVersion.Id,
                environment.Id,
                schemaRepository,
                cancellationToken)
                                         .ConfigureAwait(false);

            return(new PublishSchemaPayload(schemaVersion, report, input.ClientMutationId));
        }