2017-07-17 35 views
1

由于以下错误,无法将输入对象转换为DTO ExecutionStrategy.resolveField() - 获取数据时发生异常java.lang.ClassCastException:java。 util.LinkedHashMap与com.fathome.graphql.OffersDto不兼容environment.getArgument无法转换到我自己的java对象在graphql-java

OffersDto inputObject = environment.getArgument(“offersInput”);

请让我知道下面的代码有什么问题,提前致谢。

package com.fathome.graphql; 

import graphql.schema.*; 

import static graphql.Scalars.*; 
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; 
import static graphql.schema.GraphQLInputObjectField.newInputObjectField; 
import static graphql.schema.GraphQLInputObjectType.newInputObject; 
import static graphql.schema.GraphQLList.list; 
import static graphql.schema.GraphQLObjectType.newObject; 


public class ManualGraphQLQuerySchema { 


    public static GraphQLObjectType offersResponse = newObject() 
      .name("OffersResponse") 
      .field(newFieldDefinition() 
        .name("offerName") 
        .type(GraphQLString)) 
      .field(newFieldDefinition() 
        .name("offerId") 
        .type(GraphQLString)) 
      .build(); 

    public static GraphQLInputObjectType offersRequestType = GraphQLInputObjectType.newInputObject() 
      .name("OffersDto") 
      .field(newInputObjectField() 
        .name("offerName") 
        .type(GraphQLString)) 
      .field(newInputObjectField() 
        .name("offerId") 
        .type(GraphQLString)) 
      .build(); 

    public static GraphQLObjectType queryType = newObject() 
      .name("QueryType") 
      .field(newFieldDefinition() 
       .name("offers") 
       .type(offersResponse) 
       .argument(GraphQLArgument.newArgument() 
        .name("offersInput") 
        .type(offersRequestType)) 
       .dataFetcher(new OffersFetcher())) 
      .build(); 

} 



package com.fathome.graphql; 

import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder({ 
     "offerName", 
     "offerId" 

}) 
public class OffersDto { 

    @JsonProperty("offerName") 
    private String offerName; 

    @JsonProperty("offerName") 
    public String getOfferName() { 
     return offerName; 
    } 

    @JsonProperty("offerName") 
    public void setOfferName(String offerName) { 
     this.offerName = offerName; 
    } 

    @JsonProperty("offerId") 
    private String offerId; 

    @JsonProperty("offerId") 
    public String getOfferId() { 
     return offerId; 
    } 

    @JsonProperty("offerId") 
    public void setOfferId(String offerId) { 
     this.offerId = offerId; 
    } 
} 




package com.fathome.graphql; 

import graphql.schema.DataFetcher; 
import graphql.schema.DataFetchingEnvironment; 

public class OffersFetcher implements DataFetcher<OffersDto> { 

    @Override 
    public OffersDto get(DataFetchingEnvironment environment) { 

     //Can't cast input object DTO this is error in below line 
     //ExecutionStrategy.resolveField() - Exception while fetching data 
     //java.lang.ClassCastException: java.util.LinkedHashMap incompatible with com.fathome.graphql.OffersDto 
     OffersDto inputObject = environment.getArgument("offersInput"); 
     //calling service to get offerdetails using inputObject 
     //for testing not calling service just returning mock object. 
     OffersDto offersDto = new OffersDto(); 
     offersDto.setOfferName("123"); 
     offersDto.setOfferId("456"); 

     return offersDto; 

    } 
} 

在下面的引用链接类似于我的代码工作正常。

Episode episode = environment.getArgument(“episode”); ReviewInput review = environment.getArgument(“review”);

http://graphql-java.readthedocs.io/en/latest/execution.html

回答

2

environment.getArgument(...)得到的值要么是标值(字符串,数字等),或在对象GraphQL输入型的情况下(在你的情况等)一Map
然后你需要自己去执行反序列化。由于您使用的杰克逊,它应该是这样的:

private final ObjectMapper objectMapper; 
... 
Object rawInput = environment.getArgument("offersInput"); 
OffersDto inputObject = objectMapper.convertValue(rawInput, OffersDto.class); 

退房graphql-java-tools的架构优先的方法,或graphql-spqr代码为先,既能使DataFetcher小号完全透明的,所以像上面无需手动步骤。