Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.apollographql.federation.graphqljava.caching;
import com.apollographql.federation.graphqljava._Entity;
import graphql.ExecutionResult;
import graphql.GraphQLContext;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.InstrumentationState;
import graphql.execution.instrumentation.SimplePerformantInstrumentation;
import graphql.execution.instrumentation.parameters.InstrumentationCreateStateParameters;
import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters;
import graphql.execution.instrumentation.parameters.InstrumentationFieldParameters;
import graphql.schema.*;
import java.util.*;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
/**
* A GraphQL Java Instrumentation that computes a max age for an operation based on @cacheControl
* directives.
*
*
By default, this instrumentation will only set the `Cache-Control` `max-age` value IF positive
* int value is provided. If you would rather want to return explicit `max-age=0` values, you need
* to explicitly opt-in to this behavior by specifying `allowZeroMaxAge=true` constructor value.
*
*
You can retrieve the "max-age=..." header value with a {@link GraphQLContext}:
* String cacheControlHeader = CacheControlInstrumentation.cacheControlContext(context);
*
*
*
See @cacheControl
* documentation and the original Apollo Server plugin-response-cache
* implementation.
*/
public class CacheControlInstrumentation extends SimplePerformantInstrumentation {
private final int defaultMaxAge;
private final boolean allowZeroMaxAge;
private static final Object CONTEXT_KEY = new Object();
private static final String DIRECTIVE_NAME = "cacheControl";
private static final String MAX_AGE = "maxAge";
private static final String SCOPE = "scope";
private static final String INHERIT_MAX_AGE = "inheritMaxAge";
public CacheControlInstrumentation() {
this(0, false);
}
public CacheControlInstrumentation(int defaultMaxAge) {
this(defaultMaxAge, false);
}
public CacheControlInstrumentation(int defaultMaxAge, boolean allowZeroMaxAge) {
this.defaultMaxAge = defaultMaxAge;
this.allowZeroMaxAge = allowZeroMaxAge;
}
@Nullable
public static String cacheControlHeaderFromGraphQLContext(GraphQLContext context) {
return context.get(CONTEXT_KEY);
}
@Override
public InstrumentationState createState(InstrumentationCreateStateParameters parameters) {
return new CacheControlState(allowZeroMaxAge);
}
@Override
public InstrumentationContext beginExecution(
InstrumentationExecutionParameters parameters, InstrumentationState state) {
return new InstrumentationContext<>() {
@Override
public void onDispatched() {
// do nothing
}
@Override
public void onCompleted(ExecutionResult executionResult, Throwable throwable) {
CacheControlState cacheControlState = (CacheControlState) state;
// Attach the policy to the context object
cacheControlState
.overallPolicy
.maybeAsString()
.ifPresent(s -> parameters.getGraphQLContext().put(CONTEXT_KEY, s));
}
};
}
@Override
public @Nullable InstrumentationContext