Repository: strawberry-graphql/strawberry

Issue:
Query Codegen does not work for mutations.
<!-- Provide a general summary of the bug in the title above. -->

<!--- This template is entirely optional and can be removed, but is here to help both you and us. -->
<!--- Anything on lines wrapped in comments like these will not show up in the final text. -->

## Describe the Bug

Query codegen seems to assume that it is only generating client objects for `query` objects and not `mutation` objects.  

The error seems to be in here:

https://github.com/strawberry-graphql/strawberry/blob/6cf8238c9cb11eda69a912e493c6a477b1d2a6be/strawberry/codegen/query_codegen.py#L258-L259

This assertion fails because the objects that are relevant are to be found via `self.schema.get_type_by_name("Mutation")` rather than `self.schema.get_type_by_name("Query")`.  A somewhat simple fix seems to be:

```py
query_type = self.schema.get_type_by_name(operation_definition.operation.value.title())
```

I have no idea if this is a proper fix for this bug, but later in the method, it seems like `mutation` and `subscription` are intended to be supported:

https://github.com/strawberry-graphql/strawberry/blob/6cf8238c9cb11eda69a912e493c6a477b1d2a6be/strawberry/codegen/query_codegen.py#L271-L274

<!-- A clear and concise description of what the bug is. -->

## System Information

 - Operating system:
 - Strawberry version (if applicable):

## Additional Context

<!-- Add any other relevant information about the problem here. -->


<!-- POLAR PLEDGE BADGE START -->
## Upvote & Fund

- We're using [Polar.sh](https://polar.sh/strawberry-graphql) so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.

<a href="https://polar.sh/strawberry-graphql/strawberry/issues/2792">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/strawberry-graphql/strawberry/issues/2792/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/strawberry-graphql/strawberry/issues/2792/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->



Discussion:

Resolving patch:
diff --git a/strawberry/codegen/query_codegen.py b/strawberry/codegen/query_codegen.py
--- a/strawberry/codegen/query_codegen.py
+++ b/strawberry/codegen/query_codegen.py
@@ -255,7 +255,9 @@ def _convert_directives(
     def _convert_operation(
         self, operation_definition: OperationDefinitionNode
     ) -> GraphQLOperation:
-        query_type = self.schema.get_type_by_name("Query")
+        query_type = self.schema.get_type_by_name(
+            operation_definition.operation.value.title()
+        )
         assert isinstance(query_type, TypeDefinition)
 
         assert operation_definition.name is not None


Test patch:
diff --git a/tests/codegen/conftest.py b/tests/codegen/conftest.py
--- a/tests/codegen/conftest.py
+++ b/tests/codegen/conftest.py
@@ -46,6 +46,10 @@ class Node:
 class BlogPost(Node):
     title: str
 
+    def __init__(self, id: str, title: str) -> None:
+        self.id = id
+        self.title = title
+
 
 @strawberry.type
 class Image(Node):
@@ -97,6 +101,13 @@ def with_inputs(self, id: Optional[strawberry.ID], input: ExampleInput) -> bool:
         return True
 
 
+@strawberry.type
+class Mutation:
+    @strawberry.mutation
+    def add_book(self, name: str) -> BlogPost:
+        return BlogPost(id="c6f1c3ce-5249-4570-9182-c2836b836d14", name=name)
+
+
 @pytest.fixture
 def schema() -> strawberry.Schema:
-    return strawberry.Schema(query=Query, types=[BlogPost, Image])
+    return strawberry.Schema(query=Query, mutation=Mutation, types=[BlogPost, Image])
diff --git a/tests/codegen/queries/mutation.graphql b/tests/codegen/queries/mutation.graphql
new file mode 100644
--- /dev/null
+++ b/tests/codegen/queries/mutation.graphql
@@ -0,0 +1,5 @@
+mutation addBook($input: String!) {
+  addBook(input: $input) {
+    id
+  }
+}
diff --git a/tests/codegen/snapshots/python/mutation.py b/tests/codegen/snapshots/python/mutation.py
new file mode 100644
--- /dev/null
+++ b/tests/codegen/snapshots/python/mutation.py
@@ -0,0 +1,8 @@
+class addBookResultAddBook:
+    id: str
+
+class addBookResult:
+    add_book: addBookResultAddBook
+
+class addBookVariables:
+    input: str
diff --git a/tests/codegen/snapshots/typescript/mutation.ts b/tests/codegen/snapshots/typescript/mutation.ts
new file mode 100644
--- /dev/null
+++ b/tests/codegen/snapshots/typescript/mutation.ts
@@ -0,0 +1,11 @@
+type addBookResultAddBook = {
+    id: string
+}
+
+type addBookResult = {
+    add_book: addBookResultAddBook
+}
+
+type addBookVariables = {
+    input: string
+}