typescriptgraphqlshopify-app

Trying to create a metafield for shopify app with gadget.dev


I`m really trying hard to create a metafield with an image list for my shopify app, but having a hard time.

I'm using typescript and this is my graphql request for the metafield creation:

  const mutation = `
    mutation {
      metafieldDefinitionCreate(definition: {
        name: "Variant Gallery",
        namespace: "custom",
        key: "variant_gallery",
        description: "A gallery of images associated with the variant",
        type: "list.file_reference",
        ownerType: PRODUCTVARIANT,
        access: {
          storefront: PUBLIC_READ
        }
      }) {
        createdDefinition {
          name
          namespace
          key
          type
          access 
        }
      }
    }
  `;

unfortunately I receive following error: Error creating metafield definition: RequestError: Field must have selections (field 'type' returns MetafieldDefinitionType but has no selections. Did you mean 'type { ... }'?)

I really cant find a way to make it work. Any suggestions?


Solution

  • That error happens because the type field is not a scalar but an object: https://shopify.dev/docs/api/admin-graphql/latest/objects/MetafieldDefinitionType

    You should specify which values you want from within that object. For instance if I wanted the name I would query it like so:

    const mutation = `
      mutation {
        metafieldDefinitionCreate(definition: {
          name: "Variant Gallery",
          namespace: "custom",
          key: "variant_gallery",
          description: "A gallery of images associated with the variant",
          type: "list.file_reference",
          ownerType: PRODUCTVARIANT,
          access: {
            storefront: PUBLIC_READ
          }
        }) {
          createdDefinition {
            name
            namespace
            key
            type { name }
            access 
          }
        }
      }
    `;