Skip to content

Commit

Permalink
Fixed fragments on interfaces being omitted from generated client.
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianCzajkowski committed Apr 18, 2024
1 parent 45b4a20 commit 05353b7
Show file tree
Hide file tree
Showing 15 changed files with 693 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added `ClientForwardRefsPlugin` to standard plugins.
- Re-added `model_rebuild` calls for input types with forward references.
- Fixed fragments on interfaces being omitted from generated client.


## 0.13.0 (2024-03-4)
Expand Down
24 changes: 22 additions & 2 deletions ariadne_codegen/client_generators/result_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,12 @@ def _resolve_selection_set(
fields.extend(sub_fields)
fragments = fragments.union(sub_fragments)
elif isinstance(selection, InlineFragmentNode):
if selection.type_condition.name.value == root_type:
root_type_value = self._get_inline_fragment_root_type(
selection.type_condition.name.value, root_type
)
if root_type_value:
sub_fields, sub_fragments = self._resolve_selection_set(
selection.selection_set, root_type
selection.selection_set, root_type_value
)
fields.extend(sub_fields)
fragments = fragments.union(sub_fragments)
Expand All @@ -337,6 +340,23 @@ def _resolve_selection_set(
)
return fields, fragments

def _get_inline_fragment_root_type(
self, selection_value: str, root_type: str
) -> Optional[str]:
type_ = self.schema.type_map.get(root_type)
if not type_:
return None

if isinstance(type_, GraphQLObjectType) and selection_value in {
interface.name for interface in type_.interfaces
}:
return selection_value

if selection_value == root_type:
return root_type

return None

def _unpack_fragment(
self,
fragment_def: FragmentDefinitionNode,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from .async_base_client import AsyncBaseClient
from .base_model import BaseModel, Upload
from .client import Client
from .exceptions import (
GraphQLClientError,
GraphQLClientGraphQLError,
GraphQLClientGraphQLMultiError,
GraphQLClientHttpError,
GraphQLClientInvalidResponseError,
)
from .fragments import Item, ItemError
from .my_mutation import (
MyMutation,
MyMutationChangeItem,
MyMutationChangeItemContacts,
MyMutationChangeItemErrorsItemServiceInternalError,
)

__all__ = [
"AsyncBaseClient",
"BaseModel",
"Client",
"GraphQLClientError",
"GraphQLClientGraphQLError",
"GraphQLClientGraphQLMultiError",
"GraphQLClientHttpError",
"GraphQLClientInvalidResponseError",
"Item",
"ItemError",
"MyMutation",
"MyMutationChangeItem",
"MyMutationChangeItemContacts",
"MyMutationChangeItemErrorsItemServiceInternalError",
"Upload",
]
Loading

0 comments on commit 05353b7

Please sign in to comment.