GraphQL Module

exception pynautobot.core.graphql.GraphQLException(graph_err)

Bases: Exception

__init__(graph_err)

GraphQL Exception handling.

Parameters:

graph_err (Exception) – Exception from the request

class pynautobot.core.graphql.GraphQLQuery(api)

Bases: object

__init__(api)

Initialization of class.

Parameters:

api (pynautobot.api) – pynatutobot Api class to make calls to the Nautobot API endpoints.

query(query: str, variables: Optional[Dict[str, Any]] = None) GraphQLRecord

Runs query against Nautobot Graphql endpoint.

Parameters:
  • query (str) – Query string to send to the API

  • variables (dict) – Dictionary of variables to use with the query string, defaults to None

Raises:
  • GraphQLException

    • When the query string is invalid.

  • TypeError

    • When query passed in is not of type string. - When variables passed in is not a dictionary.

  • Exception

    • When unknown error is passed in, please open an issue so this can be addressed.

Examples

>>> query = '''
... query {
...   sites {
...     id
...     name
...     region {
...       name
...     }
...   }
... }
... '''
>>> nautobot.graphql.query(query=query)
GraphQLRecord(json={'data': {'sites': [{'id': '2cfbc91e-361f-4129-9db6-bc21a6f88d38', 'name': 'ams', 'region': {'name': 'Netherlands'}}, {'id': '7ef021de-eb21-4403-8438-0e722c0f4b44', 'name': 'atl', 'region': {'name': 'United States'}}]}}, status_code=200)
>>> query = '''
... query {
...   sites (name: "den") {
...     id
...     name
...     region {
...       name
...     }
...   }
... }
... '''
>>> nautobot.graphql.query(query=query)
GraphQLRecord(json={'data': {'sites': [{'id': '45399b54-47f9-4eec-86e3-47352e103b1b', 'name': 'den', 'region': {'name': 'United States'}}]}}, status_code=200)
>>> variables = {"site_name": "den"}
>>> query = '''
... query ($site_name:String!) {
...   sites (name: $site_name) {
...     id
...     name
...     region {
...       name
...     }
...   }
... }
... '''
>>> nautobot.graphql.query(query=query, variables=variables)
GraphQLRecord(json={'data': {'sites': [{'id': '45399b54-47f9-4eec-86e3-47352e103b1b', 'name': 'den', 'region': {'name': 'United States'}}]}}, status_code=200)
Returns:

Response of the API call

Return type:

GraphQLRecord

class pynautobot.core.graphql.GraphQLRecord(json: Dict[str, Any], status_code: int)

Bases: object

__init__(json: Dict[str, Any], status_code: int)

Initialization of class.

Parameters:
  • json (dict) – The json response from making a graphql query.

  • status_code (int) – The HTTP status code from making the graphql query.