Skip to main content

Query data from a data model (3 mins)

This article describes how to access the data in your data model via the query explorer and how to use the query builder to create GraphQL queries.

tip

For more information about GraphQL, see Learn GraphQL.

Prerequisites

Before you can query the data, make sure you've completed the steps in these articles:

  1. Upload demo data.
  2. Create a data model.
  3. Populate the data model.

Querying data in the query explorer

  1. Navigate to the CDF portal application > Explore and build > Data modeling.

  2. Select the data model you want to query, for example, the MovieDM data model.

  3. Select Query explorer > Show GraphiQL Explorer to query data from the data model using the query builder.

    Select Query explorer > Show GraphiQL Explorer

There are two main options to find data with the query explorer:

  • List data, optionally with filters and sorting.
  • Search data, also with filters and sorting.

The next sections are examples based on the data model you created earlier.

List movies

  1. Select listMovie from the query explorer to start a query to list movies.

  2. In GraphQL, you specify which fields you want to return. In this example, we'll list the movie titles and their release year. In the query, expand items and select name and releasedYear.

  3. To run the query, select the Play button or press Ctrl+Enter.

    The final query should look similar to this:

    query Movies {
    listMovie(first: 10) {
    items {
    name
    releasedYear
    }
    }
    }

Filter movies on release year

  1. To find movies released before 2001, enter this query:

    query OldMovies {
    listMovie(first: 10, filter: { releasedYear: { lt: 2001 } }) {
    items {
    name
    releasedYear
    imdbRating
    }
    }
    }

Filter top-rated movies on release year (combining multiple filters)

  1. To find the top rated movies released before year 2001, enter this query:

    query GoodOldMovies {
    listMovie(
    first: 10
    filter: {
    and: [{ releasedYear: { lt: 2001 } }, { imdbRating: { gt: 7.8 } }]
    }
    ) {
    items {
    name
    releasedYear
    imdbRating
    }
    }
    }

Include the actors in the query

  1. To include the actors we created as relationships (edges), enter this query:

    query MoviesAndActors {
    listMovie(first: 10) {
    items {
    name
    releasedYear
    imdbRating
    actors {
    items {
    name
    }
    }
    }
    }
    }

Search movies

  1. To find data with search, use a query similar to this:

    query SearchMovie {
    searchMovie(fields: description, query: "love") {
    items {
    name
    description
    actors {
    items {
    name
    }
    }
    }
    }
    }

Access the data via a URL

You can use any application to directly access the data in the data model if you have an authenticated token to pass in the Authentication field in the header.

For instance, you can call this URL: https://<cluster>.cognitedata.com/api/v1/projects/<project>/userapis/spaces/<space>/datamodels/<data-model-external-id>/versions/<version>/graphql

Where:

  • cluster - The CDF cluster, for example, api.
  • project - The CDF project, for example, demo.
  • data model external id - The external ID of your data model, for example, MovieDM.
  • space - The space where your data model lives, for example, MovieDM.
  • version - The data model version you want to use, for example, 1.

An example for a full URL is https://api.cognitedata.com/api/v1/projects/demo/userapis/spaces/MovieDM/datamodels/MovieDM/versions/1/graphql

Access data with the JavaScript SDK

To get started with the JavaScript SDK, see the JavaScript Getting started guide.

When you have authenticated the SDK, you can run the following code with the same URL as described above:

// Authenticate the `SDK` CogniteClient instance

const PROJECT = `publicdata`;
const EXTERNAL_ID = `MovieDM`;
const SPACE = `MovieDM`;
const VERSION = `1`;

const query = {
query: `
query MoviesAndActors {
listMovie {
items {
actors {
items {
age
name
}
}
name
watchedIt
}
}
}
`,
},

sdk
.post(
`/api/v1/projects/${PROJECT}/userapis/spaces/${SPACE}/datamodels/${EXTERNAL_ID}/versions/${VERSION}/graphql`,
{
data: query
}
)
.then((data) => {
// ... do something with the `data`
});

Access data with the Python SDK

To get started with the Python SDK, see the Python Getting started guide.

When you have authenticated the SDK, you can run the following code with the same URL as described above:

# Authenticate the `SDK` CogniteClient instance

PROJECT = "xxx"
EXTERNAL_ID = "MovieDM"
SPACE = "MovieDM"
VERSION = "1"
query = {
"query": """
query MoviesAndActors {
listMovie {
items {
actors {
items {
age
name
}
}
name
watchedIt
}
}
}
"""
}
response = client.post(f"/api/v1/projects/{PROJECT}/userapis/spaces/{SPACE}/datamodels/{EXTERNAL_ID}/versions/{VERSION}/graphql", json=query)

# ... do something with the `response.content`