How can I create metafield definitions when installing an app?

dan-hrpr
Shopify Partner
4 0 2

How do I execute some graphql when the app is installed to create metafield definitions?

I have managed to get the query correct using the GraphiQL but it doesn't seem to run in the hooks.

 

This is my shopify.server.js

import { restResources } from "@shopify/shopify-api/rest/admin/2023-07";
hooks: {
    afterAuth: async ({ session }) => {
      shopify.registerWebhooks({ session });
      // Add a metafield definition called tier to the customer object.
      // This is used to store the tier of the customer.
      const response = await restResources.client.graphql({
        query: `
          mutation CreateMetafieldDefinition($definition: MetafieldDefinitionInput!) {
            metafieldDefinitionCreate(definition: $definition) {
              createdDefinition {
                id
                name
              }
              userErrors {
                field
                message
                code
              }
            }
          }
        `,
        variables: {
          definition: {
            // Provide the necessary input values for the mutation
            "name": "Tier",
            "namespace": "$app:loyalty-discount",
            "key": "loyalty_tier",
            "description": "Loyalty Tier",
            "type": "single_line_text_field",
            "ownerType": "CUSTOMER",
            "validations": [
              {
                "name": "choices",
                "value": "[\"Tier 1\", \"Tier 2\", \"Tier 3\, \"Tier 4\, \"Tier 5\"]"
              }
            ],
          },
        },
      });
    },
  },

Any pointers appreciated.

Thanks Dan

 

Replies 2 (2)

tyuzu
Shopify Partner
16 0 0

Hello, Is there other way to execute custom code after user accept the app installation ? or only in the "afterAuth" hook ?

tyuzu
Shopify Partner
16 0 0

You could directly access the admin from the arguments and execute graphl. Same as below, just make sure access scope for metafields and customer are permitted.

 

  hooks: {
    afterAuth: async ({ session, admin }) => {
      shopify.registerWebhooks({ session });
      const response: Response = await admin.graphql(
        `#graphql
        mutation CreateMetafieldDefinition($definition: MetafieldDefinitionInput!) {
          metafieldDefinitionCreate(definition: $definition) {
            createdDefinition {
              id
              name
            }
            userErrors {
              field
              message
              code
            }
          }
        }`,
        {
          variables: {
            definition: {
              // Provide the necessary input values for the mutation
              "name": "Tier",
              "namespace": "$app:loyalty-discount",
              "key": "loyalty_tier",
              "description": "Loyalty Tier",
              "type": "single_line_text_field",
              "ownerType": "CUSTOMER",
              "validations": [
                {
                  "name": "choices",
                  "value": "[\"Tier 1\", \"Tier 2\", \"Tier 3\, \"Tier 4\, \"Tier 5\"]"
                }
              ],
            },
          }
        }
    );

    const responseJson = await response.json();
    console.log("responseJson", responseJson)
    },
  },