💬

Apollo clientで送信する前にリクエストボディを確認する方法

2024/10/22に公開

送信前に何を送るのかを確認したかったので備忘録

結論

ApolloLinkoperation から確認できる

import { ApolloClient, ApolloLink, HttpLink, InMemoryCache } from "@apollo/client";
import { print } from "graphql";

const httpLink = new HttpLink({
  uri: `http://localhost:8000/graphql`,
});

const authLink = new ApolloLink((operation, forward) => {
  const operationName = operation.operationName;
  const query = print(operation.query);
  const variables = operation.variables;
  const requestBody = {
    operationName: operationName,
    query: query,
    variables: variables,
  };
  console.log(requestBody); // これ
  return forward(operation);
});

export const graphqlCryspeBackendClient = new ApolloClient({
  link: authLink.concat(httpLink),
  cache: new InMemoryCache(),
});

Discussion