Skip to content
Advertisement

Dynamically generate a dictionary with GraphQL response values as key names

The goal of the following piece of code is to create a dictionary that is populated with data received from GraphQL query responses.

Manager list is constructed as the following: managers_list: [‘manager1@email.com’, ‘manager2@email.com’]

The “get_employees_per_manager” function takes a list of managers and a token, then it returns a list of employees and their actions.

“execute_query” function executes the given query and extracts all the necessary data.

JavaScript

Here’s the fragment of data that I currently have after extracting all everything I need from the response:

JavaScript

I imagine that the final structure of the dictionary should look like this:

JavaScript

So far I’ve tried to assign received results as new keys by using the append() function but that did not do the job. I’m not sure what would be the most ‘pythonic’ way to handle that.

Advertisement

Answer

You need to assign a dictionary to result[manager] first. Also, you need to assign the actions to result[manager][employee]:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement