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

Add extern_enums and skip codegen for large enums #56

Merged
merged 2 commits into from
Mar 8, 2024
Merged

Conversation

jbourassa
Copy link
Contributor

Codegen for large enums bloat the Wasm binary size on top of producing not-very-efficient deserializing code.

This PR adds an extern_enums arg to the relevant macros. The extern_enums arg is forwarded to the graphql-client crate, which will skip codegen for those enums. The type is expected to already be defined, with e.g. type CountryCode = String;.

By default, the 3 large enums from Function APIs are defined as Strings and passed as extern_enums: LanguageCode, CountryCode, CurrencyCode. Users can override that by sending extern_enums explicitly.


Decisions:

  1. The syntax used is extern_enums = ["Foo", "Bar"] instead of graphql-client's extern_enums("Foo", "Bar"). I thought this was more aligned with the other attributes.
  2. Sane defaults: opt-out of hand-picked large enums only. Generated enums are useful in many cases (e.g. DiscountApplicationStrategy), and only problematic for large enums.
  3. I unified the diverging doc style between the README and the Rustdoc – one was using full sentence whereas the other didn't. I picked full sentence style to be more aligned with Shopify's GraphQL documentation.

Note: this PR is best reviewed commit-by-commit.

@jbourassa jbourassa changed the title Jb/extern enums Add extern_enums, apply it to large enums Mar 7, 2024
@jbourassa jbourassa changed the title Add extern_enums, apply it to large enums Add extern_enums and skip codegen for large enums Mar 7, 2024
Comment on lines +88 to +91
- `query_path`: A path to a GraphQL query, whose result will be used
as the input for the function invocation. The query MUST be named "Input".
- `schema_path`: A path to Shopify's GraphQL schema definition. Use the CLI
to download a fresh copy.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now the same copy as the macro's Rustdoc.

The `generate_types` macro didn't use `parse_macro_input!` to
parse its arguments, unlike other macros. This unifies arg parsing
across macros, making it easier to add a field to all macros.
Copy link
Contributor

@dphm dphm left a comment

Choose a reason for hiding this comment

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

Great! Love the macro parsing refactor 😃

What's the behaviour for an enum that doesn't exist?

shopify_function/tests/shopify_function_target.rs Outdated Show resolved Hide resolved
The `extern_enums` arg is forwarded to the `graphql-client` crate, which
will use it as the type for the enum instead of generating code for it.

By default, the 3 large enums from Function APIs are passed as
`extern_enums`: LanguageCode, CountryCode, CurrencyCode. Users can
override that by sending `extern_enums` explicitly.
@jbourassa
Copy link
Contributor Author

What's the behaviour for an enum that doesn't exist?

The behaviour of the underlying crate, which is:

  • sending a non-existing or non-queried GraphQL enum is a no-op: the implementation checks all queried enum against the extern_enums list.
  • sending a non-existing Rust type/enum will fail at compile time as the generated Rust code won't be valid.

@jbourassa jbourassa merged commit 74f49f6 into main Mar 8, 2024
4 checks passed
@jbourassa jbourassa deleted the jb/extern-enums branch March 8, 2024 15:42
@jbourassa jbourassa mentioned this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants