Quickstart: Azure Cosmos DB for Apache Gremlin library for Node.js

APPLIES TO: Gremlin

Azure Cosmos DB for Apache Gremlin is a fully managed graph database service implementing the popular Apache Tinkerpop, a graph computing framework using the Gremlin query language. The API for Gremlin gives you a low-friction way to get started using Gremlin with a service that can grow and scale out as much as you need with minimal management.

In this quickstart, you use the gremlin library to connect to a newly created Azure Cosmos DB for Gremlin account.

Library source code | Package (npm)

Prerequisites

Setting up

This section walks you through creating an API for Gremlin account and setting up a Node.js project to use the library to connect to the account.

Create an API for Gremlin account

The API for Gremlin account should be created prior to using the Node.js library. Additionally, it helps to also have the database and graph in place.

  1. Create shell variables for accountName, resourceGroupName, and location.

    # Variable for resource group name
    resourceGroupName="msdocs-cosmos-gremlin-quickstart"
    location="westus"
    
    # Variable for account name with a randomly generated suffix
    
    let suffix=$RANDOM*$RANDOM
    accountName="msdocs-gremlin-$suffix"
    
  2. If you haven't already, sign in to the Azure CLI using az login.

  3. Use az group create to create a new resource group in your subscription.

    az group create \
        --name $resourceGroupName \
        --location $location
    
  4. Use az cosmosdb create to create a new API for Gremlin account with default settings.

    az cosmosdb create \
        --resource-group $resourceGroupName \
        --name $accountName \
        --capabilities "EnableGremlin" \
        --locations regionName=$location \
        --enable-free-tier true
    
  5. Get the API for Gremlin endpoint NAME for the account using az cosmosdb show.

    az cosmosdb show \
        --resource-group $resourceGroupName \
        --name $accountName \
        --query "name"
    
  6. Find the KEY from the list of keys for the account with az-cosmosdb-keys-list.

    az cosmosdb keys list \
        --resource-group $resourceGroupName \
        --name $accountName \
        --type "keys" \
        --query "primaryMasterKey"
    
  7. Record the NAME and KEY values. You use these credentials later.

  8. Create a database named cosmicworks using az cosmosdb gremlin database create.

    az cosmosdb gremlin database create \
        --resource-group $resourceGroupName \
        --account-name $accountName \
        --name "cosmicworks"
    
  9. Create a graph using az cosmosdb gremlin graph create. Name the graph products, then set the throughput to 400, and finally set the partition key path to /category.

    az cosmosdb gremlin graph create \
        --resource-group $resourceGroupName \
        --account-name $accountName \
        --database-name "cosmicworks" \
        --name "products" \
        --partition-key-path "/category" \
        --throughput 400
    

Create a new Node.js console application

Create a Node.js console application in an empty folder using your preferred terminal.

  1. Open your terminal in an empty folder.

  2. Initialize a new module

    npm init es6 --yes
    
  3. Create the app.js file

    touch app.js
    

Install the npm package

Add the gremlin npm package to the Node.js project.

  1. Open the package.json file and replace the contents with this JSON configuration.

    {
      "main": "app.js",
      "type": "module",
      "scripts": {
        "start": "node app.js"
      },
      "dependencies": {
        "gremlin": "^3.*"
      }
    }
    
  2. Use the npm install command to install all packages specified in the package.json file.

    npm install
    

Configure environment variables

To use the NAME and URI values obtained earlier in this quickstart, persist them to new environment variables on the local machine running the application.

  1. To set the environment variable, use your terminal to persist the values as COSMOS_ENDPOINT and COSMOS_KEY respectively.

    export COSMOS_GREMLIN_ENDPOINT="<account-name>"
    export COSMOS_GREMLIN_KEY="<account-key>"
    
  2. Validate that the environment variables were set correctly.

    printenv COSMOS_GREMLIN_ENDPOINT
    printenv COSMOS_GREMLIN_KEY
    

Code examples

The code in this article connects to a database named cosmicworks and a graph named products. The code then adds vertices and edges to the graph before traversing the added items.

Authenticate the client

Application requests to most Azure services must be authorized. For the API for Gremlin, use the NAME and URI values obtained earlier in this quickstart.

  1. Open the app.js file.

  2. Import the gremlin module.

// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. Create accountName and accountKey variables. Store the COSMOS_GREMLIN_ENDPOINT and COSMOS_GREMLIN_KEY environment variables as the values for each respective variable.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. Use PlainTextSaslAuthenticator to create a new object for the account's credentials.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. Use Client to connect using the remote server credentials and the GraphSON 2.0 serializer. Then, use Open to create a new connection to the server.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

Create vertices

Now that the application is connected to the account, use the standard Gremlin syntax to create vertices.

  1. Use submit to run a command server-side on the API for Gremlin account. Create a product vertex with the following properties:

    Value
    label product
    id 68719518371
    name Kiama classic surfboard
    price 285.55
    category surfboards
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. Create a second product vertex with these properties:

    Value
    label product
    id 68719518403
    name Montau Turtle Surfboard
    price 600.00
    category surfboards
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. Create a third product vertex with these properties:

    Value
    label product
    id 68719518409
    name Bondi Twin Surfboard
    price 585.50
    category surfboards
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

Create edges

Create edges using the Gremlin syntax to define relationships between vertices.

  1. Create an edge from the Montau Turtle Surfboard product named replaces to the Kiama classic surfboard product.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

Tip

This edge defintion uses the g.V(['<partition-key>', '<id>']) syntax. Alternatively, you can use g.V('<id>').has('category', '<partition-key>').

  1. Create another replaces edge from the same product to the Bondi Twin Surfboard.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

Query vertices & edges

Use the Gremlin syntax to traverse the graph and discover relationships between vertices.

  1. Traverse the graph and find all vertices that Montau Turtle Surfboard replaces.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>
  1. Write to the console the result of this traversal.
// <imports>
import gremlin from 'gremlin'
// </imports>

// <environment_variables>
const accountName = process.env.COSMOS_GREMLIN_ENDPOINT
const accountKey = process.env.COSMOS_GREMLIN_KEY
// </environment_variables>

// <authenticate_client>
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
  '/dbs/cosmicworks/colls/products',
  `${accountKey}`
)
// </authenticate_client>

// <connect_client>
const client = new gremlin.driver.Client(
  `wss://${accountName}.gremlin.cosmos.azure.cn:443/`,
  {
    credentials,
    traversalsource: 'g',
    rejectUnauthorized: true,
    mimeType: 'application/vnd.gremlin-v2.0+json'
  }
)

client.open()
// </connect_client>

// <drop_graph>
await client.submit('g.V().drop()')
// </drop_graph>

// <create_vertices_1>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518371',
    prop_name: 'Kiama classic surfboard',
    prop_price: 285.55,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_1>

// <create_vertices_2>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518403',
    prop_name: 'Montau Turtle Surfboard',
    prop_price: 600.00,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_2>

// <create_vertices_3>
await client.submit(
  'g.addV(\'product\').property(\'id\', prop_id).property(\'name\', prop_name).property(\'price\', prop_price).property(\'category\', prop_partition_key)', {
    prop_id: '68719518409',
    prop_name: 'Bondi Twin Surfboard',
    prop_price: 585.50,
    prop_partition_key: 'surfboards'
  }
)
// </create_vertices_3>

// <create_edges_1>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518371'
  }
)
// </create_edges_1>

// <create_edges_2>
await client.submit(
  'g.V([prop_partition_key, prop_source_id]).addE(\'replaces\').to(g.V([prop_partition_key, prop_target_id]))', {
    prop_partition_key: 'surfboards',
    prop_source_id: '68719518403',
    prop_target_id: '68719518409'
  }
)
// </create_edges_2>

// <query_vertices_edges>
const result = await client.submit(
  'g.V().hasLabel(\'product\').has(\'category\', prop_partition_key).has(\'name\', prop_name).outE(\'replaces\').inV()', {
    prop_partition_key: 'surfboards',
    prop_name: 'Montau Turtle Surfboard'
  }
)
// </query_vertices_edges>

// <output_vertices_edges>
console.dir(result)
// </output_vertices_edges>

Run the code

Validate that your application works as expected by running the application. The application should execute with no errors or warnings. The output of the application includes data about the created and queried items.

  1. Open the terminal in the Node.js project folder.

  2. Use npm <script> to run the application. Observe the output from the application.

    npm start
    

Clean up resources

When you no longer need the API for Gremlin account, delete the corresponding resource group.

  1. Create a shell variable for resourceGroupName if it doesn't already exist.

    # Variable for resource group name
    resourceGroupName="msdocs-cosmos-gremlin-quickstart"
    
  2. Use az group delete to delete the resource group.

    az group delete \
        --name $resourceGroupName
    

Next step