I've just begun learning about GraphQL but am wondering if you can assist me:
I hoping for a query that will return every Workbook name (in a specific Project), along with details of the extract it might use (specifically the period, but if I can isolate it to extract name that works just as well).
It would also be wonderful to return the names of the users in the last 30 days.
Is this possible? I've not been able to make sense of the extract logic ATM.
Michael Hesser (Tableau Forum Ambassador)
I've found the querying (or the back-end data) a bit clunky, so I gave up trying to get everything at once, and just created my own data source from multiple queries, relating a data source list to a workbook list. It might not give you everything you want, but maybe the queries themselves will give you some help moving toward a query that works for you:
// Workbooks, just paste the query string into GraphQL if you want to see the output
query = """
query workbooks {
workbooks {
name
id
projectName
owner {
name
username
email
}
embeddedDatasources {
id
}
}
}
"""
In Python, to get it into a 'readable' dataframe format with one row for each data source ID:
// All of the imports I have, though probably not all are used in the code I've pasted:
import pandas as pd
import tableauserverclient as TSC
import json
from tqdm import trange
from IPython.display import display, clear_output
import requests
import re
from unidecode import unidecode
dfw = pd.json_normalize(ts._server.metadata.query(query)["data"]["workbooks"])
dfw = dfw.explode('embeddedDatasources').reset_index(drop=True)
dfw["embeddedDatasources"] = dfw["embeddedDatasources"].apply(lambda x: x["id"] if isinstance(x,dict) else None)
# Make a dictionary to put the workbook ID in the datasource, instead of the other way round
ds_to_wb = dict(dfw[["Datasource ID","Workbook ID"]].dropna().values)
A parameterised query to get the fields in each data source (you could just omit the fields if you just want data source names). I haven't put in the subsequent stuff around getting field comments etc. and calculation formulas:
query = """
query datasource ($ds: ID) {
datasources (filter: {id: $ds} ) {
id
name
fields {
id
}
}
}
"""
Then run that query for each datasource ID:
ws_list = list(dfw["Datasource ID"].dropna().unique())
dfd_list = []
for x in trange(len(ws_list)):
ds = ws_list[x]
data = ts._server.metadata.query(query, variables={"ds": ds} )
df = pd.json_normalize(data["data"]["datasources"])
dfd_list.append(df)
dfd = pd.concat(dfd_list)
dfd = dfd.explode('fields').reset_index(drop=True)
dict_expanded = pd.json_normalize(dfd['fields'])
dfd = pd.concat([dfd.drop(columns=['fields']), dict_expanded], axis=1)
dfd.columns = ["Datasource ID","Datasource Name","Field ID"]
dfd["Workbook ID"] = dfd["Datasource ID"].map(ds_to_wb)