or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

asgi.mdcore-schema.mderror-handling.mdexecution.mdexplorer.mdfederation.mdfile-uploads.mdindex.mdrelay.mdresolvers.mdtype-bindables.mdvalidation.md

type-bindables.mddocs/

0

# Type Bindables

1

2

Classes for binding Python logic to different GraphQL schema elements. These bindables implement the schema bindable pattern, allowing you to associate resolvers and business logic with GraphQL types defined in SDL.

3

4

## Capabilities

5

6

### Object Types

7

8

Bindable for GraphQL object types, providing field resolvers and type-specific logic.

9

10

```python { .api }

11

class ObjectType(SchemaBindable):

12

"""Bindable for GraphQL object types."""

13

14

def __init__(self, name: str):

15

"""

16

Initialize ObjectType bindable.

17

18

Parameters:

19

- name: Name of GraphQL object type to bind to

20

"""

21

22

def field(self, name: str):

23

"""

24

Decorator for binding resolver to object field.

25

26

Parameters:

27

- name: Name of field to bind resolver to

28

29

Returns:

30

Decorator function for resolver

31

"""

32

33

def set_field(self, name: str, resolver: Callable):

34

"""

35

Set resolver for object field.

36

37

Parameters:

38

- name: Name of field to set resolver for

39

- resolver: Resolver function

40

"""

41

```

42

43

### Root Types

44

45

Specialized object types for GraphQL root operations.

46

47

```python { .api }

48

class QueryType(ObjectType):

49

"""Specialized ObjectType for Query root type."""

50

51

def __init__(self):

52

"""Initialize QueryType bindable (binds to 'Query' type)."""

53

54

class MutationType(ObjectType):

55

"""Specialized ObjectType for Mutation root type."""

56

57

def __init__(self):

58

"""Initialize MutationType bindable (binds to 'Mutation' type)."""

59

```

60

61

### Subscription Types

62

63

Bindable for GraphQL subscription types with support for real-time data streaming.

64

65

```python { .api }

66

class SubscriptionType(SchemaBindable):

67

"""Bindable for GraphQL subscription types."""

68

69

def __init__(self, name: str = "Subscription"):

70

"""

71

Initialize SubscriptionType bindable.

72

73

Parameters:

74

- name: Name of GraphQL subscription type to bind to

75

"""

76

77

def field(self, name: str):

78

"""

79

Decorator for binding subscription resolver to field.

80

81

Parameters:

82

- name: Name of subscription field

83

84

Returns:

85

Decorator function for subscription resolver

86

"""

87

88

def source(self, name: str):

89

"""

90

Decorator for binding source resolver to subscription field.

91

92

Parameters:

93

- name: Name of subscription field

94

95

Returns:

96

Decorator function for source resolver

97

"""

98

```

99

100

### Scalar Types

101

102

Bindable for custom GraphQL scalar types with serialization and parsing logic.

103

104

```python { .api }

105

class ScalarType(SchemaBindable):

106

"""Bindable for GraphQL scalar types."""

107

108

def __init__(self, name: str):

109

"""

110

Initialize ScalarType bindable.

111

112

Parameters:

113

- name: Name of GraphQL scalar type to bind to

114

"""

115

116

def serializer(self, serializer_func: Callable):

117

"""

118

Set serialization function for scalar.

119

120

Parameters:

121

- serializer_func: Function that converts Python value to JSON-serializable value

122

123

Returns:

124

The serializer function

125

"""

126

127

def value_parser(self, parser_func: Callable):

128

"""

129

Set value parsing function for scalar.

130

131

Parameters:

132

- parser_func: Function that parses input value to Python value

133

134

Returns:

135

The parser function

136

"""

137

138

def literal_parser(self, parser_func: Callable):

139

"""

140

Set literal parsing function for scalar.

141

142

Parameters:

143

- parser_func: Function that parses AST literal to Python value

144

145

Returns:

146

The parser function

147

"""

148

```

149

150

### Enum Types

151

152

Bindable for GraphQL enum types with custom value mappings.

153

154

```python { .api }

155

class EnumType(SchemaBindable):

156

"""Bindable for GraphQL enum types."""

157

158

def __init__(self, name: str, values: Optional[dict] = None):

159

"""

160

Initialize EnumType bindable.

161

162

Parameters:

163

- name: Name of GraphQL enum type to bind to

164

- values: Dict mapping GraphQL enum values to Python values

165

"""

166

```

167

168

### Interface Types

169

170

Bindable for GraphQL interface types with type resolution logic.

171

172

```python { .api }

173

class InterfaceType(SchemaBindable):

174

"""Bindable for GraphQL interface types."""

175

176

def __init__(self, name: str):

177

"""

178

Initialize InterfaceType bindable.

179

180

Parameters:

181

- name: Name of GraphQL interface type to bind to

182

"""

183

184

def field(self, name: str):

185

"""

186

Decorator for binding resolver to interface field.

187

188

Parameters:

189

- name: Name of field to bind resolver to

190

191

Returns:

192

Decorator function for resolver

193

"""

194

195

def type_resolver(self, type_resolver_func: Callable):

196

"""

197

Set type resolver for interface.

198

199

Parameters:

200

- type_resolver_func: Function that determines concrete type for interface value

201

202

Returns:

203

The type resolver function

204

"""

205

```

206

207

### Union Types

208

209

Bindable for GraphQL union types with type resolution logic.

210

211

```python { .api }

212

class UnionType(SchemaBindable):

213

"""Bindable for GraphQL union types."""

214

215

def __init__(self, name: str):

216

"""

217

Initialize UnionType bindable.

218

219

Parameters:

220

- name: Name of GraphQL union type to bind to

221

"""

222

223

def type_resolver(self, type_resolver_func: Callable):

224

"""

225

Set type resolver for union.

226

227

Parameters:

228

- type_resolver_func: Function that determines concrete type for union value

229

230

Returns:

231

The type resolver function

232

"""

233

```

234

235

### Input Types

236

237

Bindable for GraphQL input types with output transformation logic.

238

239

```python { .api }

240

class InputType(SchemaBindable):

241

"""Bindable for GraphQL input types."""

242

243

def __init__(self, name: str):

244

"""

245

Initialize InputType bindable.

246

247

Parameters:

248

- name: Name of GraphQL input type to bind to

249

"""

250

251

def out_type(self, out_type_func: Callable):

252

"""

253

Set output transformation function for input type.

254

255

Parameters:

256

- out_type_func: Function that transforms input dict to custom Python object

257

258

Returns:

259

The transformation function

260

"""

261

```

262

263

## Usage Examples

264

265

### Object Type with Field Resolvers

266

267

```python

268

from ariadne import ObjectType

269

270

user_type = ObjectType("User")

271

272

@user_type.field("fullName")

273

def resolve_full_name(user, info):

274

return f"{user['firstName']} {user['lastName']}"

275

276

@user_type.field("posts")

277

def resolve_user_posts(user, info):

278

return get_posts_by_user_id(user["id"])

279

280

# Alternative method

281

def resolve_email(user, info):

282

return user.get("email", "")

283

284

user_type.set_field("email", resolve_email)

285

```

286

287

### Custom Scalar Type

288

289

```python

290

from datetime import datetime

291

from ariadne import ScalarType

292

293

date_scalar = ScalarType("Date")

294

295

@date_scalar.serializer

296

def serialize_date(value):

297

if isinstance(value, datetime):

298

return value.isoformat()

299

return value

300

301

@date_scalar.value_parser

302

def parse_date_value(value):

303

if isinstance(value, str):

304

return datetime.fromisoformat(value)

305

return value

306

307

@date_scalar.literal_parser

308

def parse_date_literal(ast):

309

return datetime.fromisoformat(ast.value)

310

```

311

312

### Enum Type with Custom Values

313

314

```python

315

from enum import Enum

316

from ariadne import EnumType

317

318

class UserRole(Enum):

319

ADMIN = "administrator"

320

USER = "regular_user"

321

GUEST = "guest_user"

322

323

role_enum = EnumType("UserRole", {

324

"ADMIN": UserRole.ADMIN,

325

"USER": UserRole.USER,

326

"GUEST": UserRole.GUEST

327

})

328

```

329

330

### Interface Type with Type Resolution

331

332

```python

333

from ariadne import InterfaceType

334

335

node_interface = InterfaceType("Node")

336

337

@node_interface.type_resolver

338

def resolve_node_type(obj, info, type_):

339

if isinstance(obj, dict):

340

if "title" in obj:

341

return "Post"

342

elif "firstName" in obj:

343

return "User"

344

return None

345

346

@node_interface.field("id")

347

def resolve_node_id(obj, info):

348

return obj["id"]

349

```

350

351

### Subscription Type

352

353

```python

354

import asyncio

355

from ariadne import SubscriptionType

356

357

subscription = SubscriptionType()

358

359

@subscription.source("messageAdded")

360

async def message_added_source(obj, info, **kwargs):

361

# Return async iterator/generator

362

while True:

363

yield {"content": "New message", "timestamp": time.time()}

364

await asyncio.sleep(1)

365

366

@subscription.field("messageAdded")

367

def message_added_resolver(message, info, **kwargs):

368

return message

369

```