Creating a user-defined type (UDT) in your keyspace using GraphQL

User-defined types (UDTs) are custom data types that a user can create and use in table definitions. They are entirely optional, but sometimes useful if you have data that you would like to group together in a single column. UDTs must be added before you define or add a column using the data type in a table schema.

Prerequisites

  1. Get an application token.

  2. Create a keyspace using the Astra dashboard.

This example creates a UDT called address_type that includes a street, city, state, and zip code.

  • graphQL command

  • cURL command

  • Result

# create a user-defined type (UDT)
mutation createAddressUDT {
  createType(
    keyspaceName: "library"
    typeName: "address_type"
    fields: [
      { name: "street", type: { basic: TEXT } }
      { name: "city", type: { basic: TEXT } }
      { name: "state", type: { basic: TEXT } }
      { name: "zip", type: { basic: TEXT } }
    ]
  )
}
curl -sL --request POST --url http://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/graphql-schema \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation createAddressUDT {\n  createType(\n    keyspaceName: \"library\"\n    typeName: \"address_type\"\n    fields: [\n      { name: \"street\", type: { basic: TEXT } }\n      { name: \"city\", type: { basic: TEXT } }\n      { name: \"state\", type: { basic: TEXT } }\n      { name: \"zip\", type: { basic: TEXT } }\n    ]\n  )\n}","variables":{}}'
{
  "data": {
    "createType": true
  }
}