Error: GraphQL error: Field 'metafieldsSet' doesn't exist on type 'Mutation' - Shopify GraphQL error
Error: GraphQL error: Field 'metafieldsSet' doesn't exist on type 'Mutation' - Shopify GraphQL error
我最近一直在玩 Shopify 应用程序开发,我正在努力处理 graphql 调用来修改一些文本。下图显示了在我测试它的 shopify GraphQL 应用程序中正确进行的调用。
然而,当我尝试从反应组件进行相同的调用时,我收到以下错误
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,但它似乎没有接收到它们?
感叹,
这一直是一个 API 版本问题。 Shopify CLI 仍在 2020 年 10 月启动 API。 Metafieldset 仅在 2021 年添加 API
https://shopify.dev/api/admin-graphql/2021-10/mutations/metafieldsset
错误信息让我很反感
所以要更新只需更新 server.js
中的 API 版本
API_VERSION: "2021-10",
我最近一直在玩 Shopify 应用程序开发,我正在努力处理 graphql 调用来修改一些文本。下图显示了在我测试它的 shopify GraphQL 应用程序中正确进行的调用。
然而,当我尝试从反应组件进行相同的调用时,我收到以下错误
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,但它似乎没有接收到它们?
感叹,
这一直是一个 API 版本问题。 Shopify CLI 仍在 2020 年 10 月启动 API。 Metafieldset 仅在 2021 年添加 API
https://shopify.dev/api/admin-graphql/2021-10/mutations/metafieldsset
错误信息让我很反感
所以要更新只需更新 server.js
中的 API 版本API_VERSION: "2021-10",