Shopify Admin API GraphQL查询报错:Field 'customerByIdentifier' doesn't exist on type 'QueryRoot' 求助
Shopify Admin API GraphQL查询报错:Field 'customerByIdentifier' doesn't exist on type 'QueryRoot' 求助
哈喽,我来帮你捋捋这个问题——你遇到的这个情况其实挺典型的,大概率是API版本不匹配导致的,咱们一步步拆解:
先理清楚你的现状
你现在的后端Loader代码是这样的:
export const loader = async ({ request }: LoaderFunctionArgs) => { await authenticate.admin(request); const admin = await authenticate.admin(request); const email = "some@mail.com"; const response = await admin.admin.graphql( `#graphql query GetCustomers($query: String!) { customerByIdentifier(identifier: {emailAddress: $query}) { id } } `, { variables: { query: email } } ); const data = await response.json(); console.log(">>>>>> data", data, "<<<<<<<"); return json({ apiKey: process.env.SHOPIFY_API_KEY || "" }); };
你运行这段代码时,前端会收到错误:Error: Field “customerByIdentifier” doesn't exist on type “QueryRoot”,但在Shopify官方的GraphQL App里,用几乎一样的查询:
{ customerByIdentifier(identifier: {emailAddress: "some@mail.com"}) { id } }
却能正常返回用户信息,这就很矛盾对吧?
问题的核心原因
customerByIdentifier是Shopify Admin GraphQL API的新字段,它是在2024-07的API版本里才正式上线的。而Shopify GraphQL App默认会使用最新的API版本,所以你在App里测试没问题,但如果你的代码里配置的API版本低于2024-07,就会出现“字段不存在”的报错——因为旧版本的QueryRoot里确实没有这个字段。
解决办法
你只需要把你的代码里的Shopify API版本升级到2024-07或者更高版本就行:
- 如果你用的是Hydrogen框架或者Shopify CLI开发的,找到你的配置文件(比如
shopify.server.js或者app/shopify.server.ts),把apiVersion参数设置为"2024-07" - 要是你是直接调用Admin API的话,确保请求URL里带上
?api_version=2024-07的参数 - 另外,你的代码里重复调用了两次
await authenticate.admin(request);,其实只需要调用一次,把结果存下来复用就行,这是个小优化:
// 优化后的代码片段 const admin = await authenticate.admin(request); // 删掉前面那行多余的await authenticate.admin(request);
验证
修改完API版本后,重新启动你的服务,再执行查询应该就能正常拿到用户的id了。如果还是有问题,可以检查下你的Admin API客户端有没有read_customers的权限——不过你在Shopify App里能成功,权限应该是没问题的。
备注:内容来源于stack exchange,提问作者Yaroslav
相关产品推荐
相关产品推荐

