Schema parser options
For advanced use-cases, the schema parser can be tweaked to suit your needs.
Use SchemaParserOptions.newBuilder() to build an options object to pass to the parser.
Options:
contextClass: If you use a custom context object to execute your queries, let the parser know about it so that it can add it to data fetchers as an argument.
Make sure to also provide your context instance to the execution input:var executionInput = ExecutionInput.newExecutionInput() .query(query) .variables(variables) .graphQLContext(Map.of(CustomContext.class, context));genericWrappers: Allows defining your own generic classes that should be unwrapped when matching Java types to GraphQL types. You must supply the class and the index (zero-indexed) of the wrapped generic type. For example: If you want to unwrap type argumentTofFuture<T>, you must passFuture.classand0. UseGenericWrapper.withTransformerto also convert the value at runtime. If the transformer returnsnull, the field isnull. If the transformer returns a future of the wrapper’s content, such as Reactor’sMono::toFuture, register it withGenericWrapper.withCompletionStageTransformer(Mono.class, 0, Mono::toFuture)instead. A future declared inside the wrapper, likeMono<CompletableFuture<Item>>, is then waited on too. WithwithTransformer, graphql-java gets that inner future as the value. Wrappers and futures nested in lists or in other wrappers are unwrapped too, e.g.List<Try<Payload>>orList<CompletableFuture<Item>>. If one element fails, the whole field fails. A transformer or nested future that calls a DataLoader after an async step needs graphql-java’s DataLoader chaining (DataLoaderDispatchingContextKeys.setEnableDataLoaderChaining), otherwise the field never completes.useDefaultGenericWrappers: Defaults totrue. Tells the parser whether or not to add it’s own list of well-known generic wrappers, such asFutureandCompletableFuture. graphql-java only waits on aCompletionStage, so a method declared to return aFuturehas to return one, such as aCompletableFuture. Any other future fails the field with an error, unless a generic wrapper transformer converts it.allowUnimplementedResolvers: Defaults tofalse. Allows a schema to be created even if not all GraphQL fields have resolvers. Intended only for development, it will log a warning to remind you to turn it off for production. Any unimplemented resolvers will throw errors when queried.missingResolverDataFetcherProvider: Allows you to provide custom behavior for missing GraphQL fields. It gets the field definition and the options, and returns theDataFetcherfor that field, e.g..missingResolverDataFetcherProvider { field, options -> fetcher }.inputArgumentOptionalDetectOmission: Defaults tofalse. By default, the parser will treat omitted or null method input arguments asOptional.emptyin resolvers. If you prefer, you can disable this behavior.objectMapperConfigurer/objectMapperProvider: The JacksonObjectMapperthat converts the arguments of method resolvers. It’s a Jackson 3 mapper (tools.jackson). By default all fields share one mapper with the Kotlin module, which fails on unknown properties and converts enums to and from Strings byname().objectMapperConfigurercustomizes that mapper per field, and can do it differently based on the GraphQL field definition. It gets the mapper’sJsonMapper.Builder, e.g..objectMapperConfigurer { builder, context -> builder.addModule(MyModule()) }, and a module added there replaces the library’s module of the same type.objectMapperProvideruses your own mappers as they are, e.g..objectMapperProvider(field -> springMapper)to share a Spring Boot 4 app’sJsonMapper.preferGraphQLResolver: In cases where you have a Resolver class and legacy class that conflict on type arguments, use the Resolver class instead of throwing an error. Specifically this situation can occur when you have a graphql schema typeFoowith abarsproperty and classes:// legacy class you can't change class Foo { Set<Bar> getBars() { //...returns set of bars... } } // nice resolver that does what you want class FooResolver implements GraphQLResolver<Foo> { Set<BarDTO> getBars() { // ...converts Bar objects to BarDTO objects and returns set... } }You will now have the code find two different return types for getBars() and application will not start with the error
Caused by: graphql.kickstart.tools.SchemaClassScannerError: Two different classes used for typeIf this property is true it will ignore the legacy version.addProxyHandler: If your runtime resolver classes are auto-generated proxies of some kind you can provide a handler to help the parser find the real resolvers behind them. Four proxy handlers are provided by default for these libraries:- Javassist
- Guice
- Spring AOP
- Weld
A resolver that is a JDK dynamic proxy fails the build, since its methods can’t be called on the proxy. Use class-based proxies, e.g.
proxyTargetClass = truein Spring.fieldVisibility: Provide a graphql field visibility implementation. See Field Visibility. To turn introspection off, use graphql-java’s switches, for the whole JVM or for one request:Introspection.enabledJvmWide(false); var executionInput = ExecutionInput.newExecutionInput() .query(query) .graphQLContext(Map.of(Introspection.INTROSPECTION_DISABLED, true));coroutineContext/coroutineContextProvider: Provide a kotlin coroutine context to be used with suspend functions of resolvers. A suspend function runs on the calling thread until it first suspends, and continues on this context after that, so wrap blocking calls inwithContext(Dispatchers.IO). To run them in your ownCoroutineScope, e.g. to cancel them with the request, put it in the execution input’s context:var executionInput = ExecutionInput.newExecutionInput() .query(query) .graphQLContext(Map.of(CoroutineScope.class, scope));typeDefinitionFactory: See Type Definition Factory.includeUnusedTypes: Defaults tofalse. By default, the parser will ignore unused type definitions in the schema. Enable this option to include them regardless.allowUndeclaredDirectives: Defaults tofalse. Allows applying directives that aren’t declared in the schema, e.g. the Apollo Federation directives imported through@link. They’re only added as applied directives, with argument types guessed from their values. Directives with enum or input object arguments still have to be declared.unmappedInputFields: Defaults toWARN. What to do about input type fields that the class of a resolver argument has no property for. When a client sends such a field, Jackson fails to convert the argument, or drops the value if theObjectMapperignores unknown properties, like plain Jackson 3 and Spring Boot mappers do. The classes are checked when the schema is built, with each field’sObjectMapper, andWARNlogs a warning for each of these fields.FAILfails the build instead, andIGNOREskips the check. To leave a field unmapped on purpose, add it to@JsonIgnorePropertieson the class. Classes with a custom deserializer, a builder or a delegating creator aren’t checked, and neither are arguments bound to aMapor aJsonNode.