use of graphql.schema.TypeResolver in project graphql-java by graphql-java.
the class SchemaGenerator method getTypeResolverForUnion.
@SuppressWarnings("Duplicates")
private TypeResolver getTypeResolverForUnion(BuildContext buildCtx, UnionTypeDefinition unionType) {
TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
RuntimeWiring wiring = buildCtx.getWiring();
WiringFactory wiringFactory = wiring.getWiringFactory();
TypeResolver typeResolver;
UnionWiringEnvironment environment = new UnionWiringEnvironment(typeRegistry, unionType);
if (wiringFactory.providesTypeResolver(environment)) {
typeResolver = wiringFactory.getTypeResolver(environment);
assertNotNull(typeResolver, "The WiringFactory indicated it union provides a type resolver but then returned null");
} else {
typeResolver = wiring.getTypeResolvers().get(unionType.getName());
if (typeResolver == null) {
// this really should be checked earlier via a pre-flight check
typeResolver = new TypeResolverProxy();
}
}
return typeResolver;
}
use of graphql.schema.TypeResolver in project graphql-java by graphql-java.
the class SchemaGenerator method getTypeResolverForInterface.
@SuppressWarnings("Duplicates")
private TypeResolver getTypeResolverForInterface(BuildContext buildCtx, InterfaceTypeDefinition interfaceType) {
TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry();
RuntimeWiring wiring = buildCtx.getWiring();
WiringFactory wiringFactory = wiring.getWiringFactory();
TypeResolver typeResolver;
InterfaceWiringEnvironment environment = new InterfaceWiringEnvironment(typeRegistry, interfaceType);
if (wiringFactory.providesTypeResolver(environment)) {
typeResolver = wiringFactory.getTypeResolver(environment);
assertNotNull(typeResolver, "The WiringFactory indicated it provides a interface type resolver but then returned null");
} else {
typeResolver = wiring.getTypeResolvers().get(interfaceType.getName());
if (typeResolver == null) {
// this really should be checked earlier via a pre-flight check
typeResolver = new TypeResolverProxy();
}
}
return typeResolver;
}
use of graphql.schema.TypeResolver in project graphql-java by graphql-java.
the class HttpMain method buildStarWarsSchema.
private GraphQLSchema buildStarWarsSchema() {
//
if (starWarsSchema == null) {
//
//
// the fetcher of friends uses java-dataloader to make the circular friends fetching
// more efficient by batching and caching the calls to load Character friends
//
DataFetcher friendsFetcher = environment -> {
DataLoaderRegistry dataloaderRegistry = asMapGet(environment.getContext(), "dataloaderRegistry");
DataLoader friendsDataLoader = dataloaderRegistry.getDataLoader("friends");
List<String> friendIds = asMapGet(environment.getSource(), "friends");
return friendsDataLoader.loadMany(friendIds);
};
//
// reads a file that provides the schema types
//
Reader streamReader = loadSchemaFile("starWarsSchemaAnnotated.graphqls");
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader);
//
// the runtime wiring is used to provide the code that backs the
// logical schema
//
TypeResolver characterTypeResolver = env -> {
Map<String, Object> obj = (Map<String, Object>) env.getObject();
String id = (String) obj.get("id");
GraphQLSchema schema = env.getSchema();
if (StarWarsData.isHuman(id)) {
return (GraphQLObjectType) schema.getType("Human");
} else {
return (GraphQLObjectType) schema.getType("Droid");
}
};
RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring().type(newTypeWiring("Query").dataFetcher("hero", StarWarsData.getHeroDataFetcher()).dataFetcher("human", StarWarsData.getHumanDataFetcher()).dataFetcher("droid", StarWarsData.getDroidDataFetcher())).type(newTypeWiring("Human").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Droid").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Character").typeResolver(characterTypeResolver)).type(newTypeWiring("Episode").enumValues(StarWarsData.getEpisodeResolver())).build();
// finally combine the logical schema with the physical runtime
starWarsSchema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
}
return starWarsSchema;
}
Aggregations