JSON Custom Scalar

Note This document is a DRAFT. It is being published to solicit feedback. It is a “live document” that can change whenever feedback is given and accepted.

This draft is © Copyright IBM Corp. 2022.

1Goals

This specification defines the custom scalar type JSON that is useful for specifying accepted JSON payloads. This specification is based on this spec, unless otherwise mentioned here.

Note: All mentions of JSON refer to the scalar type defined by this specification. All mentions of JSON refer to a user-declared scalar name.

2Definition

3Example of the JSON Type

The following example uses the JSON type:

Example № 1scalar MyJSON @specifiedBy (url: "https://ibm.github.io/graphql-specs/custom-scalars/json.html")
scalar JSON
scalar UnknownScalar

type Query {  
  takesMyJSON(arg: MyJSON): Int
  takesJSON(arg: JSON): Int
}

schema {
  query: Query
}

In this example, the following points should be noted:

This is a simple example query that matches the schema:

Example № 2query example {
   takesMyJSON (arg: "{\"EV\" : \"Tesla\"}")
}

4Customizing the JSON Type

JSON can be customized using the @scalarParam directive as defined here.

Data types

The following configurable @scalarParam parameters specify the data types that JSON accepts.

When any of these parameters are set to true, data of the specified type passes validation. When set to false, the specified type is invalid. The default value for each of these parameters is true.

JSON components restrictions

The following configurable @scalarParam parameters apply restrictions to the input that the JSON accepts.

5Example of JSON Type Customization

The following example demonstrates how to customize JSON using the @scalarParam directive:

Example № 3scalar ObjectJSON @specifiedBy (url: "https://ibm.github.io/graphql-specs/custom-scalars/json.html")
                  @scalarParam (name : "UniqueNames", value: "10")
                  @scalarParam (name : "DocumentSize", value: "100")
                  @scalarParam (name : "ValueLength", value: "10")
                  @scalarParam (name : "ArrayAllowed", value: "false")
                  @scalarParam (name : "ScalarAllowed", value: "false")
     
scalar ScalarOnlyJSON @specifiedBy (url: "https://ibm.github.io/graphql-specs/custom-scalars/json.html")
                      @scalarParam (name : "ObjectAllowed", value: "false")
                      @scalarParam (name : "ArrayAllowed", value: "false")

scalar ObjectArrayJSON @specifiedBy (url: "https://ibm.github.io/graphql-specs/custom-scalars/json.html")
                       @scalarParam (name : "ScalarAllowed", value: "false")

type Query {  
  takesObjJSON (arg: ObjectJSON): Int
  takesScalarJSON (arg: ScalarOnlyJSON): Int
  takesObjArrJSON (arg: ObjectArrayJSON): Int
}

schema {
  query: Query
}

6Result Coercion

Input

Only a string formatted as a valid JSON is accepted.

Output

The output value is a string formatted as a valid JSON.

7Literal Coercion

Input

Only a string formatted as a valid JSON is accepted.

Output

The output value is a string formatted as a valid JSON.

8Value Coercion

Input

Only a string formatted as a valid JSON is accepted.

Output

The output value is a string formatted as a valid JSON.

  1. 1Goals
  2. 2Definition
  3. 3Example of the JSON Type
  4. 4Customizing the JSON Type
  5. 5Example of JSON Type Customization
  6. 6Result Coercion
  7. 7Literal Coercion
  8. 8Value Coercion