File: models.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (804 lines) | stat: -rw-r--r-- 27,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
import base64
import json
from collections.abc import Iterable
from datetime import datetime, timedelta, timezone
from typing import Any, Optional, Union

from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.core.utils import unix_time
from moto.moto_api._internal import mock_random
from moto.utilities.tagging_service import TaggingService
from moto.utilities.utils import get_partition

from .exceptions import (
    BadRequestException,
    EventsAPINotFound,
    GraphqlAPICacheNotFound,
    GraphqlAPINotFound,
    GraphQLSchemaException,
)

# AWS custom scalars and directives
# https://github.com/dotansimha/graphql-code-generator/discussions/4311#discussioncomment-2921796
AWS_CUSTOM_GRAPHQL = """scalar AWSTime
scalar AWSDateTime
scalar AWSTimestamp
scalar AWSEmail
scalar AWSJSON
scalar AWSURL
scalar AWSPhone
scalar AWSIPAddress
scalar BigInt
scalar Double

directive @aws_subscribe(mutations: [String!]!) on FIELD_DEFINITION

# Allows transformer libraries to deprecate directive arguments.
directive @deprecated(reason: String!) on INPUT_FIELD_DEFINITION | ENUM

directive @aws_auth(cognito_groups: [String!]!) on FIELD_DEFINITION
directive @aws_api_key on FIELD_DEFINITION | OBJECT
directive @aws_iam on FIELD_DEFINITION | OBJECT
directive @aws_oidc on FIELD_DEFINITION | OBJECT
directive @aws_cognito_user_pools(
  cognito_groups: [String!]
) on FIELD_DEFINITION | OBJECT
"""


# region: APICache
class APICache(BaseModel):
    def __init__(
        self,
        ttl: int,
        api_caching_behavior: str,
        type_: str,
        transit_encryption_enabled: Optional[bool] = None,
        at_rest_encryption_enabled: Optional[bool] = None,
        health_metrics_config: Optional[str] = None,
    ):
        self.ttl = ttl
        self.api_caching_behavior = api_caching_behavior
        self.type = type_
        self.transit_encryption_enabled = transit_encryption_enabled or False
        self.at_rest_encryption_enabled = at_rest_encryption_enabled or False
        self.health_metrics_config = health_metrics_config or "DISABLED"
        self.status = "AVAILABLE"

    def update(
        self,
        ttl: int,
        api_caching_behavior: str,
        type: str,
        health_metrics_config: Optional[str] = None,
    ) -> None:
        self.ttl = ttl
        self.api_caching_behavior = api_caching_behavior
        self.type = type
        if health_metrics_config is not None:
            self.health_metrics_config = health_metrics_config

    def to_json(self) -> dict[str, Any]:
        return {
            "ttl": self.ttl,
            "transitEncryptionEnabled": self.transit_encryption_enabled,
            "atRestEncryptionEnabled": self.at_rest_encryption_enabled,
            "apiCachingBehavior": self.api_caching_behavior,
            "type": self.type,
            "healthMetricsConfig": self.health_metrics_config,
            "status": self.status,
        }


# endregion


# region: GraphqlAPI
class GraphqlSchema(BaseModel):
    def __init__(self, definition: Any, region_name: str):
        self.definition = definition
        self.region_name = region_name
        # [graphql.language.ast.ObjectTypeDefinitionNode, ..]
        self.types: list[Any] = []

        self.status = "PROCESSING"
        self.parse_error: Optional[str] = None
        self._parse_graphql_definition()

    def get_type(self, name: str) -> Optional[dict[str, Any]]:  # type: ignore[return]
        for graphql_type in self.types:
            if graphql_type.name.value == name:
                return {
                    "name": name,
                    "description": graphql_type.description.value
                    if graphql_type.description
                    else None,
                    "arn": f"arn:{get_partition(self.region_name)}:appsync:graphql_type/{name}",
                    "definition": "NotYetImplemented",
                }

    def get_status(self) -> tuple[str, Optional[str]]:
        return self.status, self.parse_error

    def _parse_graphql_definition(self) -> None:
        try:
            from graphql import parse
            from graphql.error.graphql_error import GraphQLError
            from graphql.language.ast import ObjectTypeDefinitionNode

            res = parse(self.definition)
            for definition in res.definitions:
                if isinstance(definition, ObjectTypeDefinitionNode):
                    self.types.append(definition)
            self.status = "SUCCESS"
        except GraphQLError as e:
            self.status = "FAILED"
            self.parse_error = str(e)

    def get_introspection_schema(self, format_: str, include_directives: bool) -> str:
        from graphql import (
            build_client_schema,
            build_schema,
            introspection_from_schema,
            print_schema,
        )

        schema = build_schema(self.definition + AWS_CUSTOM_GRAPHQL)
        introspection_data = introspection_from_schema(schema, descriptions=False)

        if not include_directives:
            introspection_data["__schema"]["directives"] = []

        if format_ == "SDL":
            return print_schema(build_client_schema(introspection_data))
        elif format_ == "JSON":
            return json.dumps(introspection_data)
        else:
            raise BadRequestException(message=f"Invalid format {format_} given")


class GraphqlAPIKey(BaseModel):
    def __init__(self, description: str, expires: Optional[int]):
        self.key_id = str(mock_random.uuid4())[0:6]
        self.description = description
        if not expires:
            default_expiry = datetime.now(timezone.utc)
            default_expiry = default_expiry.replace(
                minute=0, second=0, microsecond=0, tzinfo=None
            )
            default_expiry = default_expiry + timedelta(days=7)
            self.expires = unix_time(default_expiry)
        else:
            self.expires = expires

    def update(self, description: Optional[str], expires: Optional[int]) -> None:
        if description:
            self.description = description
        if expires:
            self.expires = expires

    def to_json(self) -> dict[str, Any]:
        return {
            "id": self.key_id,
            "description": self.description,
            "expires": self.expires,
            "deletes": self.expires,
        }


class GraphqlAPI(BaseModel):
    def __init__(
        self,
        account_id: str,
        region: str,
        name: str,
        authentication_type: str,
        additional_authentication_providers: Optional[list[str]],
        log_config: str,
        xray_enabled: str,
        user_pool_config: str,
        open_id_connect_config: str,
        lambda_authorizer_config: str,
        visibility: str,
        backend: "AppSyncBackend",
    ) -> None:
        self.region = region
        self.name = name
        self.api_id = str(mock_random.uuid4())
        self.authentication_type = authentication_type
        self.additional_authentication_providers = additional_authentication_providers
        self.lambda_authorizer_config = lambda_authorizer_config
        self.log_config = log_config
        self.open_id_connect_config = open_id_connect_config
        self.user_pool_config = user_pool_config
        self.xray_enabled = xray_enabled
        self.visibility = visibility or "GLOBAL"  # Default to Global if not provided

        self.arn = f"arn:{get_partition(self.region)}:appsync:{self.region}:{account_id}:apis/{self.api_id}"
        self.graphql_schema: Optional[GraphqlSchema] = None

        self.api_keys: dict[str, GraphqlAPIKey] = {}

        self.api_cache: Optional[APICache] = None
        self.backend = backend

    def update(
        self,
        name: str,
        additional_authentication_providers: Optional[list[str]],
        authentication_type: str,
        lambda_authorizer_config: str,
        log_config: str,
        open_id_connect_config: str,
        user_pool_config: str,
        xray_enabled: str,
    ) -> None:
        if name:
            self.name = name
        if additional_authentication_providers:
            self.additional_authentication_providers = (
                additional_authentication_providers
            )
        if authentication_type:
            self.authentication_type = authentication_type
        if lambda_authorizer_config:
            self.lambda_authorizer_config = lambda_authorizer_config
        if log_config:
            self.log_config = log_config
        if open_id_connect_config:
            self.open_id_connect_config = open_id_connect_config
        if user_pool_config:
            self.user_pool_config = user_pool_config
        if xray_enabled is not None:
            self.xray_enabled = xray_enabled

    def create_api_key(self, description: str, expires: Optional[int]) -> GraphqlAPIKey:
        api_key = GraphqlAPIKey(description, expires)
        self.api_keys[api_key.key_id] = api_key
        return api_key

    def list_api_keys(self) -> Iterable[GraphqlAPIKey]:
        return self.api_keys.values()

    def delete_api_key(self, api_key_id: str) -> None:
        self.api_keys.pop(api_key_id)

    def update_api_key(
        self, api_key_id: str, description: str, expires: Optional[int]
    ) -> GraphqlAPIKey:
        api_key = self.api_keys[api_key_id]
        api_key.update(description, expires)
        return api_key

    def start_schema_creation(self, definition: str) -> None:
        graphql_definition = base64.b64decode(definition).decode("utf-8")

        self.graphql_schema = GraphqlSchema(graphql_definition, region_name=self.region)

    def get_schema_status(self) -> Any:
        return self.graphql_schema.get_status()  # type: ignore[union-attr]

    def get_type(self, type_name: str, type_format: str) -> Any:
        graphql_type = self.graphql_schema.get_type(type_name)  # type: ignore[union-attr]
        graphql_type["format"] = type_format  # type: ignore[index]
        return graphql_type

    def create_api_cache(
        self,
        ttl: int,
        api_caching_behavior: str,
        type: str,
        transit_encryption_enabled: Optional[bool] = None,
        at_rest_encryption_enabled: Optional[bool] = None,
        health_metrics_config: Optional[str] = None,
    ) -> APICache:
        self.api_cache = APICache(
            ttl,
            api_caching_behavior,
            type,
            transit_encryption_enabled,
            at_rest_encryption_enabled,
            health_metrics_config,
        )
        return self.api_cache

    def update_api_cache(
        self,
        ttl: int,
        api_caching_behavior: str,
        type: str,
        health_metrics_config: Optional[str] = None,
    ) -> APICache:
        self.api_cache.update(ttl, api_caching_behavior, type, health_metrics_config)  # type: ignore[union-attr]
        return self.api_cache  # type: ignore[return-value]

    def delete_api_cache(self) -> None:
        self.api_cache = None

    def to_json(self) -> dict[str, Any]:
        return {
            "name": self.name,
            "apiId": self.api_id,
            "authenticationType": self.authentication_type,
            "arn": self.arn,
            "uris": {"GRAPHQL": "http://graphql.uri"},
            "additionalAuthenticationProviders": self.additional_authentication_providers,
            "lambdaAuthorizerConfig": self.lambda_authorizer_config,
            "logConfig": self.log_config,
            "openIDConnectConfig": self.open_id_connect_config,
            "userPoolConfig": self.user_pool_config,
            "xrayEnabled": self.xray_enabled,
            "visibility": self.visibility,
            "tags": self.backend.list_tags_for_resource(self.arn),
        }


# endregion


# region: EventsAPI
class EventsAPIKey(BaseModel):
    def __init__(self, description: str, expires: Optional[int]):
        self.key_id = str(mock_random.uuid4())[0:6]
        self.description = description
        if not expires:
            default_expiry = datetime.now(timezone.utc)
            default_expiry = default_expiry.replace(
                minute=0, second=0, microsecond=0, tzinfo=None
            )
            default_expiry = default_expiry + timedelta(days=7)
            self.expires = unix_time(default_expiry)
        else:
            self.expires = expires

    def update(self, description: Optional[str], expires: Optional[int]) -> None:
        if description:
            self.description = description
        if expires:
            self.expires = expires

    def to_json(self) -> dict[str, Any]:
        return {
            "id": self.key_id,
            "description": self.description,
            "expires": self.expires,
            "deletes": self.expires,
        }


class ChannelNamespace(BaseModel):
    def __init__(
        self,
        api_id: str,
        name: str,
        subscribe_auth_modes: list[dict[str, str]],
        publish_auth_modes: list[dict[str, str]],
        code_handlers: Optional[list[dict[str, Any]]] = None,
        handler_configs: Optional[dict[str, Any]] = None,
        account_id: str = "",
        region: str = "",
        backend: Optional["AppSyncBackend"] = None,
    ) -> None:
        self.api_id = api_id
        self.name = name
        self.subscribe_auth_modes = subscribe_auth_modes
        self.publish_auth_modes = publish_auth_modes
        self.code_handlers = code_handlers or []
        self.handler_configs = handler_configs or {}

        self.channel_namespace_arn = f"arn:{get_partition(region)}:appsync:{region}:{account_id}:apis/{api_id}/channelNamespace/{name}"

        now = datetime.now(timezone.utc).isoformat()
        self.created = now
        self.last_modified = now

        self.backend = backend

    def to_json(self) -> dict[str, Any]:
        response = {
            "apiId": self.api_id,
            "name": self.name,
            "subscribeAuthModes": self.subscribe_auth_modes,
            "publishAuthModes": self.publish_auth_modes,
            "channelNamespaceArn": self.channel_namespace_arn,
            "created": self.created,
            "lastModified": self.last_modified,
            "handlerConfigs": self.handler_configs,
        }

        if self.code_handlers:
            response["codeHandlers"] = self.code_handlers

        if self.backend:
            response["tags"] = self.backend.list_tags_for_resource(
                self.channel_namespace_arn
            )

        return response


class EventsAPI(BaseModel):
    def __init__(
        self,
        account_id: str,
        region: str,
        name: str,
        owner_contact: Optional[str],
        event_config: Optional[dict[str, Any]],
        backend: "AppSyncBackend",
    ) -> None:
        self.region = region
        self.name = name
        self.api_id = str(mock_random.get_random_string(length=26))
        self.owner_contact = owner_contact
        self.event_config = event_config

        self.api_arn = f"arn:{get_partition(self.region)}:appsync:{self.region}:{account_id}:apis/{self.api_id}"

        self.api_keys: dict[str, EventsAPIKey] = {}
        self.channel_namespaces: list[ChannelNamespace] = []

        dns_prefix = str(mock_random.get_random_string(length=26))
        self.dns = {
            "REALTIME": f"{dns_prefix}.appsync-realtime-api.{self.region}.amazonaws.com",
            "HTTP": f"{dns_prefix}.appsync-api.{self.region}.amazonaws.com",
        }

        self.created = datetime.now(timezone.utc).isoformat()

        self.backend = backend

    def to_json(self) -> dict[str, Any]:
        response = {
            "apiId": self.api_id,
            "name": self.name,
            "tags": self.backend.list_tags_for_resource(self.api_arn),
            "dns": self.dns,
            "apiArn": self.api_arn,
            "created": self.created,
            "eventConfig": self.event_config or {},  # Default to empty dict if None
        }

        if self.owner_contact:
            response["ownerContact"] = self.owner_contact

        return response

    def create_api_key(self, description: str, expires: Optional[int]) -> EventsAPIKey:
        api_key = EventsAPIKey(description, expires)
        self.api_keys[api_key.key_id] = api_key
        return api_key

    def list_api_keys(self) -> Iterable[EventsAPIKey]:
        return self.api_keys.values()

    def delete_api_key(self, api_key_id: str) -> None:
        self.api_keys.pop(api_key_id)

    def update_api_key(
        self, api_key_id: str, description: str, expires: Optional[int]
    ) -> EventsAPIKey:
        api_key = self.api_keys[api_key_id]
        api_key.update(description, expires)
        return api_key


# endregion


# region: AppSyncBackend
class AppSyncBackend(BaseBackend):
    """Implementation of AppSync APIs."""

    def __init__(self, region_name: str, account_id: str) -> None:
        super().__init__(region_name, account_id)
        self.graphql_apis: dict[str, GraphqlAPI] = {}
        self.events_apis: dict[str, EventsAPI] = {}
        self.tagger = TaggingService()

    def create_graphql_api(
        self,
        name: str,
        log_config: str,
        authentication_type: str,
        user_pool_config: str,
        open_id_connect_config: str,
        additional_authentication_providers: Optional[list[str]],
        xray_enabled: str,
        lambda_authorizer_config: str,
        tags: dict[str, str],
        visibility: str,
    ) -> GraphqlAPI:
        graphql_api = GraphqlAPI(
            account_id=self.account_id,
            region=self.region_name,
            name=name,
            authentication_type=authentication_type,
            additional_authentication_providers=additional_authentication_providers,
            log_config=log_config,
            xray_enabled=xray_enabled,
            user_pool_config=user_pool_config,
            open_id_connect_config=open_id_connect_config,
            lambda_authorizer_config=lambda_authorizer_config,
            visibility=visibility,
            backend=self,
        )
        self.graphql_apis[graphql_api.api_id] = graphql_api
        self.tagger.tag_resource(
            graphql_api.arn, TaggingService.convert_dict_to_tags_input(tags)
        )
        return graphql_api

    def update_graphql_api(
        self,
        api_id: str,
        name: str,
        log_config: str,
        authentication_type: str,
        user_pool_config: str,
        open_id_connect_config: str,
        additional_authentication_providers: Optional[list[str]],
        xray_enabled: str,
        lambda_authorizer_config: str,
    ) -> GraphqlAPI:
        graphql_api = self.graphql_apis[api_id]
        graphql_api.update(
            name,
            additional_authentication_providers,
            authentication_type,
            lambda_authorizer_config,
            log_config,
            open_id_connect_config,
            user_pool_config,
            xray_enabled,
        )
        return graphql_api

    def get_graphql_api(self, api_id: str) -> GraphqlAPI:
        if api_id not in self.graphql_apis:
            raise GraphqlAPINotFound(api_id)
        return self.graphql_apis[api_id]

    def get_graphql_schema(self, api_id: str) -> GraphqlSchema:
        graphql_api = self.get_graphql_api(api_id)
        if not graphql_api.graphql_schema:
            # When calling get_introspetion_schema without a graphql schema
            # the response GraphQLSchemaException exception includes InvalidSyntaxError
            # in the message. This might not be the case for other methods.
            raise GraphQLSchemaException(message="InvalidSyntaxError")
        return graphql_api.graphql_schema

    def delete_graphql_api(self, api_id: str) -> None:
        self.graphql_apis.pop(api_id)

    def list_graphql_apis(self) -> Iterable[GraphqlAPI]:
        """
        Pagination or the maxResults-parameter have not yet been implemented.
        """
        return self.graphql_apis.values()

    def create_api_key(
        self, api_id: str, description: str, expires: Optional[int]
    ) -> Union[GraphqlAPIKey, EventsAPIKey]:
        if api_id in self.graphql_apis:
            return self.graphql_apis[api_id].create_api_key(description, expires)
        else:
            return self.events_apis[api_id].create_api_key(description, expires)

    def delete_api_key(self, api_id: str, api_key_id: str) -> None:
        if api_id in self.graphql_apis:
            self.graphql_apis[api_id].delete_api_key(api_key_id)
        else:
            self.events_apis[api_id].delete_api_key(api_key_id)

    def list_api_keys(
        self, api_id: str
    ) -> Iterable[Union[GraphqlAPIKey, EventsAPIKey]]:
        """
        Pagination or the maxResults-parameter have not yet been implemented.
        """
        if api_id in self.graphql_apis:
            return self.graphql_apis[api_id].list_api_keys()
        elif api_id in self.events_apis:
            return self.events_apis[api_id].list_api_keys()
        else:
            return []

    def update_api_key(
        self,
        api_id: str,
        api_key_id: str,
        description: str,
        expires: Optional[int],
    ) -> Union[GraphqlAPIKey, EventsAPIKey]:
        if api_id in self.graphql_apis:
            return self.graphql_apis[api_id].update_api_key(
                api_key_id, description, expires
            )
        else:
            return self.events_apis[api_id].update_api_key(
                api_key_id, description, expires
            )

    def start_schema_creation(self, api_id: str, definition: str) -> str:
        self.graphql_apis[api_id].start_schema_creation(definition)
        return "PROCESSING"

    def get_schema_creation_status(self, api_id: str) -> Any:
        return self.graphql_apis[api_id].get_schema_status()

    def tag_resource(self, resource_arn: str, tags: dict[str, str]) -> None:
        self.tagger.tag_resource(
            resource_arn, TaggingService.convert_dict_to_tags_input(tags)
        )

    def untag_resource(self, resource_arn: str, tag_keys: list[str]) -> None:
        self.tagger.untag_resource_using_names(resource_arn, tag_keys)

    def list_tags_for_resource(self, resource_arn: str) -> dict[str, str]:
        return self.tagger.get_tag_dict_for_resource(resource_arn)

    def get_type(self, api_id: str, type_name: str, type_format: str) -> Any:
        return self.graphql_apis[api_id].get_type(type_name, type_format)

    def get_api_cache(self, api_id: str) -> APICache:
        if api_id not in self.graphql_apis:
            raise GraphqlAPINotFound(api_id)
        api_cache = self.graphql_apis[api_id].api_cache
        if api_cache is None:
            raise GraphqlAPICacheNotFound("get")
        return api_cache

    def delete_api_cache(self, api_id: str) -> None:
        if api_id not in self.graphql_apis:
            raise GraphqlAPINotFound(api_id)
        if self.graphql_apis[api_id].api_cache is None:
            raise GraphqlAPICacheNotFound("delete")
        self.graphql_apis[api_id].delete_api_cache()
        return

    def create_api_cache(
        self,
        api_id: str,
        ttl: int,
        api_caching_behavior: str,
        type: str,
        transit_encryption_enabled: Optional[bool] = None,
        at_rest_encryption_enabled: Optional[bool] = None,
        health_metrics_config: Optional[str] = None,
    ) -> APICache:
        if api_id not in self.graphql_apis:
            raise GraphqlAPINotFound(api_id)
        graphql_api = self.graphql_apis[api_id]
        if graphql_api.api_cache is not None:
            raise BadRequestException(message="The API has already enabled caching.")
        api_cache = graphql_api.create_api_cache(
            ttl,
            api_caching_behavior,
            type,
            transit_encryption_enabled,
            at_rest_encryption_enabled,
            health_metrics_config,
        )
        return api_cache

    def update_api_cache(
        self,
        api_id: str,
        ttl: int,
        api_caching_behavior: str,
        type: str,
        health_metrics_config: Optional[str] = None,
    ) -> APICache:
        if api_id not in self.graphql_apis:
            raise GraphqlAPINotFound(api_id)
        graphql_api = self.graphql_apis[api_id]
        if graphql_api.api_cache is None:
            raise GraphqlAPICacheNotFound("update")
        api_cache = graphql_api.update_api_cache(
            ttl, api_caching_behavior, type, health_metrics_config
        )
        return api_cache

    def flush_api_cache(self, api_id: str) -> None:
        if api_id not in self.graphql_apis:
            raise GraphqlAPINotFound(api_id)
        if self.graphql_apis[api_id].api_cache is None:
            raise GraphqlAPICacheNotFound("flush")
        return

    def create_api(
        self,
        name: str,
        owner_contact: Optional[str],
        tags: Optional[dict[str, str]],
        event_config: Optional[dict[str, Any]],
    ) -> EventsAPI:
        events_api = EventsAPI(
            account_id=self.account_id,
            region=self.region_name,
            name=name,
            owner_contact=owner_contact,
            event_config=event_config,
            backend=self,
        )

        self.events_apis[events_api.api_id] = events_api

        self.tagger.tag_resource(
            events_api.api_arn, TaggingService.convert_dict_to_tags_input(tags)
        )

        return events_api

    def list_apis(self) -> Iterable[EventsAPI]:
        """
        Pagination or the maxResults-parameter have not yet been implemented.
        """
        return self.events_apis.values()

    def delete_api(self, api_id: str) -> None:
        self.events_apis.pop(api_id)

    def create_channel_namespace(
        self,
        api_id: str,
        name: str,
        subscribe_auth_modes: list[dict[str, str]],
        publish_auth_modes: list[dict[str, str]],
        code_handlers: Optional[list[dict[str, Any]]] = None,
        tags: Optional[dict[str, str]] = None,
        handler_configs: Optional[dict[str, Any]] = None,
    ) -> ChannelNamespace:
        # Check if API exists
        if api_id not in self.events_apis:
            raise EventsAPINotFound(api_id)

        channel_namespace = ChannelNamespace(
            api_id=api_id,
            name=name,
            subscribe_auth_modes=subscribe_auth_modes,
            publish_auth_modes=publish_auth_modes,
            code_handlers=code_handlers,
            handler_configs=handler_configs,
            account_id=self.account_id,
            region=self.region_name,
            backend=self,
        )

        for api in self.events_apis.values():
            if api.api_id == api_id:
                api.channel_namespaces.append(channel_namespace)

        if tags:
            self.tagger.tag_resource(
                channel_namespace.channel_namespace_arn,
                TaggingService.convert_dict_to_tags_input(tags),
            )

        return channel_namespace

    def list_channel_namespaces(self, api_id: str) -> Iterable[ChannelNamespace]:
        if api_id not in self.events_apis:
            raise EventsAPINotFound(api_id)
        return self.events_apis[api_id].channel_namespaces

    def delete_channel_namespace(self, api_id: str, name: str) -> None:
        if api_id not in self.events_apis:
            raise EventsAPINotFound(api_id)
        for channel_namespace in self.events_apis[api_id].channel_namespaces:
            if channel_namespace.name == name:
                self.events_apis[api_id].channel_namespaces.remove(channel_namespace)
                return

    def get_api(self, api_id: str) -> EventsAPI:
        if api_id not in self.events_apis:
            raise EventsAPINotFound(api_id)
        return self.events_apis[api_id]


# endregion


appsync_backends = BackendDict(AppSyncBackend, "appsync")