Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not generate a convenience initializer for mock objects without fields #2634

Merged
merged 1 commit into from
Nov 3, 2022

Conversation

Gois
Copy link
Contributor

@Gois Gois commented Nov 2, 2022

We encountered a problem during testing with the mocks, some objects don't have fields except for their __typename. In that case the cli would still generate a convenience initializer that would call self.init() which causes infinite recursion.

This has been fixed by checking if the fields are empty or not.

@netlify
Copy link

netlify bot commented Nov 2, 2022

👷 Deploy request for apollo-ios-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit b22ad15

@calvincestari
Copy link
Member

Hi @Gois 👋🏻 - thanks for the contribution.

Do you have a GraphQL example you can share, I'm intrigued at the practical use of an object without fields?

@Gois
Copy link
Contributor Author

Gois commented Nov 3, 2022

Hi @calvincestari, sure I can elaborate a bit more. In our case we use it for example like this in a query:

... on CategoryPicker {
    __typename
}

Based on that type (if we get it back) in our response we know that we have to build up a certain UI element. So in Swift we check it like this:

if child?.asCategoryPicker != nil {
    // code for adding UI element
}

In this case we don't have any fields in our schema for this type, so that's why it only has the typename field and we ran into the problem with the convenience initializer.

@calvincestari
Copy link
Member

Got it, thanks for sharing! I'll get this PR merged today.

Copy link
Member

@calvincestari calvincestari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@calvincestari
Copy link
Member

Codegen CLI Integration Tests are failing because of a CocoaPods error with forks - merging anyways..

@calvincestari calvincestari merged commit cc56fc6 into apollographql:main Nov 3, 2022
@calvincestari calvincestari changed the title Do not generate a convience initializer for objects without fields Do not generate a convenience initializer for mock objects without fields Nov 3, 2022
@calvincestari calvincestari mentioned this pull request Nov 18, 2022
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants