@OBJECT Directive

The @object directive can be used to explicitly shape the nested object as an object.

The directive @object takes no arguments.

SELECT JSON_SERIALIZE(data PRETTY) AS data FROM GRAPHQL('
    query {
        driver {
            driverId: driver_id
            driverName: name
            driverPoints: points
            teamDetails: team @object {
                teamId: team_id
                teamName: name
                teamPoints: points
            }
        }
    }
');

The preceding example explicitly defines that the field teamDetails must an object with subfields: teamID, teamName and teamPoints in the output:

DATA
--------------------------------------------------------------------------------
{
  "driverId" : 101,
  "driverName" : "Lando Norris",
  "driverPoints" : 282,
  "teamDetails" :
  {
    "teamId" : 301,
    "teamName" : "McLaren Mercedes",
    "teamPoints" : 666
  }
}

{
  "driverId" : 102,
  "driverName" : "Oscar Piastri",
  "driverPoints" : 384,
  "teamDetails" :
  {
    "teamId" : 301,
    "teamName" : "McLaren Mercedes",
    "teamPoints" : 666
  }
}
....................
....................
20 rows selected.

When you query with the GRAPHQL() table function, you can use the limit argument with the @object directive to limit the number of objects in the output:

SELECT JSON_SERIALIZE(data PRETTY) AS data FROM GRAPHQL('
    query {
        team (limit: 5) {
            teamId: team_id
            teamName: name
            teamPoints: points
            driver (limit: 1) @object {
                driverId: driver_id
                driverName: name
                driverPoints: points
            }
        }
    }
');

The limit argument in the preceding example will only produce 5 rows since the team object is limited to 5:

DATA
--------------------------------------------------------------------------------
{
  "teamId" : 301,
  "teamName" : "McLaren Mercedes",
  "teamPoints" : 666,
  "driver" :
  {
    "driverId" : 101,
    "driverName" : "Lando Norris",
    "driverPoints" : 282
  }
}

{
  "teamId" : 302,
  "teamName" : "Ferrari",
  "teamPoints" : 652,
  "driver" :
  {
    "driverId" : 103,
    "driverName" : "Charles Leclerc",
    "driverPoints" : 312
  }
}
...............................
...............................
5 rows selected.