Skip to content

Add extern_enums and skip codegen for large enums - #56

Merged
jbourassa merged 2 commits into
mainfrom
jb/extern-enums
Mar 8, 2024
Merged

Add extern_enums and skip codegen for large enums#56
jbourassa merged 2 commits into
mainfrom
jb/extern-enums

Conversation

@jbourassa

Copy link
Copy Markdown
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.

@jbourassajbourassa changed the title Jb/extern enumsAdd extern_enums, apply it to large enumsMar 7, 2024
@jbourassajbourassa changed the title Add extern_enums, apply it to large enumsAdd extern_enums and skip codegen for large enumsMar 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
Copy Markdown
ContributorAuthor

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.

Comment threadexample_with_targets/README.md Outdated
Comment threadshopify_function_macro/src/lib.rs Outdated
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.
dphm
dphm approved these changes Mar 8, 2024

@dphmdphm left a comment

Copy link
Copy Markdown
Contributor

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?

Comment threadshopify_function/tests/shopify_function_target.rs Outdated
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
Copy Markdown
ContributorAuthor

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 mainMar 8, 2024
@jbourassa
jbourassa deleted the jb/extern-enums branch March 8, 2024 15:42
@jbourassajbourassa mentioned this pull request Mar 8, 2024
Sign up for freeto 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

@jbourassa@dphm@davejcameron