Skip to content

Metafield value support or documentation #25

Open
@stevenpslade

Description

@stevenpslade

I found it confusing to use the shopify_function crate for the metafield data types being passed between Shopify and the Function.

One issue was the inability to preview / see the generated types, which I see an existing issue for: #17

Here is an example of a function API that sends metafields: https://shopify.dev/docs/api/functions/reference/order-routing-location-rule/graphql/common-objects/metafield

The metafield value is passed from Shopify to the function as a string and I found the support for this in shopify_function not clear.

When not using this crate and generating types myself in api.rs, I ended up doing something like this to handle the metafield format:

pub struct Metafield {
    pub value: Option<String>,
}

pub struct Configuration {
  pub value: Option<ExampleGroups>,
}

impl Configuration {
  fn from_str(value: &str) -> Self {
    // ExampleGroups::from_str constructs the JSON from the metafield string value
    Configuration { value: Some(ExampleGroups::from_str(&value)) }
  }

  fn default() -> Self {
    Configuration {
      value: None,
    }
  }
}

pub fn parse_function_configuration_value(metafield: Option<Metafield>) -> Configuration {
  match metafield.and_then(|m| m.value) {
    Some(value) => Configuration::from_str(&value),
    None => Configuration::default(),
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions