看起来 apollo Client 不会查询 ID 为 . (点)在他们使用 Next JS 和 Sanity CMS with GraphQL

Looks like apolloClient does not query entries with id that has . (dot) in them using NextJS and SanityCMS with GraphQL

我有一个带有一些数据和本地化(翻译)的 SanityCMS,当创建文档时,它们会得到一个类似于 i18n.b4d654f7-2b47-4339-9a92-d9ad5d111b43.no 的 ID,其中最后一个分隔符是 locale/language.

当我使用命令 sanity graphql deploy 时,我得到了一个 api 部署的 playground 来测试 GraphQL 查询。这个游乐场运行良好,显示了我想看到的一切。

但我在 NextJS 中有一个前端,我使用 ApolloClient 与 Sanity 部署的 api 进行通信。我使用为 ApolloClient 创建 playground/api 时创建的相同 URI,但出于某些奇怪的原因,它只获取没有任何点分隔的 ID

一个基本的测试查询如下所示:

query test {
    allPage {
      title
      i18n_lang
      _id
    }
  }

这是从 apolloclient 中获取的数据(你只能看到没有任何点分隔的 _id):

{
  allPage: [
    {
      __typename: 'Page',
      title: 'Men',
      i18n_lang: 'en',
      _id: '14df2893-d9e5-439d-a966-d41371085cf8'
    },
    {
      __typename: 'Page',
      title: 'Startpage - en',
      i18n_lang: 'en',
      _id: '179f7877-61c8-44be-9c0b-c5abb631c4d7'
    },
    {
      __typename: 'Page',
      title: 'Women',
      i18n_lang: 'en',
      _id: '40f6584c-1a1c-4574-8837-cfd9e9d062c7'
    },
    {
      __typename: 'Page',
      title: 'Badetøy',
      i18n_lang: 'en',
      _id: '49efb30f-f6e9-4aff-9d7f-944972d3a276'
    },
    {
      __typename: 'Page',
      title: 'Shoes',
      i18n_lang: 'en',
      _id: '9f9d1c7e-f983-4608-a066-327de0f5241e'
    },
    {
      __typename: 'Page',
      title: 'Sneakerss',
      i18n_lang: 'en',
      _id: 'b4d654f7-2b47-4339-9a92-d9ad5d111b43'
    },
    {
      __typename: 'Page',
      title: 'test',
      i18n_lang: null,
      _id: 'c8a744b1-79f2-48e5-9fec-7be13a8af9df'
    }
  ]
}

这是playground的数据(这里可以清楚的看到其他id):

{
  "data": {
    "allPage": [
      {
        "title": "Men",
        "i18n_lang": "en",
        "_id": "14df2893-d9e5-439d-a966-d41371085cf8"
      },
      {
        "title": "Startpage - en",
        "i18n_lang": "en",
        "_id": "179f7877-61c8-44be-9c0b-c5abb631c4d7"
      },
      {
        "title": "Women",
        "i18n_lang": "en",
        "_id": "40f6584c-1a1c-4574-8837-cfd9e9d062c7"
      },
      {
        "title": "Badetøy",
        "i18n_lang": "en",
        "_id": "49efb30f-f6e9-4aff-9d7f-944972d3a276"
      },
      {
        "title": "Shoes",
        "i18n_lang": "en",
        "_id": "9f9d1c7e-f983-4608-a066-327de0f5241e"
      },
      {
        "title": "Sneakerss",
        "i18n_lang": "en",
        "_id": "b4d654f7-2b47-4339-9a92-d9ad5d111b43"
      },
      {
        "title": "test",
        "i18n_lang": null,
        "_id": "c8a744b1-79f2-48e5-9fec-7be13a8af9df"
      },
      {
        "title": "Startpage - en",
        "i18n_lang": "en",
        "_id": "drafts.179f7877-61c8-44be-9c0b-c5abb631c4d7"
      },
      {
        "title": "Shoes",
        "i18n_lang": "en",
        "_id": "drafts.9f9d1c7e-f983-4608-a066-327de0f5241e"
      },
      {
        "title": "Sneakerssssss",
        "i18n_lang": "en",
        "_id": "drafts.b4d654f7-2b47-4339-9a92-d9ad5d111b43"
      },
      {
        "title": "Kille",
        "i18n_lang": "se",
        "_id": "drafts.i18n.14df2893-d9e5-439d-a966-d41371085cf8.se"
      },
      {
        "title": "Startpage - no",
        "i18n_lang": "no",
        "_id": "drafts.i18n.179f7877-61c8-44be-9c0b-c5abb631c4d7.no"
      },
      {
        "title": "Badetøy",
        "i18n_lang": "no",
        "_id": "drafts.i18n.49efb30f-f6e9-4aff-9d7f-944972d3a276.no"
      },
      {
        "title": "Badetøy",
        "i18n_lang": "se",
        "_id": "drafts.i18n.49efb30f-f6e9-4aff-9d7f-944972d3a276.se"
      },
      {
        "title": "Sko",
        "i18n_lang": "no",
        "_id": "drafts.i18n.9f9d1c7e-f983-4608-a066-327de0f5241e.no"
      },
      {
        "title": "Joggesko",
        "i18n_lang": "no",
        "_id": "drafts.i18n.b4d654f7-2b47-4339-9a92-d9ad5d111b43.no"
      },
      {
        "title": "Herre",
        "i18n_lang": "no",
        "_id": "i18n.14df2893-d9e5-439d-a966-d41371085cf8.no"
      },
      {
        "title": "Kille",
        "i18n_lang": "se",
        "_id": "i18n.14df2893-d9e5-439d-a966-d41371085cf8.se"
      },
      {
        "title": "Startpage - no",
        "i18n_lang": "no",
        "_id": "i18n.179f7877-61c8-44be-9c0b-c5abb631c4d7.no"
      },
      {
        "title": "Startpage - se",
        "i18n_lang": "se",
        "_id": "i18n.179f7877-61c8-44be-9c0b-c5abb631c4d7.se"
      },
      {
        "title": "Dame",
        "i18n_lang": "no",
        "_id": "i18n.40f6584c-1a1c-4574-8837-cfd9e9d062c7.no"
      },
      {
        "title": "Sko",
        "i18n_lang": "no",
        "_id": "i18n.9f9d1c7e-f983-4608-a066-327de0f5241e.no"
      },
      {
        "title": "Skor",
        "i18n_lang": "se",
        "_id": "i18n.9f9d1c7e-f983-4608-a066-327de0f5241e.se"
      },
      {
        "title": "Joggesko",
        "i18n_lang": "no",
        "_id": "i18n.b4d654f7-2b47-4339-9a92-d9ad5d111b43.no"
      },
      {
        "title": "Joggeskor",
        "i18n_lang": "se",
        "_id": "i18n.b4d654f7-2b47-4339-9a92-d9ad5d111b43.se"
      },
      {
        "title": "test",
        "i18n_lang": "no",
        "_id": "i18n.c8a744b1-79f2-48e5-9fec-7be13a8af9df.no"
      },
      {
        "title": "test",
        "i18n_lang": "se",
        "_id": "i18n.c8a744b1-79f2-48e5-9fec-7be13a8af9df.se"
      }
    ]
  }
}

有趣的是,游乐场托管在 https://<sanity-id>.api.sanity.io/v1/graphql/test/default,这与此处用于 apolloclient 的 URI 相同:

function createApolloClient() {
  return new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: new HttpLink({
      uri: 'https://<sanity-id>.api.sanity.io/v1/graphql/test/default', // Server URL (must be absolute)
      credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
    }),
    cache: new InMemoryCache(),
  });
}

非根路径上的文档(例如,drafts.<_id>drafts.i18n.<_id>i18n.<_id> 等)在提取时需要身份验证,因为只有根路径上的文档 (没有点)是公开可见的,无需身份验证。您可以尝试向您的客户端配置添加读取令牌,然后过滤掉查询中的草稿吗?