Skip to content

GraphQL

pynautobot.core.graphql

GraphQL endpoint for making queries to the Nautobot GraphQL endpoint.

pynautobot.core.graphql.GraphQLException

Bases: Exception

__init__(graph_err)

GraphQL Exception handling.

Parameters:

Name Type Description Default
graph_err Exception

Exception from the request

required

pynautobot.core.graphql.GraphQLQuery

__init__(api)

Initialization of class.

Parameters:

Name Type Description Default
api api

pynatutobot Api class to make calls to the Nautobot API endpoints.

required

query(query, variables=None)

Runs query against Nautobot Graphql endpoint.

Parameters:

Name Type Description Default
query str

Query string to send to the API

required
variables dict

Dictionary of variables to use with the query string, defaults to None

None

Raises:

Type Description
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:

Name Type Description
GraphQLRecord GraphQLRecord

Response of the API call

pynautobot.core.graphql.GraphQLRecord

__init__(json, status_code)

Initialization of class.

Parameters:

Name Type Description Default
json dict

The json response from making a graphql query.

required
status_code int

The HTTP status code from making the graphql query.

required