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

Use graphql_client_codegen with output query string #51

Merged
merged 2 commits into from
Apr 19, 2024

Conversation

dphm
Copy link
Contributor

@dphm dphm commented Jan 11, 2024

Closes https://github.com/Shopify/script-service/issues/7474

  • For generate_types and shopify_function_target macros:
    • Generates pub struct Output using graphql_client_codegen::generate_module_token_stream_from_string using the output query string, instead of writing the query to a temporary .output.graphql file
    • Generates pub struct Input using graphql_client_codegen::generate_module_token_stream as it's still using a file path

No change to the interface, internally doesn't require the .output.graphql file anymore.

Depends on

@dphm dphm force-pushed the graphql-client-codegen/query branch 2 times, most recently from 3758f94 to 8642e38 Compare January 11, 2024 21:18
@dphm dphm requested review from surma and adampetro January 11, 2024 21:23
@dphm dphm force-pushed the graphql-client-codegen/query branch 2 times, most recently from 6faed75 to d92765a Compare January 11, 2024 21:39
)?;
let expected = crate::target_a::output::FunctionTargetAResult { status: Some(200) };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

📝 Alternative to changing these tests to assert JSON output is to make this result type public.

Error:

d@Delta-Shopify shopify-function-rust % cargo t
warning: virtual workspace defaulting to `resolver = "1"` despite one or more workspace members being on edition 2021 which implies `resolver = "2"`
note: to keep the current resolver, specify `workspace.resolver = "1"` in the workspace root's manifest
note: to use the edition 2021 resolver, specify `workspace.resolver = "2"` in the workspace root's manifest
note: for more details see https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
   Compiling shopify_function_macro v0.5.0 (/Users/d/src/github.com/Shopify/shopify-function-rust/shopify_function_macro)
   Compiling shopify_function v0.5.0 (/Users/d/src/github.com/Shopify/shopify-function-rust/shopify_function)
   Compiling example_with_targets v1.0.0 (/Users/d/src/github.com/Shopify/shopify-function-rust/example_with_targets)
   Compiling example v1.0.0 (/Users/d/src/github.com/Shopify/shopify-function-rust/example)
error[E0603]: module `output` is private
  --> example_with_targets/src/tests.rs:16:37
   |
16 |     let expected = crate::target_a::output::FunctionTargetAResult { status: Some(200) };
   |                                     ^^^^^^  --------------------- struct `FunctionTargetAResult` is not publicly re-exported
   |                                     |
   |                                     private module
   |
note: the module `output` is defined here
  --> example_with_targets/src/lib.rs:4:1
   |
4  | / #[shopify_function_target(
5  | |     // Implicit target = "example.target-a"
6  | |     // Implicit generated module name = "target_a"
7  | |     query_path = "a.graphql",
8  | |     schema_path = "schema.graphql"
9  | | )]
   | |__^
   = note: this error originates in the attribute macro `shopify_function_target` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0603]: module `output` is private
  --> example_with_targets/src/tests.rs:32:34
   |
32 |     let expected = crate::mod_b::output::FunctionTargetBResult {
   |                                  ^^^^^^  --------------------- struct `FunctionTargetBResult` is not publicly re-exported
   |                                  |
   |                                  private module
   |
note: the module `output` is defined here
  --> example_with_targets/src/lib.rs:16:1
   |
16 | / #[shopify_function_target(
17 | |     // Explicit target if function name does not match target handle
18 | |     target = "example.target-b",
19 | |     // Override generated module name
...  |
22 | |     schema_path = "schema.graphql"
23 | | )]
   | |__^
   = note: this error originates in the attribute macro `shopify_function_target` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0603`.
error: could not compile `example_with_targets` (lib test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe export a test helper function to assert expected JSON string?

Copy link
Contributor

Choose a reason for hiding this comment

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

What are the reasons not to make it public?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I could do that 😆


## Usage

* The [`generate_types`] macro allows you to generate structs based on your [input query]. It will also generate output/response types for the current Function API, based on the provided schema.
* It will automatically generate an `.output.graphql` file for code generation purposes. This file can be added to your `.gitignore`.
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 the relevant non-formatting change in the file.

@dphm dphm force-pushed the graphql-client-codegen/query branch from d92765a to 26e0ca4 Compare January 12, 2024 15:30
@dphm dphm force-pushed the graphql-client-codegen/query branch from 26e0ca4 to 126105f Compare January 12, 2024 21:05
Cargo.lock Outdated Show resolved Hide resolved
Cargo.lock Outdated Show resolved Hide resolved
@dphm dphm force-pushed the graphql-client-codegen/query branch from 126105f to 2402d95 Compare March 4, 2024 15:25
Copy link
Member

@surma surma left a comment

Choose a reason for hiding this comment

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

Nice job. No more temporary, hidden files 🎉

@dphm dphm force-pushed the graphql-client-codegen/query branch 2 times, most recently from 414adaf to 46dd55b Compare April 10, 2024 17:01
"Output",
&output_query_file_name,
schema_path.value().as_str(),
extern_enums.as_deref(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jbourassa Does the output struct need extern enums as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally, yes: if the output struct wants e.g. CountryCode, we don't want to bloat the binary size and have mismatching definitions for CountryCode.

@dphm dphm force-pushed the graphql-client-codegen/query branch from 46dd55b to 21de3ec Compare April 10, 2024 17:03
@dphm dphm force-pushed the graphql-client-codegen/query branch from 129ba9b to 307b0ee Compare April 10, 2024 17:12
@dphm dphm marked this pull request as ready for review April 10, 2024 17:19
@dphm dphm requested a review from jbourassa April 10, 2024 17:20
)?;
let expected = crate::target_a::output::FunctionTargetAResult { status: Some(200) };
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the reasons not to make it public?

Comment on lines -399 to -402
///
/// Note: This macro creates a file called `.output.graphql` in the root
/// directory of the project. It can be safely added to your `.gitignore`. We
/// hope we can avoid creating this file at some point in the future.
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

@dphm
Copy link
Contributor Author

dphm commented Apr 19, 2024

For the sake of time I'm going to merge this and file an issue to consider making the output structs public if someone else wants to do it 😆

@dphm dphm merged commit ede17f4 into main Apr 19, 2024
4 checks passed
@dphm dphm deleted the graphql-client-codegen/query branch April 19, 2024 14:36
@adampetro adampetro mentioned this pull request May 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.

4 participants