Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Last active June 9, 2024 01:44
Show Gist options
  • Save aoudiamoncef/ad81ab4407c6c13e37004dd696775a4a to your computer and use it in GitHub Desktop.
Save aoudiamoncef/ad81ab4407c6c13e37004dd696775a4a to your computer and use it in GitHub Desktop.
GraphQL Java Spring Boot 2 configuration with queries cache(Caffeine)
@Component
public class CacheProvider implements PreparsedDocumentProvider {
@Autowired
private AppProperties appProps;
@Override
public PreparsedDocumentEntry getDocument(ExecutionInput executionInput, Function<ExecutionInput, PreparsedDocumentEntry> computeFunction) {
final Cache<String, PreparsedDocumentEntry> cache = Caffeine.newBuilder()
.maximumSize(appProps.getCache()
.getRequests()
.getSize())
.build();
Function<String, PreparsedDocumentEntry> mapCompute = key -> computeFunction.apply(executionInput);
return cache.get(executionInput.getQuery(), mapCompute);
}
}
@Slf4j
@Configuration
public class GraphQLConfig {
@Autowired
private ApplicationProperties appProps;
@Autowired
private AuthorResolver authorResolver;
@Autowired
private BookResolver bookResolver;
@Autowired
private CacheProvider cacheProvider;
@Autowired
private Instrumentation instrumentation;
private GraphQL graphQL;
@PostConstruct
public void init() {
final List<String> sdl = new ArrayList<>();
final String schemaAuthor = resourceToString(SCHEMA_AUTHOR);
final String schemaBook = resourceToString(SCHEMA_BOOK);
sdl.add(schemaAuthor);
sdl.add(schemaBook);
final GraphQLSchema graphQLSchema = buildSchema(sdl);
this.graphQL = GraphQL
.newGraphQL(graphQLSchema)
.queryExecutionStrategy(
new AsyncExecutionStrategy(new GraphQLDataFetcherExceptionHandler())
)
.preparsedDocumentProvider(cacheProvider)
.instrumentation(instrumentation)
.build();
}
private GraphQLSchema buildSchema(final List<String> sdl) {
final SchemaParser schemaParser = new SchemaParser();
final TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
sdl.forEach(schema -> typeRegistry.merge(schemaParser.parse(schema)));
final RuntimeWiring runtimeWiring = buildWiring();
final SchemaGenerator schemaGenerator = new SchemaGenerator();
return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
}
private RuntimeWiring buildWiring() {
return RuntimeWiring.newRuntimeWiring()
.type(QUERY_TYPE, typeWiring -> typeWiring
.dataFetcher(FIND_AUTHOR_BY_NAME, authorResolver.getAuthorByName())
.dataFetcher(FIND_BOOK_BY_NAME, bookResolver.getBookByName())
)
.directive(UPPERCASE, new UppercaseDirective())
.build();
}
@Bean
public GraphQL graphQL() {
return graphQL;
}
private String resourceToString(final String pathResource) {
try (final InputStream is = new ClassPathResource(pathResource).getInputStream()) {
return IOUtils.toString(is, StandardCharsets.UTF_8);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment