2021-09-22 17:30:15 -07:00
# Querying Metadata Entities
### Learn how to fetch & update Entities in your Metadata Graph programmatically
## Queries: Reading an Entity
DataHub provides the following GraphQL queries for retrieving entities in your Metadata Graph.
### Getting a Metadata Entity
To retrieve a Metadata Entity by primary key (urn), simply use the `<entityName>(urn: String!)` GraphQL Query.
For example, to retrieve a User entity, you can issue the following GraphQL Query:
*As GraphQL*
```graphql
{
corpUser(urn: "urn:li:corpuser:datahub") {
username
urn
}
}
```
*As CURL*
```curl
curl --location --request POST 'http://localhost:8080/api/graphql' \
--header 'X-DataHub-Actor: urn:li:corpuser:datahub' \
--header 'Content-Type: application/json' \
2021-11-09 18:04:43 -08:00
--data-raw '{ "query":"{ corpUser(urn: \"urn:li:corpuser:datahub\") { username urn } }", "variables":{}}'
2021-09-22 17:30:15 -07:00
```
### Searching for a Metadata Entity
To perform full-text search against an Entity of a particular type, use the `search(input: SearchInput!)` GraphQL Query.
As GraphQL:
```graphql
{
2022-02-17 01:08:29 +01:00
search(input: { type: DATASET, query: "my sql dataset", start: 0, count: 10 }) {
2021-09-22 17:30:15 -07:00
start
count
total
searchResults {
entity {
urn
type
...on Dataset {
name
}
}
}
}
}
```
As CURL:
```curl
curl --location --request POST 'http://localhost:8080/api/graphql' \
--header 'X-DataHub-Actor: urn:li:corpuser:datahub' \
--header 'Content-Type: application/json' \
2021-11-09 18:04:43 -08:00
--data-raw '{ "query":"{ search(input: { type: DATASET, query: \"my sql dataset\", start: 0, count: 10 }) { start count total searchResults { entity { urn type ...on Dataset { name } } } } }", "variables":{}}'
2021-09-22 17:30:15 -07:00
```
Note that per-field filtering criteria may additionally be provided.
2022-01-13 00:44:31 +05:30
### Querying for owners of a dataset
As GraphQL:
```graphql
query {
dataset(urn: "urn:li:dataset:(urn:li:dataPlatform:hdfs,SampleHdfsDataset,PROD)") {
ownership {
owners {
owner {
... on CorpUser {
urn
type
}
... on CorpGroup {
urn
type
}
}
}
}
}
}
```
### Querying for tags of a dataset
As GraphQL:
```graphql
query {
dataset(urn: "urn:li:dataset:(urn:li:dataPlatform:hdfs,SampleHdfsDataset,PROD)") {
tags {
tags {
tag {
name
}
}
}
}
}
```
2021-09-22 17:30:15 -07:00
### Coming soon
List Metadata Entities! listDatasets, listDashboards, listCharts, listDataFlows, listDataJobs, listTags
## Mutations: Modifying an Entity
DataHub provides the following GraphQL mutations for updating entities in your Metadata Graph.
### Authorization
2022-07-01 20:35:55 +01:00
Mutations which change Entity metadata are subject to [DataHub Access Policies ](../../authorization/policies.md ). This means that DataHub's server
2021-09-22 17:30:15 -07:00
will check whether the requesting actor is authorized to perform the action. If you're querying the GraphQL endpoint via the DataHub
Proxy Server, which is discussed more in [Getting Started ](./getting-started.md ), then the Session Cookie provided will carry the actor information.
If you're querying the Metadata Service API directly, then you'll have to provide this via a special `X-DataHub-Actor` HTTP header, which should
contain the URN (primary key) of the actor making the request. For example, `X-DataHub-Actor: urn:li:corpuser:datahub` .
### Updating a Metadata Entity
To update an existing Metadata Entity, simply use the `update<entityName>(urn: String!, input: EntityUpdateInput!)` GraphQL Query.
For example, to update a Dashboard entity, you can issue the following GraphQL mutation:
*As GraphQL*
```graphql
mutation updateDashboard {
updateDashboard(
urn: "urn:li:dashboard:(looker,baz)",
input: {
editableProperties: {
description: "My new desription"
}
}
) {
urn
}
}
```
*As CURL*
```curl
curl --location --request POST 'http://localhost:8080/api/graphql' \
--header 'X-DataHub-Actor: urn:li:corpuser:datahub' \
--header 'Content-Type: application/json' \
2021-11-09 18:04:43 -08:00
--data-raw '{ "query": "mutation updateDashboard { updateDashboard(urn:\"urn:li:dashboard:(looker,baz)\", input: { editableProperties: { description: \"My new desription\" } } ) { urn } }", "variables":{}}'
2021-09-22 17:30:15 -07:00
```
**Be careful**: these APIs allow you to make significant changes to a Metadata Entity, often including
updating the entire set of Owners & Tags.
### Adding & Removing Tags / Glossary Terms
To attach a Tag or Glossary Term to a Metadata Entity, you can use the `addTag` and `addTerm` mutations.
To remove them, you can use the `removeTag` and `removeTerm` mutations.
For example, to add a Tag a DataFlow entity, you can issue the following GraphQL mutation:
*As GraphQL*
```graphql
mutation addTag {
addTag(input: { tagUrn: "urn:li:tag:NewTag", resourceUrn: "urn:li:dataFlow:(airflow,dag_abc,PROD)" })
}
```
*As CURL*
```curl
curl --location --request POST 'http://localhost:8080/api/graphql' \
--header 'X-DataHub-Actor: urn:li:corpuser:datahub' \
--header 'Content-Type: application/json' \
2021-11-09 18:04:43 -08:00
--data-raw '{ "query": "mutation addTag { addTag(input: { tagUrn: \"urn:li:tag:NewTag\", resourceUrn: \"urn:li:dataFlow:(airflow,dag_abc,PROD)\" }) }", "variables":{}}'
2021-09-22 17:30:15 -07:00
```
### Coming soon
**Entity Creation**: createDataset, createDashboard, createChart, etc.
**Entity Removal**: removeDataset, removeDashboard, removeChart, etc.
## Handling Errors
In GraphQL, requests that have errors do not always result in a non-200 HTTP response body. Instead, errors will be
present in the response body inside a top-level `errors` field.
This enables situations in which the client is able to deal gracefully will partial data returned by the application server.
To verify that no error has returned after making a GraphQL request, make sure you check *both* the `data` and `errors` fields that are returned.
## Feedback, Feature Requests, & Support
Visit our [Slack channel ](https://datahubspace.slack.com/join/shared_invite/zt-nx7i0dj7-I3IJYC551vpnvvjIaNRRGw#/shared-invite/email ) to ask questions, tell us what we can do better, & make requests for what you'd like to see in the future. Or just
2022-02-17 01:08:29 +01:00
stop by to say 'Hi'.