1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Files
netbox-community-netbox/docs/plugins/development/graphql-api.md

50 lines
1.1 KiB
Markdown
Raw Normal View History

2022-01-27 14:18:25 -05:00
# GraphQL API
## Defining the Schema Class
A plugin can extend NetBox's GraphQL API by registering its own schema class. By default, NetBox will attempt to import `graphql.schema` from the plugin, if it exists. This path can be overridden by defining `graphql_schema` on the PluginConfig instance as the dotted path to the desired Python class. This class must be a subclass of `graphene.ObjectType`.
### Example
```python
# graphql.py
2024-03-11 08:50:56 -07:00
from typing import List
import strawberry
import strawberry_django
2022-01-27 14:18:25 -05:00
2024-03-11 08:50:56 -07:00
from . import models
2022-01-27 14:18:25 -05:00
2024-03-11 08:50:56 -07:00
@strawberry_django.type(
models.MyModel,
fields='__all__',
)
class MyModelType:
pass
2022-01-27 14:18:25 -05:00
2024-03-11 08:50:56 -07:00
@strawberry.type
class MyQuery:
@strawberry.field
def dummymodel(self, id: int) -> DummyModelType:
return None
dummymodel_list: List[DummyModelType] = strawberry_django.field()
schema = [
MyQuery,
]
2022-01-27 14:18:25 -05:00
```
## GraphQL Objects
NetBox provides two object type classes for use by plugins.
::: netbox.graphql.types.BaseObjectType
options:
members: false
::: netbox.graphql.types.NetBoxObjectType
options:
members: false