【问题标题】:Error: GraphQL error: Field 'metafieldsSet' doesn't exist on type 'Mutation' - Shopify GraphQL error错误:GraphQL 错误:“Mutation”类型上不存在字段“metafieldsSet”-Shopify GraphQL 错误
【发布时间】:2021-12-03 14:52:32
【问题描述】:

我最近一直在玩 Shopify 应用程序开发,我正在努力使用 graphql 调用来修改一些文本。下图显示了在我测试它的 shopify GraphQL 应用程序中正确进行的调用。

但是,当我尝试从 react 组件进行同样的调用时,我收到以下错误

Unhandled Runtime Error
Error: GraphQL error: Field 'metafieldsSet' doesn't exist on type 'Mutation'
GraphQL error: Variable $metafields is declared by metafieldsSet but not used

这是我尝试更新元字段的反应组件

import React, { useState } from 'react';
import gql from 'graphql-tag';
import { Mutation } from 'react-apollo';
import { Layout, Button, Banner, Toast, Stack, Frame } from '@shopify/polaris';
import { Context } from '@shopify/app-bridge-react';
// GraphQL mutation that updates the prices of products
const UPDATE_TEXT = gql`mutation metafieldsSet($metafields: [MetafieldsSetInput!]!) {
    metafieldsSet(metafields: $metafields) {
      metafields {
        value
        id
        key
        ownerType
     
      }
      userErrors {
        field
        message
      }
    }
  }
`;
class UpdateTheText extends React.Component{
    static contextType = Context;
    render(){
        return(
            <Mutation mutation={UPDATE_TEXT}>
            {(handleSubmit, {error, data}) => {
              const [hasResults, setHasResults] = useState(false);
    
              const showError = error && (
                <Banner status="critical">{error.message}</Banner>
              );
    
              const showToast = hasResults && (
                <Toast
                  content="Successfully updated"
                  onDismiss={() => setHasResults(false)}
                />
              );
    
              return (
                <Frame>
                  {showToast}
                  <Layout.Section>
                    {showError}
                  </Layout.Section>
    
                  <Layout.Section>
                    <Stack distribution={"center"}>
                      <Button
                        primary
                        textAlign={"center"}
                        onClick={() => {
                          let promise = new Promise((resolve) => resolve());
                            const newmetafields = {
                                  key: "ExtraShopDesc",
                                  namespace: "ExtraShopDescription",
                                  ownerId: "gid://shopify/Shop/55595073672",
                                  type: "single_line_text_field",
                                  value: "This is an extra shop description twice"
                              }
    
                            promise = promise.then(() => handleSubmit({ variables: { metafields: newmetafields }}));
                          
    
                          if (promise) {
                            promise.then(() => this.props.onUpdate().then(() => setHasResults(true)));
                        }}
                      }
                      >
                        Update Text
                      </Button>
                    </Stack>
                  </Layout.Section>
                </Frame>
              );
            }}
          </Mutation>
        );
    }

}

export default UpdateTheText;

我相信我正在将变量传递给 gql,但它似乎没有接收到它们?

【问题讨论】:

    标签: graphql shopify-app


    【解决方案1】:

    叹息,

    这一直是 API 版本问题。 Shopify CLI 仍在启动 Oct 2020 API。 Metafieldset 仅在 2021 API 中添加

    https://shopify.dev/api/admin-graphql/2021-10/mutations/metafieldsset

    错误消息让我失望

    所以要更新只需更新 server.js 中的 API 版本

    API_VERSION: "2021-10",
    

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 2020-10-26
      • 2020-06-26
      • 2019-09-29
      • 2019-05-08
      • 2021-04-14
      • 2018-09-06
      • 2020-01-15
      • 2020-01-09
      相关资源
      最近更新 更多