public class GraphQLSchemaGenerator extends Object
This class is the main entry point to the library. It is used to generate a GraphQL schema by analyzing the registered classes
and exposing the chosen methods as GraphQL queries or mutations. The process of choosing the methods to expose is delegated
to ResolverBuilder instances, and a different set of builders can be attached to each registered class.
One such coupling of a registered class and a set of builders is modeled by an instance of OperationSource.
Methods of the with*OperationSource family are used to register sources to be analyzed.
Builders can also be registered globally (to be used when none are provided explicitly) via withResolverBuilders(ResolverBuilder...).
The process of mapping the Java methods to GraphQL queries/mutations will also transparently map all encountered Java types
to corresponding GraphQL types. The entire mapping process is handled by an instance OperationMapper where actual type
mapping is delegated to different instances of TypeMapper.
To customize the mapping process, clients can registers their own TypeMappers using withTypeMappers(TypeMapper...).
Runtime conversion between values provided by the GraphQL client and those expected by Java code might be needed.
This is handled by InputConverter instances.
Similarly, the conversion between values returned by Java code and those expected by the GraphQL client (if needed) is
handled by OutputConverter instances.
Custom implementations of both InputConverter and OutputConverter can be provided using
withInputConverters(InputConverter[]) and withOutputConverters(OutputConverter[]) respectively.
Example:
UserService userService = new UserService(); //could also be injected by a framework
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withOperationsFromSingletons(userService) //register an operations source and use the default strategy
.withNestedResolverBuildersForType(User.class, new BeanResolverBuilder()) //customize how queries are extracted from User.class
.generate();
GraphQL graphQL = new GraphQL(schema);
//keep the reference to GraphQL instance and execute queries against it.
//this query selects a user by ID and requests name and regDate fields only
ExecutionResult result = graphQL.execute(
"{ user (id: 123) {
name,
regDate
}}");
| Constructor and Description |
|---|
GraphQLSchemaGenerator()
Default constructor
|
GraphQLSchemaGenerator(String queryRoot,
String mutationRoot,
String subscriptionRoot)
Constructor which allows to customize names of root types.
|
GraphQLSchemaGenerator(String queryRoot,
String queryRootDescription,
String mutationRoot,
String mutationRootDescription,
String subscriptionRoot,
String subscriptionRootDescription)
Constructor which allows to customize names of root types.
|
public GraphQLSchemaGenerator()
public GraphQLSchemaGenerator(String queryRoot, String mutationRoot, String subscriptionRoot)
queryRoot - name of query root typemutationRoot - name of mutation root typesubscriptionRoot - name of subscription root typepublic GraphQLSchemaGenerator(String queryRoot, String queryRootDescription, String mutationRoot, String mutationRootDescription, String subscriptionRoot, String subscriptionRootDescription)
queryRoot - name of query root typemutationRoot - name of mutation root typesubscriptionRoot - name of subscription root typepublic GraphQLSchemaGenerator withOperationsFromSingleton(Object serviceSingleton)
serviceSingleton as a singleton OperationSource,
with its class (obtained via Object.getClass()) as its runtime type and with the globally registered
ResolverBuilders.
All query/mutation methods discovered by analyzing the serviceSingleton's type will be later,
in query resolution time, invoked on this specific instance (hence the 'singleton' in the method name).
Instances of stateless service classes are commonly registered this way.serviceSingleton - The singleton bean whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromSingleton(Object serviceSingleton, Type beanType)
serviceSingleton as a singleton OperationSource,
with beanType as its runtime type and with the globally registered ResolverBuilders.
serviceSingleton - The singleton bean whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timebeanType - Runtime type of serviceSingleton. Should be explicitly provided when it differs from its class
(that can be obtained via Object.getClass()). This is commonly the case when the class is generic
or when the instance has been proxied by a framework.
Use TypeToken to get a Type literal
or TypeFactory to create it dynamically.GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromSingleton(Object serviceSingleton, AnnotatedType beanType)
withOperationsFromSingleton(Object, Type), except that an AnnotatedType is used as
serviceSingleton's runtime type. Needed when type annotations such as GraphQLNonNull
not directly declared on the class should be captured.serviceSingleton - The singleton bean whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timebeanType - Runtime type of serviceSingleton. Should be explicitly provided when it differs from its class
(that can be obtained via Object.getClass()) and when annotations on the type should be kept.
Use TypeToken to get an AnnotatedType literal
or TypeFactory to create it dynamically.GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromSingleton(Object serviceSingleton, ResolverBuilder... builders)
withOperationsFromSingleton(Object) except that custom ResolverBuilders will be used
to look through beanType for methods to be exposed.serviceSingleton - The singleton bean whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timebuilders - Custom strategy to use when analyzing beanTypeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromSingleton(Object serviceSingleton, Type beanType, ResolverBuilder... builders)
withOperationsFromSingleton(Object, Type) except that custom ResolverBuilders will be used
to look through beanType for methods to be exposed.serviceSingleton - The singleton bean whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timebeanType - Runtime type of serviceSingleton. Should be explicitly provided when it differs from its class
(that can be obtained via Object.getClass()). This is commonly the case when the class is generic
or when the instance has been proxied by a framework.
Use TypeToken to get a Type literal
or TypeFactory to create it dynamically.builders - Custom strategy to use when analyzing beanTypeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromSingleton(Object serviceSingleton, AnnotatedType beanType, ResolverBuilder... builders)
withOperationsFromSingleton(Object, AnnotatedType) except that custom ResolverBuilders will be used
to look through beanType for methods to be exposed.serviceSingleton - The singleton bean whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timebeanType - Runtime type of serviceSingleton. Should be explicitly provided when it differs from its class
(that can be obtained via Object.getClass()) and when annotations on the type should be kept.
Use TypeToken to get an AnnotatedType literal
or TypeFactory to create it dynamically.builders - Custom builders to use when analyzing beanTypeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromSingletons(Object... serviceSingletons)
withOperationsFromSingleton(Object) except that multiple beans can be registered at the same time.serviceSingletons - Singleton beans whose type is to be scanned for query/mutation methods and on which
those methods will be invoked in query/mutation execution timeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOperationsFromType(Type serviceType)
public GraphQLSchemaGenerator withOperationsFromType(Type serviceType, ResolverBuilder... builders)
public GraphQLSchemaGenerator withOperationsFromTypes(Type... serviceType)
public GraphQLSchemaGenerator withOperationsFromType(AnnotatedType serviceType)
public GraphQLSchemaGenerator withOperationsFromType(AnnotatedType serviceType, ResolverBuilder... builders)
public GraphQLSchemaGenerator withOperationsFromTypes(AnnotatedType... serviceType)
public GraphQLSchemaGenerator withNestedOperationsFromTypes(Type... types)
types - The domain types that are to be scanned for query/mutation methodsGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withNestedOperationsFromTypes(AnnotatedType... types)
withNestedOperationsFromTypes(Type...) except that an AnnotatedType is used,
so any extra annotations on the type (not only those directly on the class) are kept.types - The domain types that are to be scanned for query/mutation methodsGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withNestedResolverBuildersForType(Type querySourceType, ResolverBuilder... resolverBuilders)
querySourceType type to be scanned for exposed methods, using the provided ResolverBuilders.
Domain types are discovered dynamically, when referred to by an exposed method (either as its parameter type or return type).
This method gives a way to customize how the discovered domain type will be analyzed.querySourceType - The domain type that is to be scanned for query/mutation methodsresolverBuilders - Custom resolverBuilders to use when analyzing querySourceType typeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withNestedResolverBuildersForType(AnnotatedType querySourceType, ResolverBuilder... resolverBuilders)
withNestedResolverBuildersForType(Type, ResolverBuilder...) except that an AnnotatedType is used
so any extra annotations on the type (not only those directly on the class) are kept.querySourceType - The annotated domain type that is to be scanned for query/mutation methodsresolverBuilders - Custom resolverBuilders to use when analyzing querySourceType typeGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withResolverBuilders(ResolverBuilder... resolverBuilders)
ResolverBuilders to be used for sources that don't have explicitly assigned builders.resolverBuilders - builders to be globally registeredGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withResolverBuilders(ExtensionProvider<GeneratorConfiguration,ResolverBuilder> provider)
public GraphQLSchemaGenerator withNestedResolverBuilders(ResolverBuilder... resolverBuilders)
ResolverBuilders to be used for sources that don't have explicitly assigned builders.resolverBuilders - builders to be globally registeredGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withNestedResolverBuilders(ExtensionProvider<GeneratorConfiguration,ResolverBuilder> provider)
public GraphQLSchemaGenerator withInputFieldBuilders(InputFieldBuilder... inputFieldBuilders)
public GraphQLSchemaGenerator withInputFieldBuilders(ExtensionProvider<ExtendedGeneratorConfiguration,InputFieldBuilder> provider)
public GraphQLSchemaGenerator withAbstractInputTypeResolution()
public GraphQLSchemaGenerator withAbstractInputHandler(AbstractInputHandler abstractInputHandler)
public GraphQLSchemaGenerator withBasePackages(String... basePackages)
public GraphQLSchemaGenerator withStringInterpolation(MessageBundle... messageBundles)
public GraphQLSchemaGenerator withJavaDeprecationRespected(boolean respectJavaDeprecation)
public GraphQLSchemaGenerator withJavaDeprecationReason(String deprecationReason)
public GraphQLSchemaGenerator withTypeInfoGenerator(TypeInfoGenerator typeInfoGenerator)
public GraphQLSchemaGenerator withValueMapperFactory(ValueMapperFactory valueMapperFactory)
public GraphQLSchemaGenerator withInterfaceMappingStrategy(InterfaceMappingStrategy interfaceStrategy)
public GraphQLSchemaGenerator withScalarDeserializationStrategy(ScalarDeserializationStrategy scalarStrategy)
public GraphQLSchemaGenerator withInclusionStrategy(InclusionStrategy inclusionStrategy)
public GraphQLSchemaGenerator withImplementationDiscoveryStrategy(ImplementationDiscoveryStrategy implDiscoveryStrategy)
public GraphQLSchemaGenerator withTypeTransformer(TypeTransformer transformer)
public GraphQLSchemaGenerator withTypeMappers(TypeMapper... typeMappers)
TypeMappers to be used for mapping Java type to GraphQL types.
Ordering of mappers is strictly important as the first TypeMapper that supports the given Java type
will be used for mapping it.
typeMappers - Custom type mappers to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withTypeMappersPrepended(TypeMapper... typeMappers)
public GraphQLSchemaGenerator withTypeMappers(ExtensionProvider<GeneratorConfiguration,TypeMapper> provider)
TypeMappers to be used for mapping Java type to GraphQL types.
Ordering of mappers is strictly important as the first TypeMapper that supports the given Java type
will be used for mapping it.
provider - Provides the customized list of TypeMappers to useGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withSchemaTransformers(SchemaTransformer... transformers)
public GraphQLSchemaGenerator withSchemaTransformers(ExtensionProvider<GeneratorConfiguration,SchemaTransformer> provider)
public GraphQLSchemaGenerator withInputConverters(InputConverter<?,?>... inputConverters)
InputConverters to be used for converting values provided by the GraphQL client
into those expected by the corresponding Java method. Only needed in some specific cases when usual deserialization
isn't enough, for example, when a client-provided List should be repackaged into a Map,
which is normally done because GraphQL type system has no direct support for maps.
Ordering of converters is strictly important as the first InputConverter that supports the given Java type
will be used for converting it.
inputConverters - Custom input converters to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withInputConvertersPrepended(InputConverter<?,?>... inputConverters)
public GraphQLSchemaGenerator withInputConverters(ExtensionProvider<GeneratorConfiguration,InputConverter> provider)
public GraphQLSchemaGenerator withOutputConverters(OutputConverter<?,?>... outputConverters)
OutputConverters to be used for converting values returned by the exposed Java method
into those expected by the GraphQL client. Only needed in some specific cases when usual serialization isn't enough,
for example, when an instance of Map should be repackaged into a List, which
is normally done because GraphQL type system has no direct support for maps.
Ordering of converters is strictly important as the first OutputConverter that supports the given Java type
will be used for converting it.
outputConverters - Custom output converters to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withOutputConvertersPrepended(OutputConverter<?,?>... outputConverters)
public GraphQLSchemaGenerator withOutputConverters(ExtensionProvider<GeneratorConfiguration,OutputConverter> provider)
public GraphQLSchemaGenerator withTypeAdapters(AbstractTypeAdapter<?,?>... typeAdapters)
AbstractTypeAdapter) are both type mappers and bi-directional converters,
implementing TypeMapper, InputConverter and OutputConverter.
They're used in the same way as mappers/converters individually, and exist solely because it can sometimes
be convenient to group the logic for mapping and converting to/from the same Java type in one place.
For example, because GraphQL type system has no notion of maps, Maps require special logic
both when mapping them to a GraphQL type and when converting them before and after invoking a Java method.
For this reason, all code dealing with translating Maps is kept in one place in
MapToListTypeAdapter.
Ordering of mappers/converters is strictly important as the first one supporting the given Java type will be used to map/convert it.
See withTypeMappers(ExtensionProvider)
typeAdapters - Custom type adapters to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withArgumentInjectors(ArgumentInjector... argumentInjectors)
public GraphQLSchemaGenerator withArgumentInjectors(ExtensionProvider<GeneratorConfiguration,ArgumentInjector> provider)
public GraphQLSchemaGenerator withModules(Module... modules)
public GraphQLSchemaGenerator withModules(ExtensionProvider<GeneratorConfiguration,Module> provider)
public GraphQLSchemaGenerator withResolverInterceptors(ResolverInterceptor... interceptors)
public GraphQLSchemaGenerator withResolverInterceptorFactories(ExtensionProvider<GeneratorConfiguration,ResolverInterceptorFactory> provider)
public GraphQLSchemaGenerator withAdditionalTypes(Collection<graphql.schema.GraphQLType> additionalTypes)
public GraphQLSchemaGenerator withAdditionalDirectives(Type... additionalDirectives)
public GraphQLSchemaGenerator withAdditionalDirectives(AnnotatedType... additionalDirectives)
public GraphQLSchemaGenerator withAdditionalDirectives(graphql.schema.GraphQLDirective... additionalDirectives)
public GraphQLSchemaGenerator withTypeSynonymGroup(Type... synonyms)
public GraphQLSchemaGenerator withTypeSynonymGroup(AnnotatedType... synonyms)
public GraphQLSchemaGenerator withTypeComparator(Comparator<AnnotatedType> comparator)
public GraphQLSchemaGenerator withOperationBuilder(OperationBuilder operationBuilder)
public GraphQLSchemaGenerator withDirectiveBuilder(DirectiveBuilder directiveBuilder)
public GraphQLSchemaGenerator withRelayCompliantMutations()
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withRelayCompliantMutations(String wrapperFieldName, String wrapperFieldDescription)
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withRelayNodeInterfaceInference(boolean enabled)
enabled - Whether the inference should be enabledGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withRelayConnectionCheckRelaxed()
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withSchemaProcessors(GraphQLSchemaProcessor... processors)
processors - Custom processors to call right before the GraphQL schema is builtGraphQLSchemaGenerator instance, to allow method chainingpublic graphql.schema.GraphQLSchema generate()
GraphQL instances. See the example in the description of this class.Copyright © 2016–2018. All rights reserved.