[minor] code restructured
@@ -69,28 +69,7 @@ public class GraphQLProvider {
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl);
// Add of the CharacterImpl type definition
- ObjectTypeDefinition humanDef = (ObjectTypeDefinition) typeRegistry.getType("Human").get();
- ObjectTypeDefinition.Builder characterImplDef = ObjectTypeDefinition.newObjectTypeDefinition();
- characterImplDef.name("CharacterImpl");
- for (FieldDefinition fieldDef : humanDef.getFieldDefinitions()) {
- // Let's add only the fields coming from the Character interface
- switch (fieldDef.getName()) {
- case "id":
- case "name":
- case "friends":
- case "appearsIn":
- characterImplDef.fieldDefinition(fieldDef);
- }
- }
- for (Type<?> typeName : humanDef.getImplements()) {
- // Let's add only the fields coming from the Character interface
- switch (((TypeName) typeName).getName()) {
- case "Character":
- characterImplDef.implementz(typeName);
- }
-
- }
- typeRegistry.add(characterImplDef.build());
+ typeRegistry.add(getCharacterImplType(typeRegistry));
RuntimeWiring runtimeWiring = buildWiring();
SchemaGenerator schemaGenerator = new SchemaGenerator();
@@ -104,25 +83,54 @@ public class GraphQLProvider {
// Also see sample :
// https://github.com/graphql-java/graphql-java-examples/tree/master/http-example
return RuntimeWiring.newRuntimeWiring()
+ // The Data Fetchers for queries must be defined. They'll trigger the JPA access to the database
.type(newTypeWiring("QueryType").dataFetcher("hero", graphQLDataFetchers.hero()))
.type(newTypeWiring("QueryType").dataFetcher("characters", graphQLDataFetchers.characters()))
.type(newTypeWiring("QueryType").dataFetcher("human", graphQLDataFetchers.human()))
.type(newTypeWiring("QueryType").dataFetcher("droid", graphQLDataFetchers.droid()))
+ //
+ // Defining the Data Fetchers for the fields are useless: these field will be populated by JPA
// .type(newTypeWiring("Character").dataFetcher("friends", graphQLDataFetchers.friends()))
// .type(newTypeWiring("Droid").dataFetcher("friends", graphQLDataFetchers.friends()))
// .type(newTypeWiring("Human").dataFetcher("friends", graphQLDataFetchers.friends()))
+ //
+ // We still need to link the interface types to the concrete types
.type("Character", typeWriting -> typeWriting.typeResolver(getCharacterResolver()))
//
.build();
}
+ private ObjectTypeDefinition getCharacterImplType(TypeDefinitionRegistry typeRegistry) {
+ ObjectTypeDefinition humanDef = (ObjectTypeDefinition) typeRegistry.getType("Human").get();
+ ObjectTypeDefinition.Builder characterImplDef = ObjectTypeDefinition.newObjectTypeDefinition();
+ characterImplDef.name("CharacterImpl");
+ for (FieldDefinition fieldDef : humanDef.getFieldDefinitions()) {
+ // Let's add only the fields coming from the Character interface
+ switch (fieldDef.getName()) {
+ case "id":
+ case "name":
+ case "friends":
+ case "appearsIn":
+ characterImplDef.fieldDefinition(fieldDef);
+ }
+ }
+ for (Type<?> typeName : humanDef.getImplements()) {
+ // Let's add only the fields coming from the Character interface
+ switch (((TypeName) typeName).getName()) {
+ case "Character":
+ characterImplDef.implementz(typeName);
+ }
+
+ }
+ return characterImplDef.build();
+ }
+
private TypeResolver getCharacterResolver() {
return new TypeResolver() {
@Override
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
Object javaObject = env.getObject();
String ret = null;
- logger.debug("Resolving javaObject {}", javaObject.getClass().getName());
if (javaObject instanceof Human) {
ret = "Human";
