Skip to content

GraphQL API

create_item(name)

Create a new item.

Parameters:

Name Type Description Default
name str

The name of the item.

required

Returns:

Type Description
dict

The created item.

Source code in templates/graphql/handler.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@app.resolver(type_name="Mutation", field_name="createItem")
@tracer.capture_method
def create_item(name: str) -> dict:
    """Create a new item.

    Args:
        name: The name of the item.

    Returns:
        The created item.
    """
    try:
        item = Item(name=name).dump()
        repository.put_item(item)
        return item
    except (ValidationError, Exception) as error:
        raise RuntimeError(f"Failed to create item with name '{name}'. Cause: {error}") from error

get_item(id)

Retrieve an item by ID.

Parameters:

Name Type Description Default
id str

The unique identifier of the item.

required

Returns:

Type Description
dict | None

The item if found, or None.

Source code in templates/graphql/handler.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@app.resolver(type_name="Query", field_name="getItem")
@tracer.capture_method
def get_item(id: str) -> dict | None:
    """Retrieve an item by ID.

    Args:
        id: The unique identifier of the item.

    Returns:
        The item if found, or None.
    """
    try:
        return repository.get_item(id)
    except Exception as error:
        raise RuntimeError(f"Failed to get item with ID '{id}'. Cause: {error}") from error

list_items()

Retrieve all items.

Returns:

Type Description
list[dict]

A list of items.

Source code in templates/graphql/handler.py
38
39
40
41
42
43
44
45
46
47
48
49
@app.resolver(type_name="Query", field_name="listItems")
@tracer.capture_method
def list_items() -> list[dict]:
    """Retrieve all items.

    Returns:
        A list of items.
    """
    try:
        return repository.list_items()
    except Exception as error:
        raise RuntimeError(f"Failed to list items. Cause: {error}") from error

main(event, context)

Lambda entry point for the AppSync resolver.

Parameters:

Name Type Description Default
event dict

The AppSync resolver event.

required
context LambdaContext

The Lambda execution context.

required

Returns:

Type Description
dict

The resolved data.

Source code in templates/graphql/handler.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@logger.inject_lambda_context(correlation_id_path=correlation_paths.APPSYNC_RESOLVER)
@tracer.capture_lambda_handler
@metrics.log_metrics
def main(event: dict, context: LambdaContext) -> dict:
    """Lambda entry point for the AppSync resolver.

    Args:
        event: The AppSync resolver event.
        context: The Lambda execution context.

    Returns:
        The resolved data.
    """
    return app.resolve(event, context)