or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-types.mddebug.mdfields.mdfiltering.mdforms.mdindex.mdrest-framework.mdtesting.mdviews.md

fields.mddocs/

0

# GraphQL Fields

1

2

Specialized GraphQL fields for Django querysets with automatic resolution, pagination support, and ORM optimization. These fields provide efficient querying capabilities for Django models through GraphQL with built-in relationship handling and performance optimizations.

3

4

## Capabilities

5

6

### DjangoListField

7

8

Field for returning lists of Django objects with automatic queryset resolution and support for custom managers and filtering.

9

10

```python { .api }

11

class DjangoListField(graphene.Field):

12

"""

13

Field for returning lists of Django objects with automatic queryset resolution.

14

15

Automatically resolves Django model querysets with support for custom managers,

16

prefetch optimization, and filtering integration.

17

"""

18

19

def __init__(self, _type, resolver=None, source=None, args=None, **kwargs):

20

"""

21

Initialize list field with Django object type.

22

23

Parameters:

24

- _type: DjangoObjectType or GraphQL type

25

- resolver: Custom resolver function

26

- source: Source attribute name

27

- args: Additional GraphQL arguments

28

- **kwargs: Additional field options

29

"""

30

31

@property

32

def model(self):

33

"""

34

Get Django model from the field type.

35

36

Returns:

37

django.db.models.Model: Django model class

38

"""

39

40

@property

41

def _underlying_type(self):

42

"""

43

Get the underlying GraphQL type.

44

45

Returns:

46

GraphQL type wrapped by this field

47

"""

48

49

def get_manager(self):

50

"""

51

Get model's default manager for queryset resolution.

52

53

Returns:

54

django.db.models.Manager: Model manager

55

"""

56

57

@staticmethod

58

def list_resolver(django_object_type, resolver, default_manager, root, info, **args):

59

"""

60

Static resolver method for list fields.

61

62

Parameters:

63

- django_object_type: DjangoObjectType class

64

- resolver: Custom resolver function

65

- default_manager: Model's default manager

66

- root: Parent object

67

- info: GraphQL execution info

68

- **args: Resolver arguments

69

70

Returns:

71

QuerySet or list of model instances

72

"""

73

74

def get_resolver(self, parent_resolver):

75

"""

76

Get configured resolver for this field.

77

78

Parameters:

79

- parent_resolver: Parent resolver function

80

81

Returns:

82

function: Resolver function

83

"""

84

```

85

86

### DjangoConnectionField

87

88

Relay-style connection field with Django-specific pagination, filtering, and queryset optimization for efficient data fetching.

89

90

```python { .api }

91

class DjangoConnectionField(graphene.relay.ConnectionField):

92

"""

93

Relay-style connection field with Django-specific pagination.

94

95

Provides cursor-based pagination following the Relay specification

96

with Django QuerySet optimization and filtering integration.

97

"""

98

99

def __init__(self, type, on=None, max_limit=None, enforce_first_or_last=False, **kwargs):

100

"""

101

Initialize connection field.

102

103

Parameters:

104

- type: GraphQL connection type

105

- on: Manager/related name to use for queryset

106

- max_limit: Maximum pagination limit

107

- enforce_first_or_last: Require first/last arguments in queries

108

- **kwargs: Additional connection options

109

"""

110

111

@property

112

def type(self):

113

"""

114

Get the connection type.

115

116

Returns:

117

GraphQL connection type

118

"""

119

120

@property

121

def connection_type(self):

122

"""

123

Get the connection type for this field.

124

125

Returns:

126

GraphQL connection type

127

"""

128

129

@property

130

def node_type(self):

131

"""

132

Get the node type from the connection.

133

134

Returns:

135

GraphQL node type (typically DjangoObjectType)

136

"""

137

138

@property

139

def model(self):

140

"""

141

Get Django model from the connection type.

142

143

Returns:

144

django.db.models.Model: Django model class

145

"""

146

147

def get_manager(self):

148

"""

149

Get the manager for queryset resolution.

150

151

Returns:

152

django.db.models.Manager: Model manager

153

"""

154

155

@classmethod

156

def resolve_queryset(cls, connection, queryset, info, args):

157

"""

158

Resolve queryset for connection field.

159

160

Parameters:

161

- connection: Connection instance

162

- queryset: Django QuerySet

163

- info: GraphQL execution info

164

- args: Field arguments

165

166

Returns:

167

QuerySet: Resolved queryset

168

"""

169

170

@classmethod

171

def resolve_connection(cls, connection, args, iterable):

172

"""

173

Resolve connection with pagination.

174

175

Parameters:

176

- connection: Connection class

177

- args: Pagination arguments (first, last, before, after)

178

- iterable: Iterable data source

179

180

Returns:

181

Connection: Paginated connection instance

182

"""

183

184

@classmethod

185

def connection_resolver(cls, resolver, connection, default_manager, max_limit,

186

enforce_first_or_last, root, info, **args):

187

"""

188

Main resolver logic for connection fields.

189

190

Parameters:

191

- resolver: Custom resolver function

192

- connection: Connection class

193

- default_manager: Model's default manager

194

- max_limit: Maximum pagination limit

195

- enforce_first_or_last: Enforce pagination arguments

196

- root: Parent object

197

- info: GraphQL execution info

198

- **args: Resolver arguments

199

200

Returns:

201

Connection: Resolved connection with pagination

202

"""

203

204

def get_resolver(self, parent_resolver):

205

"""

206

Get configured resolver for this connection field.

207

208

Parameters:

209

- parent_resolver: Parent resolver function

210

211

Returns:

212

function: Connection resolver function

213

"""

214

```

215

216

## Usage Examples

217

218

### Basic List Field

219

220

```python

221

import graphene

222

from graphene_django import DjangoObjectType, DjangoListField

223

224

class UserType(DjangoObjectType):

225

class Meta:

226

model = User

227

fields = '__all__'

228

229

class Query(graphene.ObjectType):

230

all_users = DjangoListField(UserType)

231

232

def resolve_all_users(self, info):

233

return User.objects.all()

234

```

235

236

### Connection Field with Pagination

237

238

```python

239

from graphene_django.fields import DjangoConnectionField

240

241

class UserConnection(graphene.relay.Connection):

242

class Meta:

243

node = UserType

244

245

class Query(graphene.ObjectType):

246

users = DjangoConnectionField(UserType)

247

248

def resolve_users(self, info, **args):

249

return User.objects.all()

250

251

# GraphQL query with pagination:

252

# query {

253

# users(first: 10, after: "cursor") {

254

# edges {

255

# node {

256

# id

257

# username

258

# }

259

# }

260

# pageInfo {

261

# hasNextPage

262

# endCursor

263

# }

264

# }

265

# }

266

```

267

268

### Custom Manager Integration

269

270

```python

271

class ActiveUserManager(models.Manager):

272

def get_queryset(self):

273

return super().get_queryset().filter(is_active=True)

274

275

class User(models.Model):

276

username = models.CharField(max_length=150)

277

is_active = models.BooleanField(default=True)

278

279

objects = models.Manager()

280

active = ActiveUserManager()

281

282

class Query(graphene.ObjectType):

283

active_users = DjangoListField(UserType)

284

285

def resolve_active_users(self, info):

286

return User.active.all() # Uses custom manager

287

```

288

289

### Connection with Custom Limit

290

291

```python

292

class Query(graphene.ObjectType):

293

users = DjangoConnectionField(

294

UserType,

295

max_limit=100, # Limit maximum page size

296

enforce_first_or_last=True # Require pagination args

297

)

298

```

299

300

### Relationship Fields

301

302

```python

303

class PostType(DjangoObjectType):

304

class Meta:

305

model = Post

306

fields = '__all__'

307

308

class UserType(DjangoObjectType):

309

posts = DjangoConnectionField(PostType)

310

311

class Meta:

312

model = User

313

fields = '__all__'

314

315

def resolve_posts(self, info, **args):

316

return self.post_set.all()

317

```

318

319

### Field with Custom Arguments

320

321

```python

322

class Query(graphene.ObjectType):

323

users_by_role = DjangoListField(

324

UserType,

325

role=graphene.String(required=True)

326

)

327

328

def resolve_users_by_role(self, info, role):

329

return User.objects.filter(role=role)

330

```