Search in sources :

Example 11 with DataFetcherResult

use of graphql.execution.DataFetcherResult in project teamcity-rest by JetBrains.

the class Mutation method assignBuildTypeWithAgent.

@Used("graphql")
@NotNull
public DataFetcherResult<AssignBuildTypeWithAgentPayload> assignBuildTypeWithAgent(@NotNull AssignBuildTypeWithAgentInput input) {
    return runWithAgent(input.getAgentRawId(), agent -> {
        DataFetcherResult.Builder<AssignBuildTypeWithAgentPayload> result = DataFetcherResult.newResult();
        SBuildType bt = myBuildTypeFinder.getItem("id:" + input.getBuildTypeRawId()).getBuildType();
        if (bt == null) {
            final String errorMessage = String.format("Build type with id=%s is not found.", input.getBuildTypeRawId());
            return result.error(new EntityNotFoundGraphQLError(errorMessage)).build();
        }
        myAgentTypeManager.includeRunConfigurationsToAllowed(agent.getAgentTypeId(), new String[] { bt.getInternalId() });
        return result.data(new AssignBuildTypeWithAgentPayload(new Agent(agent), new BuildType(bt))).build();
    });
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) BuildType(jetbrains.buildServer.server.graphql.model.buildType.BuildType) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with DataFetcherResult

use of graphql.execution.DataFetcherResult in project teamcity-rest by JetBrains.

the class Mutation method authorizeAgent.

@Used("graphql")
@NotNull
public DataFetcherResult<AuthorizeAgentPayload> authorizeAgent(@NotNull AuthorizeAgentInput input, @NotNull DataFetchingEnvironment dfe) {
    return runWithAgent(input.getAgentRawId(), agent -> {
        DataFetcherResult.Builder<AuthorizeAgentPayload> result = DataFetcherResult.newResult();
        GraphQLContext context = dfe.getContext();
        String authReason = input.getReason() == null ? "" : input.getReason();
        // Move agent to another pool first as we don't want some cheeky build to start while agent is in a wrong pool.
        if (input.getTargetAgentPoolRawId() != null) {
            try {
                myAgentPoolManager.moveAgentToPool(input.getTargetAgentPoolRawId(), agent);
            } catch (NoSuchAgentPoolException e) {
                return result.error(new EntityNotFoundGraphQLError(String.format("Agent pool with id=%d is not found.", input.getTargetAgentPoolRawId()))).build();
            } catch (PoolQuotaExceededException e) {
                LOG.debug(e.getMessage());
                return result.error(new OperationFailedGraphQLError(String.format("Agent pool with id=%d does not accept agents.", input.getTargetAgentPoolRawId()))).build();
            } catch (AgentTypeCannotBeMovedException e) {
                LOG.debug(e.getMessage());
                return result.error(new OperationFailedGraphQLError(String.format("Agent with id=%d can not be moved.", input.getAgentRawId()))).build();
            }
        }
        agent.setAuthorized(true, context.getUser(), authReason);
        Agent agentModel = new Agent(agent);
        AbstractAgentPool targetPoolModel = null;
        if (input.getTargetAgentPoolRawId() != null) {
            AgentPool realPool = myAgentPoolManager.findAgentPoolById(input.getTargetAgentPoolRawId());
            if (realPool != null) {
                targetPoolModel = myAgentPoolFactory.produce(realPool);
            }
        }
        return result.data(new AuthorizeAgentPayload(agentModel, targetPoolModel)).build();
    });
}
Also used : AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) AbstractAgentPool(jetbrains.buildServer.server.graphql.model.agentPool.AbstractAgentPool) DataFetcherResult(graphql.execution.DataFetcherResult) OperationFailedGraphQLError(jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError) GraphQLContext(jetbrains.buildServer.server.graphql.GraphQLContext) EntityNotFoundGraphQLError(jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError) Used(jetbrains.buildServer.Used) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with DataFetcherResult

use of graphql.execution.DataFetcherResult in project stargate by stargate.

the class UpdateFetcher method getPayload.

@Override
protected MutationPayload<DataFetcherResult<Object>> getPayload(DataFetchingEnvironment environment, StargateGraphqlContext context) throws UnauthorizedException {
    DataFetchingFieldSelectionSet selectionSet = environment.getSelectionSet();
    EntityModel entityModel = model.getEntity();
    Keyspace keyspace = context.getDataStore().schema().keyspace(entityModel.getKeyspaceName());
    // We're either getting the values from a single entity argument, or individual PK field
    // arguments:
    Predicate<String> hasArgument;
    Function<String, Object> getArgument;
    if (model.getEntityArgumentName().isPresent()) {
        Map<String, Object> entity = environment.getArgument(model.getEntityArgumentName().get());
        hasArgument = entity::containsKey;
        getArgument = entity::get;
    } else {
        hasArgument = environment::containsArgument;
        getArgument = environment::getArgument;
    }
    List<BuiltCondition> whereConditions = bindWhere(model.getWhereConditions(), hasArgument, getArgument, entityModel::validateForUpdate, keyspace);
    List<BuiltCondition> ifConditions = bindIf(model.getIfConditions(), hasArgument, getArgument, keyspace);
    Collection<ValueModifier> modifiers;
    if (!model.getIncrementModels().isEmpty()) {
        modifiers = buildIncrementModifiers(model.getIncrementModels(), keyspace, hasArgument, getArgument);
    } else {
        modifiers = buildModifiers(entityModel, keyspace, hasArgument, getArgument);
    }
    // we are supporting BigInt and GraphQLString for ISO date format
    Optional<Long> timestamp = TimestampParser.parse(model.getCqlTimestampArgumentName(), environment);
    AbstractBound<?> query = buildUpdateQuery(entityModel, modifiers, whereConditions, ifConditions, timestamp, context);
    List<TypedKeyValue> primaryKey = TypedKeyValue.forDML((BoundDMLQuery) query);
    authorizeUpdate(entityModel, primaryKey, context);
    Function<List<MutationResult>, DataFetcherResult<Object>> resultBuilder = getDeleteOrUpdateResultBuilder(environment);
    return new MutationPayload<>(query, primaryKey, resultBuilder);
}
Also used : ValueModifier(io.stargate.db.query.builder.ValueModifier) DataFetchingFieldSelectionSet(graphql.schema.DataFetchingFieldSelectionSet) EntityModel(io.stargate.graphql.schema.graphqlfirst.processor.EntityModel) DataFetcherResult(graphql.execution.DataFetcherResult) BuiltCondition(io.stargate.db.query.builder.BuiltCondition) TypedKeyValue(io.stargate.auth.TypedKeyValue) Keyspace(io.stargate.db.schema.Keyspace) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with DataFetcherResult

use of graphql.execution.DataFetcherResult in project graphql-orchestrator-java by intuit.

the class BatchResultTransformerTest method defaultBatchTransformerBatchResultWithError.

@Test
public void defaultBatchTransformerBatchResultWithError() {
    batchResults.put("field1", "value1");
    GraphQLError error = GraphqlErrorBuilder.newError().message("boom").build();
    final DataFetchingEnvironment dataFetchingEnvironment = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().mergedField(newMergedField().addField(newField("field1").build()).build()).build();
    environments.add(dataFetchingEnvironment);
    dataFetcherResult = DataFetcherResult.<Map<String, Object>>newResult().data(batchResults).error(error).build();
    final List<DataFetcherResult<Object>> results = batchResultTransformer.toBatchResult(dataFetcherResult, environments);
    assertThat(results).hasSize(1).extracting(DataFetcherResult::getData).containsOnly("value1");
    assertThat(results).flatExtracting(DataFetcherResult::getErrors).containsOnly(error);
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) GraphQLError(graphql.GraphQLError) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with DataFetcherResult

use of graphql.execution.DataFetcherResult in project graphql-orchestrator-java by intuit.

the class BatchResultTransformerTest method defaultBatchTransformerWithMatchingPathError.

@Test
public void defaultBatchTransformerWithMatchingPathError() {
    final DataFetchingEnvironment environment = DataFetchingEnvironmentImpl.newDataFetchingEnvironment().mergedField(newMergedField().addField(newField("field1").build()).build()).build();
    environments.add(environment);
    GraphQLError correctError = GraphqlErrorBuilder.newError().message("boom").path(Arrays.asList("field1", 1, 2)).build();
    final GraphQLError shouldBeIgnoredError = GraphqlErrorBuilder.newError().message("boom").path(Arrays.asList("field2", 1, 2)).build();
    dataFetcherResult = DataFetcherResult.<Map<String, Object>>newResult().data(batchResults).error(correctError).error(shouldBeIgnoredError).build();
    final List<DataFetcherResult<Object>> results = batchResultTransformer.toBatchResult(dataFetcherResult, environments);
    assertThat(results).hasSize(1).flatExtracting(DataFetcherResult::getErrors).containsOnly(correctError).doesNotContain(shouldBeIgnoredError);
}
Also used : DataFetcherResult(graphql.execution.DataFetcherResult) GraphQLError(graphql.GraphQLError) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Test(org.junit.Test)

Aggregations

DataFetcherResult (graphql.execution.DataFetcherResult)35 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)18 NotNull (org.jetbrains.annotations.NotNull)17 EntityNotFoundGraphQLError (jetbrains.buildServer.server.graphql.util.EntityNotFoundGraphQLError)16 Used (jetbrains.buildServer.Used)14 Map (java.util.Map)12 OperationFailedGraphQLError (jetbrains.buildServer.server.graphql.util.OperationFailedGraphQLError)12 Test (org.junit.Test)12 HashMap (java.util.HashMap)10 ExecutionStepInfo (graphql.execution.ExecutionStepInfo)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 ArrayList (java.util.ArrayList)7 Collections (java.util.Collections)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 jetbrains.buildServer.server.graphql.model.mutation.agentPool (jetbrains.buildServer.server.graphql.model.mutation.agentPool)6 GraphQLError (graphql.GraphQLError)5 MergedField (graphql.execution.MergedField)5 Field (graphql.language.Field)5 OperationDefinition (graphql.language.OperationDefinition)5