schemagen-graphql icon indicating copy to clipboard operation
schemagen-graphql copied to clipboard

GraphQLObjectMapper is throwing exception when building Classes Hierarchy

Open alissongodoi opened this issue 7 years ago • 1 comments

log.txt GraphQLObjectMapper is throwing exception in the typeArguments stack. It follows attached the log.

at java.util.Stack.peek(Unknown Source) ~[na:1.8.0_121]
at com.bretpatterson.schemagen.graphql.impl.GraphQLObjectMapper.getClassFromType(GraphQLObjectMapper.java:742) ~[schemagen-graphql-1.0.2.jar:na]

Basically I have the following classes:

public abstract class BaseEntity<ID> implements IdentifiedObject<ID>, VersionedEntity {
   @Id
   private ID id;

   public ID getId() {
       return id;
   }
   public void setId(ID id) {
       this.id=id;
   }	
}
public class Person extends BaseEntity<Long> implements IdentifiedObject<Long> {
    // Person details
}

It fails in the build:

@GraphQLController
public class KeyValueStoreController {
	  @GraphQLIgnore
	  private Map<String,Person> dataStore = new HashMap<String,Person>();
	  
	  public KeyValueStoreController() {
		  Person c = new Person();
		  c.setFirstName("Alisson");
		  c.setLastName("Godoi");
		  c.setId(1L);
		  dataStore.put("1", c);
	  }
	  
	  @GraphQLQuery(name="get")
	  public Person getValue(@GraphQLParam(name="key") String key) {
	     return dataStore.get(key);
	  }
	  
	  @GraphQLMutation(name="put")
	  public Customer setValue(String key, Customer value) {
	  	dataStore.put(key,value);
	  	return value;
	  }
}

KeyValueStoreController key = new KeyValueStoreController();
ObjectMapper objectMapper = new ObjectMapper();
GraphQLSchema schema = GraphQLSchemaBuilder.newBuilder()
    .registerTypeFactory(new JacksonTypeFactory(objectMapper))
    .registerGraphQLControllerObjects(ImmutableList.<Object>of(key))
    .build();

typeArguments variable is empty. It is trying to fetch variable type ID for the method BaseEntity.getId() getClassFromType(typeArguments.peek().get(((TypeVariable<?>) type).getName()));

alissongodoi avatar May 08 '17 06:05 alissongodoi

Hi @alissongodoi

I have created a fix for this issue in PR #17.

The cause was the library not handling the case Person extends Entity<ID> correctly.

I have added some code using getSuperclass() to cater for this Scenario.

If the meantime you can create a custom type mapper to workaround the problem (not a real solution I know).

Ciaran

cliedeman avatar May 14 '17 21:05 cliedeman