Skip to content

Commit 1f752b6

Browse files
authored
Warn if fields or exclude are not defined on DjangoObjectType (#981)
1 parent 17146f9 commit 1f752b6

File tree

22 files changed

+150
-1
lines changed

22 files changed

+150
-1
lines changed

docs/authorization.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ If you are using ``DjangoObjectType`` you can define a custom `get_queryset`.
111111
class PostNode(DjangoObjectType):
112112
class Meta:
113113
model = Post
114+
fields = '__all__'
114115
115116
@classmethod
116117
def get_queryset(cls, queryset, info):

docs/filtering.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ For example:
4545
class Meta:
4646
# Assume you have an Animal model defined with the following fields
4747
model = Animal
48+
fields = '__all__'
4849
filter_fields = ['name', 'genus', 'is_domesticated']
4950
interfaces = (relay.Node, )
5051
@@ -75,6 +76,7 @@ You can also make more complex lookup types available:
7576
class AnimalNode(DjangoObjectType):
7677
class Meta:
7778
model = Animal
79+
fields = '__all__'
7880
# Provide more complex lookup types
7981
filter_fields = {
8082
'name': ['exact', 'icontains', 'istartswith'],
@@ -116,6 +118,7 @@ create your own ``FilterSet``. You can pass it directly as follows:
116118
class Meta:
117119
# Assume you have an Animal model defined with the following fields
118120
model = Animal
121+
fields = '__all__'
119122
filter_fields = ['name', 'genus', 'is_domesticated']
120123
interfaces = (relay.Node, )
121124
@@ -179,6 +182,7 @@ in unison with the ``filter_fields`` parameter:
179182
class AnimalNode(DjangoObjectType):
180183
class Meta:
181184
model = Animal
185+
fields = '__all__'
182186
filterset_class = AnimalFilter
183187
interfaces = (relay.Node, )
184188
@@ -236,6 +240,7 @@ Extend the tuple of fields if you want to order by more than one field.
236240
class Meta:
237241
name = 'Group'
238242
model = GroupModel
243+
fields = '__all__'
239244
interfaces = (relay.Node,)
240245
241246
def resolve_users(self, info, **kwargs):

docs/mutations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Simple example
2525
class QuestionType(DjangoObjectType):
2626
class Meta:
2727
model = Question
28+
fields = '__all__'
2829
2930
3031
class QuestionMutation(graphene.Mutation):
@@ -90,6 +91,7 @@ DjangoModelFormMutation
9091
class PetType(DjangoObjectType):
9192
class Meta:
9293
model = Pet
94+
fields = '__all__'
9395
9496
class PetMutation(DjangoModelFormMutation):
9597
pet = Field(PetType)

docs/queries.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Full example
2828
class QuestionType(DjangoObjectType):
2929
class Meta:
3030
model = Question
31+
fields = '__all__'
3132
3233
3334
class Query:
@@ -53,6 +54,9 @@ all fields that should be exposed using the fields attribute.
5354
This will make it less likely to result in unintentionally exposing data when
5455
your models change.
5556

57+
Setting neither ``fields`` nor ``exclude`` is deprecated and will raise a warning, you should at least explicitly make
58+
``DjangoObjectType`` include all fields in the model as described below.
59+
5660
``fields``
5761
~~~~~~~~~~
5862

@@ -127,6 +131,7 @@ For example the following ``Model`` and ``DjangoObjectType``:
127131
class Pet(DjangoObjectType):
128132
class Meta:
129133
model = PetModel
134+
fields = '__all__'
130135
131136
Results in the following GraphQL schema definition:
132137

@@ -151,6 +156,7 @@ You can disable this automatic conversion by setting
151156
class Pet(DjangoObjectType):
152157
class Meta:
153158
model = PetModel
159+
fields = '__all__'
154160
convert_choices_to_enum = False
155161
156162
.. code::
@@ -168,6 +174,7 @@ automatically converted into enums:
168174
class Pet(DjangoObjectType):
169175
class Meta:
170176
model = PetModel
177+
fields = '__all__'
171178
convert_choices_to_enum = ['kind']
172179
173180
**Note:** Setting ``convert_choices_to_enum = []`` is the same as setting it to
@@ -206,6 +213,7 @@ need to create the most basic class for this to work:
206213
class CategoryType(DjangoObjectType):
207214
class Meta:
208215
model = Category
216+
fields = '__all__'
209217
210218
.. _django-objecttype-get-queryset:
211219

@@ -224,6 +232,7 @@ Use this to control filtering on the ObjectType level instead of the Query objec
224232
class QuestionType(DjangoObjectType):
225233
class Meta:
226234
model = Question
235+
fields = '__all__'
227236
228237
@classmethod
229238
def get_queryset(cls, queryset, info):
@@ -347,6 +356,7 @@ the core graphene pages for more information on customizing the Relay experience
347356
class QuestionType(DjangoObjectType):
348357
class Meta:
349358
model = Question
359+
fields = '__all__'
350360
interfaces = (relay.Node,)
351361
352362

docs/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
class CategoryType(DjangoObjectType):
99
class Meta:
1010
model = Category
11+
fields = '__all__'
1112

1213

1314
class IngredientType(DjangoObjectType):
1415
class Meta:
1516
model = Ingredient
17+
fields = '__all__'
1618

1719

1820
class Query(object):

docs/tutorial-plain.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
157157
class CategoryType(DjangoObjectType):
158158
class Meta:
159159
model = Category
160+
fields = '__all__'
160161
161162
162163
class IngredientType(DjangoObjectType):
163164
class Meta:
164165
model = Ingredient
166+
fields = '__all__'
165167
166168
167169
class Query(object):

docs/tutorial-relay.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,15 @@ Create ``cookbook/ingredients/schema.py`` and type the following:
130130
class CategoryNode(DjangoObjectType):
131131
class Meta:
132132
model = Category
133+
fields = '__all__'
133134
filter_fields = ['name', 'ingredients']
134135
interfaces = (relay.Node, )
135136
136137
137138
class IngredientNode(DjangoObjectType):
138139
class Meta:
139140
model = Ingredient
141+
fields = '__all__'
140142
# Allow for some more advanced filtering here
141143
filter_fields = {
142144
'name': ['exact', 'icontains', 'istartswith'],

examples/cookbook-plain/cookbook/ingredients/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
class CategoryType(DjangoObjectType):
88
class Meta:
99
model = Category
10+
fields = "__all__"
1011

1112

1213
class IngredientType(DjangoObjectType):
1314
class Meta:
1415
model = Ingredient
16+
fields = "__all__"
1517

1618

1719
class Query(object):

examples/cookbook-plain/cookbook/recipes/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
class RecipeType(DjangoObjectType):
88
class Meta:
99
model = Recipe
10+
fields = "__all__"
1011

1112

1213
class RecipeIngredientType(DjangoObjectType):
1314
class Meta:
1415
model = RecipeIngredient
16+
fields = "__all__"
1517

1618

1719
class Query(object):

examples/cookbook/cookbook/ingredients/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CategoryNode(DjangoObjectType):
1010
class Meta:
1111
model = Category
1212
interfaces = (Node,)
13+
fields = "__all__"
1314
filter_fields = ["name", "ingredients"]
1415

1516

@@ -18,6 +19,7 @@ class Meta:
1819
model = Ingredient
1920
# Allow for some more advanced filtering here
2021
interfaces = (Node,)
22+
fields = "__all__"
2123
filter_fields = {
2224
"name": ["exact", "icontains", "istartswith"],
2325
"notes": ["exact", "icontains"],

examples/cookbook/cookbook/recipes/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class RecipeNode(DjangoObjectType):
88
class Meta:
99
model = Recipe
1010
interfaces = (Node,)
11+
fields = "__all__"
1112
filter_fields = ["title", "amounts"]
1213

1314

@@ -16,6 +17,7 @@ class Meta:
1617
model = RecipeIngredient
1718
# Allow for some more advanced filtering here
1819
interfaces = (Node,)
20+
fields = "__all__"
1921
filter_fields = {
2022
"ingredient__name": ["exact", "icontains", "istartswith"],
2123
"recipe": ["exact"],

examples/starwars/schema.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Ship(DjangoObjectType):
1212
class Meta:
1313
model = ShipModel
1414
interfaces = (relay.Node,)
15+
fields = "__all__"
1516

1617
@classmethod
1718
def get_node(cls, info, id):
@@ -22,12 +23,14 @@ def get_node(cls, info, id):
2223
class Character(DjangoObjectType):
2324
class Meta:
2425
model = CharacterModel
26+
fields = "__all__"
2527

2628

2729
class Faction(DjangoObjectType):
2830
class Meta:
2931
model = FactionModel
3032
interfaces = (relay.Node,)
33+
fields = "__all__"
3134

3235
@classmethod
3336
def get_node(cls, info, id):

graphene_django/debug/tests/test_query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ReporterType(DjangoObjectType):
2121
class Meta:
2222
model = Reporter
2323
interfaces = (Node,)
24+
fields = "__all__"
2425

2526
class Query(graphene.ObjectType):
2627
reporter = graphene.Field(ReporterType)
@@ -65,6 +66,7 @@ class ReporterType(DjangoObjectType):
6566
class Meta:
6667
model = Reporter
6768
interfaces = (Node,)
69+
fields = "__all__"
6870

6971
class Query(graphene.ObjectType):
7072
reporter = graphene.Field(ReporterType)
@@ -130,6 +132,7 @@ class ReporterType(DjangoObjectType):
130132
class Meta:
131133
model = Reporter
132134
interfaces = (Node,)
135+
fields = "__all__"
133136

134137
class Query(graphene.ObjectType):
135138
all_reporters = graphene.List(ReporterType)
@@ -172,6 +175,7 @@ class ReporterType(DjangoObjectType):
172175
class Meta:
173176
model = Reporter
174177
interfaces = (Node,)
178+
fields = "__all__"
175179

176180
class Query(graphene.ObjectType):
177181
all_reporters = DjangoConnectionField(ReporterType)
@@ -220,6 +224,7 @@ class ReporterType(DjangoObjectType):
220224
class Meta:
221225
model = Reporter
222226
interfaces = (Node,)
227+
fields = "__all__"
223228

224229
class Query(graphene.ObjectType):
225230
all_reporters = DjangoFilterConnectionField(ReporterType, fields=["last_name"])

0 commit comments

Comments
 (0)