What is the difference between GraphQLJSON and GraphQLJSONObject exported by graphql-type-json?

2.4k Views Asked by At

From the official package documentation:

GraphQLJSON can represent any JSON-serializable value, including scalars, arrays, and objects. GraphQLJSONObject represents specifically JSON objects, which covers many practical use cases for JSON scalars.

It sounds a bit confusing as to me both definitions seem quite similar. Can someone please help me understand this better with an example? Thanks in anticipation.

https://www.npmjs.com/package/graphql-type-json

2

There are 2 best solutions below

0
Алексей Мартинкевич On BEST ANSWER

JSON value can be of type string, number, boolean, object, array or null. So GraphQLJSON represents any of this, while GraphQLJSONObject represents JSON objects exclusively.

0
Splendor On

The wording can be confusing. There is a discussion here but I think a couple examples make it easier to understand.

GraphQLJSON will accept anything that you could assign as the value side of a key value pair in valid JSON.

// Each line represents valid GraphQLJSON
3
"3"
"foo"
{"foo": 3}
{"foo": ["bar", "3"]}

// Invalid GraphQLJSON might be something like this
foo

And GraphQLJSONObject is more strict. It only includes the object subset of values contained in GraphQLJSON (so no raw strings, numbers, etc.)

// Each line represents valid GraphQLJSONObject
{"foo": 3}
{"foo": ["bar", "3"]}

// Each line represents invalid GraphQLJSONObject
3
"3"
"foo"