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()
.withOperationsFromType(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(Object... serviceSingletons)
Construct with
serviceSingletons as singleton query sources with default builders |
| Modifier and Type | Method and Description |
|---|---|
graphql.schema.GraphQLSchema |
generate()
Generates a GraphQL schema based on the results of analysis of the registered sources.
|
GraphQLSchemaGenerator |
withAdditionalTypes(Collection<graphql.schema.GraphQLType> additionalTypes) |
GraphQLSchemaGenerator |
withArgumentInjectors(ArgumentInjector... argumentInjectors) |
GraphQLSchemaGenerator |
withBasePackage(String basePackage) |
GraphQLSchemaGenerator |
withDefaultArgumentInjectors() |
GraphQLSchemaGenerator |
withDefaultConverters()
Registers all built-in
InputConverters and OutputConverters. |
GraphQLSchemaGenerator |
withDefaultInputConverters() |
GraphQLSchemaGenerator |
withDefaultMappers()
Registers all built-in
TypeMappers |
GraphQLSchemaGenerator |
withDefaultNestedResolverBuilders()
Registers default resolver builders.
|
GraphQLSchemaGenerator |
withDefaultOutputConverters() |
GraphQLSchemaGenerator |
withDefaultResolverBuilders()
Registers default resolver builders.
|
GraphQLSchemaGenerator |
withDefaults()
|
GraphQLSchemaGenerator |
withInputConverters(InputConverter<?,?>... inputConverters)
Registers custom
InputConverters to be used for converting values provided by the GraphQL client
into those expected by the corresponding Java method. |
GraphQLSchemaGenerator |
withInputFieldDiscoveryStrategy(InputFieldDiscoveryStrategy inputFieldStrategy) |
GraphQLSchemaGenerator |
withInterfaceMappingStrategy(InterfaceMappingStrategy interfaceStrategy) |
GraphQLSchemaGenerator |
withMetaDataGenerator(TypeInfoGenerator typeInfoGenerator) |
GraphQLSchemaGenerator |
withNestedOperationsFromTypes(AnnotatedType... types)
The same as
withNestedOperationsFromTypes(Type...) except that an AnnotatedType is used,
so any extra annotations on the type (not only those directly on the class) are kept. |
GraphQLSchemaGenerator |
withNestedOperationsFromTypes(Type... types)
Register a type to be scanned for exposed methods, using the globally registered builders.
|
GraphQLSchemaGenerator |
withNestedResolverBuilders(ResolverBuilder... resolverBuilders)
Globally registers
ResolverBuilders to be used for sources that don't have explicitly assigned builders. |
GraphQLSchemaGenerator |
withNestedResolverBuildersForType(AnnotatedType querySourceType,
ResolverBuilder... resolverBuilders)
Same as
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. |
GraphQLSchemaGenerator |
withNestedResolverBuildersForType(Type querySourceType,
ResolverBuilder... resolverBuilders)
Register
querySourceType type to be scanned for exposed methods, using the provided ResolverBuilders. |
GraphQLSchemaGenerator |
withOperationsFromSingleton(Object serviceSingleton)
Register
serviceSingleton as a singleton OperationSource,
with its class (obtained via Object.getClass()) as its runtime type and with the globally registered
ResolverBuilders. |
GraphQLSchemaGenerator |
withOperationsFromSingleton(Object serviceSingleton,
AnnotatedType beanType)
Same as
withOperationsFromSingleton(Object, Type), except that an AnnotatedType is used as
serviceSingleton's runtime type. |
GraphQLSchemaGenerator |
withOperationsFromSingleton(Object serviceSingleton,
AnnotatedType beanType,
ResolverBuilder... builders)
Same as
withOperationsFromSingleton(Object, AnnotatedType) except that custom ResolverBuilders will be used
to look through beanType for methods to be exposed. |
GraphQLSchemaGenerator |
withOperationsFromSingleton(Object serviceSingleton,
ResolverBuilder... builders)
Same as
withOperationsFromSingleton(Object) except that custom ResolverBuilders will be used
to look through beanType for methods to be exposed. |
GraphQLSchemaGenerator |
withOperationsFromSingleton(Object serviceSingleton,
Type beanType)
Register
serviceSingleton as a singleton OperationSource,
with beanType as its runtime type and with the globally registered ResolverBuilders. |
GraphQLSchemaGenerator |
withOperationsFromSingleton(Object serviceSingleton,
Type beanType,
ResolverBuilder... builders)
Same as
withOperationsFromSingleton(Object, Type) except that custom ResolverBuilders will be used
to look through beanType for methods to be exposed. |
GraphQLSchemaGenerator |
withOperationsFromSingletons(Object... serviceSingletons)
Same as
withOperationsFromSingleton(Object) except that multiple beans can be registered at the same time. |
GraphQLSchemaGenerator |
withOperationsFromType(AnnotatedType serviceType) |
GraphQLSchemaGenerator |
withOperationsFromType(AnnotatedType serviceType,
ResolverBuilder... builders) |
GraphQLSchemaGenerator |
withOperationsFromType(Type serviceType) |
GraphQLSchemaGenerator |
withOperationsFromType(Type serviceType,
ResolverBuilder... builders) |
GraphQLSchemaGenerator |
withOperationsFromTypes(AnnotatedType... serviceType) |
GraphQLSchemaGenerator |
withOperationsFromTypes(Type... serviceType) |
GraphQLSchemaGenerator |
withOutputConverters(OutputConverter<?,?>... outputConverters)
Registers custom
OutputConverters to be used for converting values returned by the exposed Java method
into those expected by the GraphQL client. |
GraphQLSchemaGenerator |
withRelayCompliantMutations()
Sets a flag that all mutations should be mapped in a Relay-compliant way,
using the default name and description for output wrapper fields.
|
GraphQLSchemaGenerator |
withRelayCompliantMutations(String wrapperFieldName,
String wrapperFieldDescription)
Sets a flag that all mutations should be mapped in a Relay-compliant way,
using the default name and description for output wrapper fields.
|
GraphQLSchemaGenerator |
withResolverBuilders(ResolverBuilder... resolverBuilders)
Globally registers
ResolverBuilders to be used for sources that don't have explicitly assigned builders. |
GraphQLSchemaGenerator |
withScalarMappingStrategy(ScalarMappingStrategy scalarStrategy) |
GraphQLSchemaGenerator |
withSchemaProcessors(GraphQLSchemaProcessor... processors)
Registers custom schema processors that can perform arbitrary transformations on the schema just before it is built.
|
GraphQLSchemaGenerator |
withTypeAdapters(AbstractTypeAdapter<?,?>... typeAdapters)
Type adapters (instances of
AbstractTypeAdapter) are both type mappers and bi-directional converters,
implementing TypeMapper, InputConverter and OutputConverter. |
GraphQLSchemaGenerator |
withTypeMappers(TypeMapper... typeMappers)
Registers custom
TypeMappers to be used for mapping Java type to GraphQL types. |
GraphQLSchemaGenerator |
withValueMapperFactory(ValueMapperFactory valueMapperFactory) |
public GraphQLSchemaGenerator()
public GraphQLSchemaGenerator(Object... serviceSingletons)
serviceSingletons as singleton query sources with default builders
Equivalent to: new GraphQLSchemaGenerator().withOperationsFromType(serviceSingletons)
serviceSingletons - Singletons to register as query sourcespublic 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 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 withInterfaceMappingStrategy(InterfaceMappingStrategy interfaceStrategy)
public GraphQLSchemaGenerator withScalarMappingStrategy(ScalarMappingStrategy scalarStrategy)
public GraphQLSchemaGenerator withBasePackage(String basePackage)
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 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.
See InputConverter.supports(AnnotatedType)
See withDefaults()
inputConverters - Custom input converters to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic 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.
See OutputConverter.supports(AnnotatedType)
See withDefaults()
outputConverters - Custom output converters to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic 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 withDefaults()
typeAdapters - Custom type adapters to register with the builderGraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withArgumentInjectors(ArgumentInjector... argumentInjectors)
public GraphQLSchemaGenerator withMetaDataGenerator(TypeInfoGenerator typeInfoGenerator)
public GraphQLSchemaGenerator withValueMapperFactory(ValueMapperFactory valueMapperFactory)
public GraphQLSchemaGenerator withInputFieldDiscoveryStrategy(InputFieldDiscoveryStrategy inputFieldStrategy)
public GraphQLSchemaGenerator withAdditionalTypes(Collection<graphql.schema.GraphQLType> additionalTypes)
public GraphQLSchemaGenerator withRelayCompliantMutations()
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withRelayCompliantMutations(String wrapperFieldName, String wrapperFieldDescription)
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 GraphQLSchemaGenerator withDefaults()
TypeMappers, InputConverters and OutputConverters
Equivalent to calling withDefaultResolverBuilders().withDefaultMappers().withDefaultConverters()
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withDefaultMappers()
TypeMappers
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withDefaultInputConverters()
public GraphQLSchemaGenerator withDefaultOutputConverters()
public GraphQLSchemaGenerator withDefaultConverters()
InputConverters and OutputConverters.
The equivalent of calling both withDefaultInputConverters() and withDefaultOutputConverters().
See withInputConverters(InputConverter[]) and withOutputConverters(OutputConverter[]) )}
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withDefaultArgumentInjectors()
public GraphQLSchemaGenerator withDefaultResolverBuilders()
AnnotatedResolverBuilder.
GraphQLSchemaGenerator instance, to allow method chainingpublic GraphQLSchemaGenerator withDefaultNestedResolverBuilders()
AnnotatedResolverBuilder.
GraphQLSchemaGenerator instance, to allow method chainingpublic graphql.schema.GraphQLSchema generate()
GraphQL instances. See the example in the description of this class.Copyright © 2016–2017. All rights reserved.