Exemplo n.º 1
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));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
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));
        }