Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if fields or exclude are not defined on DjangoObjectType #981

Merged
merged 5 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/authorization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ If you are using ``DjangoObjectType`` you can define a custom `get_queryset`.
class PostNode(DjangoObjectType):
class Meta:
model = Post
fields = '__all__'

@classmethod
def get_queryset(cls, queryset, info):
Expand Down
5 changes: 5 additions & 0 deletions docs/filtering.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ For example:
class Meta:
# Assume you have an Animal model defined with the following fields
model = Animal
fields = '__all__'
filter_fields = ['name', 'genus', 'is_domesticated']
interfaces = (relay.Node, )

Expand Down Expand Up @@ -75,6 +76,7 @@ You can also make more complex lookup types available:
class AnimalNode(DjangoObjectType):
class Meta:
model = Animal
fields = '__all__'
# Provide more complex lookup types
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
Expand Down Expand Up @@ -116,6 +118,7 @@ create your own ``FilterSet``. You can pass it directly as follows:
class Meta:
# Assume you have an Animal model defined with the following fields
model = Animal
fields = '__all__'
filter_fields = ['name', 'genus', 'is_domesticated']
interfaces = (relay.Node, )

Expand Down Expand Up @@ -179,6 +182,7 @@ in unison with the ``filter_fields`` parameter:
class AnimalNode(DjangoObjectType):
class Meta:
model = Animal
fields = '__all__'
filterset_class = AnimalFilter
interfaces = (relay.Node, )

Expand Down Expand Up @@ -236,6 +240,7 @@ Extend the tuple of fields if you want to order by more than one field.
class Meta:
name = 'Group'
model = GroupModel
fields = '__all__'
interfaces = (relay.Node,)

def resolve_users(self, info, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions docs/mutations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Simple example
class QuestionType(DjangoObjectType):
class Meta:
model = Question
fields = '__all__'


class QuestionMutation(graphene.Mutation):
Expand Down Expand Up @@ -90,6 +91,7 @@ DjangoModelFormMutation
class PetType(DjangoObjectType):
class Meta:
model = Pet
fields = '__all__'

class PetMutation(DjangoModelFormMutation):
pet = Field(PetType)
Expand Down
10 changes: 10 additions & 0 deletions docs/queries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Full example
class QuestionType(DjangoObjectType):
class Meta:
model = Question
fields = '__all__'


class Query:
Expand All @@ -53,6 +54,9 @@ all fields that should be exposed using the fields attribute.
This will make it less likely to result in unintentionally exposing data when
your models change.

Setting neither ``fields`` nor ``exclude`` is deprecated and will raise a warning, you should at least explicitly make
``DjangoObjectType`` include all fields in the model as described below.

``fields``
~~~~~~~~~~

Expand Down Expand Up @@ -127,6 +131,7 @@ For example the following ``Model`` and ``DjangoObjectType``:
class Pet(DjangoObjectType):
class Meta:
model = PetModel
fields = '__all__'

Results in the following GraphQL schema definition:

Expand All @@ -151,6 +156,7 @@ You can disable this automatic conversion by setting
class Pet(DjangoObjectType):
class Meta:
model = PetModel
fields = '__all__'
convert_choices_to_enum = False

.. code::
Expand All @@ -168,6 +174,7 @@ automatically converted into enums:
class Pet(DjangoObjectType):
class Meta:
model = PetModel
fields = '__all__'
convert_choices_to_enum = ['kind']

**Note:** Setting ``convert_choices_to_enum = []`` is the same as setting it to
Expand Down Expand Up @@ -206,6 +213,7 @@ need to create the most basic class for this to work:
class CategoryType(DjangoObjectType):
class Meta:
model = Category
fields = '__all__'

.. _django-objecttype-get-queryset:

Expand All @@ -224,6 +232,7 @@ Use this to control filtering on the ObjectType level instead of the Query objec
class QuestionType(DjangoObjectType):
class Meta:
model = Question
fields = '__all__'

@classmethod
def get_queryset(cls, queryset, info):
Expand Down Expand Up @@ -347,6 +356,7 @@ the core graphene pages for more information on customizing the Relay experience
class QuestionType(DjangoObjectType):
class Meta:
model = Question
fields = '__all__'
interfaces = (relay.Node,)


Expand Down
2 changes: 2 additions & 0 deletions docs/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
class CategoryType(DjangoObjectType):
class Meta:
model = Category
fields = '__all__'


class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
fields = '__all__'


class Query(object):
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial-plain.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
class CategoryType(DjangoObjectType):
class Meta:
model = Category
fields = '__all__'


class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
fields = '__all__'


class Query(object):
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial-relay.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
class CategoryNode(DjangoObjectType):
class Meta:
model = Category
fields = '__all__'
filter_fields = ['name', 'ingredients']
interfaces = (relay.Node, )


class IngredientNode(DjangoObjectType):
class Meta:
model = Ingredient
fields = '__all__'
# Allow for some more advanced filtering here
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
Expand Down
2 changes: 2 additions & 0 deletions examples/cookbook-plain/cookbook/ingredients/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
class CategoryType(DjangoObjectType):
class Meta:
model = Category
fields = "__all__"


class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
fields = "__all__"


class Query(object):
Expand Down
2 changes: 2 additions & 0 deletions examples/cookbook-plain/cookbook/recipes/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
class RecipeType(DjangoObjectType):
class Meta:
model = Recipe
fields = "__all__"


class RecipeIngredientType(DjangoObjectType):
class Meta:
model = RecipeIngredient
fields = "__all__"


class Query(object):
Expand Down
2 changes: 2 additions & 0 deletions examples/cookbook/cookbook/ingredients/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CategoryNode(DjangoObjectType):
class Meta:
model = Category
interfaces = (Node,)
fields = "__all__"
filter_fields = ["name", "ingredients"]


Expand All @@ -18,6 +19,7 @@ class Meta:
model = Ingredient
# Allow for some more advanced filtering here
interfaces = (Node,)
fields = "__all__"
filter_fields = {
"name": ["exact", "icontains", "istartswith"],
"notes": ["exact", "icontains"],
Expand Down
2 changes: 2 additions & 0 deletions examples/cookbook/cookbook/recipes/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class RecipeNode(DjangoObjectType):
class Meta:
model = Recipe
interfaces = (Node,)
fields = "__all__"
filter_fields = ["title", "amounts"]


Expand All @@ -16,6 +17,7 @@ class Meta:
model = RecipeIngredient
# Allow for some more advanced filtering here
interfaces = (Node,)
fields = "__all__"
filter_fields = {
"ingredient__name": ["exact", "icontains", "istartswith"],
"recipe": ["exact"],
Expand Down
3 changes: 3 additions & 0 deletions examples/starwars/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Ship(DjangoObjectType):
class Meta:
model = ShipModel
interfaces = (relay.Node,)
fields = "__all__"

@classmethod
def get_node(cls, info, id):
Expand All @@ -22,12 +23,14 @@ def get_node(cls, info, id):
class Character(DjangoObjectType):
class Meta:
model = CharacterModel
fields = "__all__"


class Faction(DjangoObjectType):
class Meta:
model = FactionModel
interfaces = (relay.Node,)
fields = "__all__"

@classmethod
def get_node(cls, info, id):
Expand Down
5 changes: 5 additions & 0 deletions graphene_django/debug/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"

class Query(graphene.ObjectType):
reporter = graphene.Field(ReporterType)
Expand Down Expand Up @@ -65,6 +66,7 @@ class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"

class Query(graphene.ObjectType):
reporter = graphene.Field(ReporterType)
Expand Down Expand Up @@ -130,6 +132,7 @@ class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"

class Query(graphene.ObjectType):
all_reporters = graphene.List(ReporterType)
Expand Down Expand Up @@ -172,6 +175,7 @@ class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"

class Query(graphene.ObjectType):
all_reporters = DjangoConnectionField(ReporterType)
Expand Down Expand Up @@ -220,6 +224,7 @@ class ReporterType(DjangoObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
fields = "__all__"

class Query(graphene.ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterType, fields=["last_name"])
Expand Down
Loading