首页 > 解决方案 > GraphQL 错误:String 类型的变量 $customerAccessToken!提供了无效值

问题描述

我正在尝试使用用户登录 Shopify 时提供的 customerAccessToken 恢复客户。

使用 Apollo,这是我的代码:

this.apollo.mutate({
  mutation: getCustomerFromToken,
  variables: {
    input: {
      customerAccessToken: '217b9a6952c28eb4db376487a6301294' // Also tried btoa('217b9a6952c28eb4db376487a6301294')
    }
  },
})

这是我的 GraphQL 查询:

query getCustomerFromToken($customerAccessToken: String!) {
  customer(customerAccessToken: $customerAccessToken) {
    id
    addresses(first: 5) {
      edges {
        node {
        address1
        address2
        company
        city
        province
        zip
        country
        phone
        }
      }
    }
    orders(first: 200) {
      edges {
        cursor
        node {
          id
          totalPriceV2 {
            amount
            currencyCode
          }
          processedAt
          orderNumber
        }
      }
    }
  }
}

这是我用来从 Shopify 检索 accessToken 的登录 GraphQL 代码:

mutation customerAccessTokenCreate($input: CustomerAccessTokenCreateInput!) {
  customerAccessTokenCreate(input: $input) {
    customerAccessToken {
      accessToken
      expiresAt
    }
    customerUserErrors {
      code
      field
      message
    }
  }
}

标签: graphqlshopifyshopify-api

解决方案


我的问题是两个方面。

  1. 我在查询端点上使用了突变
  2. 查询不使用有效负载中的输入
const payload = {
    customerAccessToken: "..."
}

// NOT

const payload = {
  input: {
    customerAccessToken: "..."
  }
}

推荐阅读