-
Notifications
You must be signed in to change notification settings - Fork 10
Use graphql_client_codegen with output query string #51
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,57 +4,57 @@ A crate to help developers build [Shopify Functions]. | |
|
|
||
| ## Dependencies | ||
|
|
||
| * Make sure you have `graphql_client` in your dependencies | ||
| - Make sure you have `graphql_client` in your dependencies | ||
|
|
||
| ``` | ||
| cargo add graphql_client@0.13.0 | ||
| ``` | ||
| ``` | ||
| cargo add graphql_client@0.14.0 | ||
| ``` | ||
|
|
||
| ## 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`. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the relevant non-formatting change in the file. |
||
| * The [`shopify_function`] attribute macro marks the following function as the entry point for a Shopify Function. It manages the Functions `STDIN` input parsing and `STDOUT` output serialization for you. | ||
| * The [`run_function_with_input`] function is a utility for unit testing which allows you to quickly add new tests based on a given JSON input string. | ||
| - 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. | ||
| - The [`shopify_function`] attribute macro marks the following function as the entry point for a Shopify Function. It manages the Functions `STDIN` input parsing and `STDOUT` output serialization for you. | ||
| - The [`run_function_with_input`] function is a utility for unit testing which allows you to quickly add new tests based on a given JSON input string. | ||
|
|
||
| See the [example] for details on usage, or use the following guide to convert an existing Rust-based function. | ||
|
|
||
| ## Updating an existing function to use `shopify_function` | ||
|
|
||
| 1. `cargo add shopify_function` | ||
| 1. `cargo add graphql_client@0.13.0` | ||
| 1. `cargo add graphql_client@0.14.0` | ||
| 1. Delete `src/api.rs`. | ||
| 1. In `main.rs`: | ||
| 1. Add imports for `shopify_function`. | ||
|
|
||
| ```rust | ||
| use shopify_function::prelude::*; | ||
| use shopify_function::Result; | ||
| ``` | ||
| 1. Add imports for `shopify_function`. | ||
|
|
||
| 1. Remove references to `mod api`. | ||
| 1. Add type generation, right under your imports. | ||
| ```rust | ||
| use shopify_function::prelude::*; | ||
| use shopify_function::Result; | ||
| ``` | ||
|
|
||
| ```rust | ||
| generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql"); | ||
| ``` | ||
| 1. Remove references to `mod api`. | ||
| 1. Add type generation, right under your imports. | ||
|
|
||
| 1. Remove the `main` function entirely. | ||
| 1. Attribute the `function` function with the `shopify_function` macro, and change its return type. | ||
| ```rust | ||
| generate_types!(query_path = "./input.graphql", schema_path = "./schema.graphql"); | ||
| ``` | ||
|
|
||
| ```rust | ||
| #[shopify_function] | ||
| fn function(input: input::ResponseData) -> Result<output::FunctionResult> { | ||
| ``` | ||
| 1. Remove the `main` function entirely. | ||
| 1. Attribute the `function` function with the `shopify_function` macro, and change its return type. | ||
|
|
||
| 1. Update the types and fields utilized in the function to the new, auto-generated structs. For example: | ||
| | Old | New | | ||
| | --- | --- | | ||
| | `input::Input` | `input::ResponseData` | | ||
| | `input::Metafield` | `input::InputDiscountNodeMetafield` | | ||
| | `input::DiscountNode` | `input::InputDiscountNode` | | ||
| | `FunctionResult` | `output::FunctionResult` | | ||
| | `DiscountApplicationStrategy::First` | `output::DiscountApplicationStrategy::FIRST` | | ||
| ```rust | ||
| #[shopify_function] | ||
| fn function(input: input::ResponseData) -> Result<output::FunctionResult> { | ||
| ``` | ||
|
|
||
| 1. Update the types and fields utilized in the function to the new, auto-generated structs. For example: | ||
| | Old | New | | ||
| | --- | --- | | ||
| | `input::Input` | `input::ResponseData` | | ||
| | `input::Metafield` | `input::InputDiscountNodeMetafield` | | ||
| | `input::DiscountNode` | `input::InputDiscountNode` | | ||
| | `FunctionResult` | `output::FunctionResult` | | ||
| | `DiscountApplicationStrategy::First` | `output::DiscountApplicationStrategy::FIRST` | | ||
|
|
||
| 1. Add `.output.graphql` to your `.gitignore`. | ||
|
|
||
|
|
@@ -69,6 +69,7 @@ cargo doc --open | |
| You can also use the [cargo-expand](https://github.com/dtolnay/cargo-expand) crate to view the generated source, or use the [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer) VSCode extension to get [IntelliSense](https://code.visualstudio.com/docs/editor/intellisense) for Rust and the generated types. | ||
|
|
||
| --- | ||
|
|
||
| License Apache-2.0 | ||
|
|
||
| [Shopify Functions]: https://shopify.dev/api/functions | ||
|
|
||
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😆