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

CLI code generation error when the server schema has types with long names #3131

Closed
oliveira-marcio opened this issue Jul 20, 2023 · 6 comments
Labels
bug Generally incorrect behavior

Comments

@oliveira-marcio
Copy link

Summary

After downloading the server's schema with the Apollo CLI, I'm facing this issue when running the generate command from the CLI:

$ ./Pods/Apollo/apollo-ios-cli generate   
Error: cannotCreateFile(at: "/Users/moliveira/ios-apps/iOSApp/LetsGetChecked/ResultsNarrativesAPI/Schema/Objects/ContainerColumnsOrCallToActionBannerOrContainerSectionTwoColumnOrFaqOrSectionComparisonTableOrSectionGridOrSectionHeroOrSectionIconCarouselOrSectionLogosOrSectionOneColumnOrSectionProductCardsOrSectionReviewsOrSectionStepsOrSectionTabbedInformationOrSectionTestimonialCarouselOrSectionTrustpilot.graphql.swift")

Other files from schema were properly created with the CLI command but this one failed because this object name has more than 255 characters (the MacOS limitation for file names).

Version

1.3.1

Steps to reproduce the behavior

  1. Run the CLI fetch-schema command to download the server schema (schema.graphqls) with some long object inside. Example:
type ContainerColumnsOrCallToActionBannerOrContainerSectionTwoColumnOrFaqOrSectionComparisonTableOrSectionGridOrSectionHeroOrSectionIconCarouselOrSectionLogosOrSectionOneColumnOrSectionProductCardsOrSectionReviewsOrSectionStepsOrSectionTabbedInformationOrSectionTestimonialCarouselOrSectionTrustpilot implements Entry {
  sys: Sys!
  contentfulMetadata: ContentfulMetadata!
  linkedFrom(allowedLocales: [String]): ContainerColumnsOrCallToActionBannerOrContainerSectionTwoColumnOrFaqOrSectionComparisonTableOrSectionGridOrSectionHeroOrSectionIconCarouselOrSectionLogosOrSectionOneColumnOrSectionProductCardsOrSectionReviewsOrSectionStepsOrSectionTabbedInformationOrSectionTestimonialCarouselOrSectionTrustpilotLinkingCollections
  internalName(locale: String): String
}
  1. Run the CLI generate command with a .graphql operation file in the same downloaded schema file folder

Logs

Error: cannotCreateFile(at: "/Users/moliveira/ios-apps/iOSApp/LetsGetChecked/ResultsNarrativesAPI/Schema/Objects/ContainerColumnsOrCallToActionBannerOrContainerSectionTwoColumnOrFaqOrSectionComparisonTableOrSectionGridOrSectionHeroOrSectionIconCarouselOrSectionLogosOrSectionOneColumnOrSectionProductCardsOrSectionReviewsOrSectionStepsOrSectionTabbedInformationOrSectionTestimonialCarouselOrSectionTrustpilot.graphql.swift")

Anything else?

No response

@oliveira-marcio oliveira-marcio added bug Generally incorrect behavior needs investigation labels Jul 20, 2023
@oliveira-marcio oliveira-marcio changed the title Cannot generate some collections from the server schema with long names CLI code generation error when the server schema has types with long names Jul 20, 2023
@drjackyl
Copy link

I have the very same problem with types created by Contentful. Those are ridiculously long and I would also like to make Contentful aware of this.

Fortunately, I don't need those types for my queries and I can manually remove them from the schema.

I couldn't find an option to exclude types from the schema in the apollo config, that would help me already. But for those who need to use those types, it's a bad situation.

@calvincestari
Copy link
Member

Hi 👋🏻 - I don't really have a good answer to this other than the type should be renamed.

I think Apollo Kotlin suffers the same problem but their codegen engine has the ability to include/exclude types based on a regex pattern; Apollo iOS doesn't have that feature. I don't think adding filename length logic to our codegen engine is appropriate because that may cause other issues when the directory paths exceed 255 characters causing the file to be created somewhere else entirely; my answer in that case would be similarly to trim down the directory paths.

A local script to rename the type before codegen could be used but that's tedious and would have to be done before each run of code generation.

I couldn't find an option to exclude types from the schema in the apollo config, that would help me already. But for those who need to use those types, it's a bad situation.

Yes, when those types are being used the only option is to rename them them. A custom directive would be particularly useful here.

@oliveira-marcio
Copy link
Author

oliveira-marcio commented Jul 21, 2023

I was in touch with Contentful support today and they said they just released a fix today to handle those long names (I couldn't test yet because the space I'm consuming doesn't belong to my team so I couldn't refresh the schema yet). Still, these long names are generated by Contentful itself when building the server schema to download. It's not something we can just rename. Anyway, can't the CLI detect those large names objects during the code generation and try to truncate (or ask for the user suggestion to change) only the file name (since this is a MacOS limitation) but keep the object name inside as it is (since Swift doesn't have issues with large names for objects)? I think this might mitigate the problem.

@calvincestari
Copy link
Member

We're deliberating about this within the team but IMO a custom directive is still the correct way to solve this and a few other use cases too.

@calvincestari calvincestari added this to the Minor Releases (1.x) milestone Jul 27, 2023
@calvincestari
Copy link
Member

We've decided as a team that a custom directive to rename a schema type is the preferred solution.

I've added it to our Minor Releases milestone but it has no particular delivery date attached to it. We're always willing to work with community contributions if a feature is needed sooner than we can get to implementing it.

@AnthonyMDev
Copy link
Contributor

We're going to track this feature in #3283 which we hope to begin work on shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Generally incorrect behavior
Projects
None yet
Development

No branches or pull requests

5 participants
@calvincestari @drjackyl @AnthonyMDev @oliveira-marcio and others