2017-07-17 16 views
1

I以下のエラーのために入力オブジェクトをDTOにキャストできませんExecutionStrategy.resolveField() - データのフェッチ中に例外が発生しましたjava.lang.ClassCastException:java。 util.LinkedHashMapはcom.fathome.graphql.OffersDと互換性がありませんenvironment.getArgumentはgraphql-java内の自分自身の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 = 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 sが完全に透明に上記の必要のようなので、何の手動手順をしません。

関連する問題