Scalar Type Json is not working when we are using type "Input" for Mutation

209 Views Asked by At

Scenario: When I have a scenario where I need to send one attribute with key value pair data to a graphql server, I use the Scalar type as Json in the setup and declare the same thing in the graphql schema. However, during testing, I discovered that the map value is correct until the ObjectScalar.class properly and that the value is being put in the LinkedHashMap, but when the request reaches the controller, it becomes empty.

Note: When there is read operation from graphql Server then it works fine

To Reproduce

  • Spring Boot Version : 2.7.5
  • GraphQL : spring-boot-starter-graphql
  • GraphQL Scalar Extender Version : graphql-java-extended-scalars-20.02

GraphqlSchema

scalar JSON 
input RuleInput { 
   ruleName: String 
   ruleConfiguration: JSON 
}
 
type Mutation { 
   createRule(ruleInput: RuleInput): JSON 
}

Java DTO Class

import java.util.Map; 
import lombok.AllArgsConstructor; 
import lombok.Data; 
import lombok.NoArgsConstructor; 
@Data 
@NoArgsConstructor 
@AllArgsConstructor 
public class RuleInput { 
  private String ruleName; 
  private Map<String, Object> ruleConfiguration; 
}

GraphQL Configuration

import graphql.scalars.ExtendedScalars; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.graphql.execution.RuntimeWiringConfigurer; 
@Configuration 
public class GraphQLConfiguration { 
  @Bean 
  protected RuntimeWiringConfigurer runtimeWiringConfigurer() { 
    return builder -> builder .scalar(ExtendedScalars.Json); 
  } 
}

Controller

@Slf4J 
@Controller 
public class RuleController { 
  @MutationMapping 
  public Mono<Long> createRule(@Argument(name = "ruleInput") RuleInput ruleInput) { 
    log.info("Input :{}", ruleInput); 
    // except the Map value, rest all values are coming correctly. 
    return Mono.empty(); 
  } 
}

Any help would be much appreciated.

Please find the below links for the sample project

https://github.com/GRajaMca/graphql-sample

Mutation Query used during testing

mutation{
  createRule(ruleInput:{ruleName:"Test",ruleConfiguration:{Config1:"Value1",Config2:"Value2"}})
}
0

There are 0 best solutions below