I am working on an application with graphQL and spring boot framework. Here I am using GraphQL's Schema-first approach to write my schema details. In my scenario, I need to assign JSONObject and JSONArray as input to few of the fields in my Schema file. In graphQL schema file, we can not directly set JSONObject/JSONArray as data types. Can someone please guide me on how to handle JSONObject/JSONArray in graphQL?
Asked
Active
Viewed 5,717 times
1 Answers
3
You can define your own scalar type JSON:
scalar JSON
type Foo {
field: JSON
}
Then implement a new java GraphQLScalarType bean:
@Component
public class JSONScalarType extends GraphQLScalarType {
JSONScalarType() {
super("JSON", "JSON value", new Coercing<Object,Object>() {
...
});
}
}
Finally implement the scalar Coercing. You can find a tutorial on scalar types here https://www.graphql-java.com/documentation/v15/scalars/
Or, as in graphql a JSON is an object, you can have a look at how the graphql-java-extended-scalars implemented the Object and JSON scalars : https://github.com/graphql-java/graphql-java-extended-scalars/blob/master/src/main/java/graphql/scalars/object/ObjectScalar.java
Another solution would be to use the graphql-java-extended-scalars library: https://github.com/graphql-java/graphql-java-extended-scalars

AllirionX
- 1,073
- 6
- 13
-
Thank you Allirion..! You saved my time, It works.! – swanthri Aug 26 '20 at 06:08
-
@DingThree you're welcome! Could you let me know which solution you used (custom scalar or use the extended-scalar library)? – AllirionX Aug 27 '20 at 10:59
-
Hello AllirionX - I have created a custom scalar and mapped it with my schema file. And, it worked perfectly.! Thank you.! – swanthri Aug 28 '20 at 07:44
-
1Has someone got the complete implementation of the call, sharing will be helpful? – Ameya Jun 03 '21 at 10:28