简体   繁体   English

在 Apollo Client 中使用变量作为 graphQL 操作名称

[英]Using a variable as an graphQL operation name in Apollo Client

I am connecting to a graphQL endpoint, to get the number of reactions(eg like, love, etc) on various content items on the page.我正在连接到一个 graphQL 端点,以获取页面上各种内容项的反应(例如喜欢、喜欢等)的数量。 The content items are identified by GUIDs.内容项由 GUID 标识。 The endpoint does not support fetching reactions for an array of GUIDs, so I am using batched queries instead, with a batch size of 20. In this case, the endpoint requires that each query in a batch has a unique operation name.端点不支持获取 GUID 数组的反应,因此我改用批处理查询,批量大小为 20。在这种情况下,端点要求批处理中的每个查询都具有唯一的操作名称。 How do I pass the GUID into the GQL query?如何将 GUID 传递到 GQL 查询中?

Here is my function call:这是我的 function 电话:

export const getReactionsByObjectIdsEngagement = async (
  objectIds: string[],
  apolloClient: ApolloClient<NormalizedCacheObject>
): Promise<IReaction[]> => {
  const results: IReaction[] = [];
  objectIds.forEach(async (contentId) => {
    await apolloClient
      .query({
        query: GET_REACTIONS_QUERY_IP(contentId),
        variables: { contentId: contentId },
        context: {
          clientName: EngagementClientName,
          headers: {
            'content-type': 'application/json',
          },
        },
        errorPolicy: 'ignore',
      })
      .then((data) => {
        console.log(data);
        const result: IReaction = {
          Id: contentId,
          Reactions: {
            Cool: data.data.getReactionsQuery.cool,
            Like: data.data.getReactionsQuery.like,
            LOL: data.data.getReactionsQuery.lol,
            Love: data.data.getReactionsQuery.love,
            Magic: data.data.getReactionsQuery.magic,
            Party: data.data.getReactionsQuery.party,
            Wow: data.data.getReactionsQuery.wow,
            YouRock: data.data.getReactionsQuery.youRock,
          },
        };
        results.push(result);
      })
      .catch((error) => {
        console.error(error);
      });
  });

  return results;
};

And the related query:以及相关查询:

export const GET_REACTIONS_QUERY_IP = (contentId: string) => {
  return gql`
    query getReactionsQuery($contentId: ID!) {
      "${contentId}": reactionsCount(contentId: $contentId) {
        like
        love
        wow
        cool
        party
        youRock
        magic
        lol
      }
    }
  `;
};

I have got the interpolation itself to work, but the request does not fire off successfully, because the Apollo client encounters an unexpected number or string in Name.我已经让插值本身工作了,但是请求没有成功触发,因为 Apollo 客户端在名称中遇到了意外的数字或字符串。 Initially I thought that it was because I failed to escape something in GUID, so I tried generating a random number in Typescript and passing it into the query.最初我以为是因为我在 GUID 中转义失败,所以我尝试在 Typescript 中生成一个随机数并将其传递到查询中。 Again, the passing in works, but I get the same error in Apollo ('Expected Name, got myQuery_99").同样,传递有效,但我在 Apollo 中遇到了同样的错误('Expected Name, got myQuery_99')。

Apollo client error阿波罗客户端错误

Batching works fine with the forEach loop, the request shows as expected in the Developer Tools Network tab in the browser.批处理在 forEach 循环中运行良好,请求按预期显示在浏览器的 Developer Tools Network 选项卡中。

Help a junior deliver some good news on the next daily.帮助初级人员在第二天传递一些好消息。 Thanks in advance!提前致谢!

If you end up here: the issue was an extra set of quote marks around my variable in the query: "${contentId}" should be ${contentId} ..如果你在这里结束:问题是在查询中我的变量周围有一组额外的引号: "${contentId}"应该是${contentId} ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM