Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to name graphql requests in the devtool network tab?

I'm using apollo as my client and I run plenty of queries and mutations on my app. I was wondering if there is a way to have each of my query/mutation displayed by its name (eg. getProduct) instead of all showing as "graph" in my network tab? I'm on Brave (Chromium).

It would make debugging easier if I didn't have to click on each one and check the headers or the response to identify which query or mutation this request corresponds to.

Here's how it currently shows in my devtools:

network tab screenshot

Thanks a lot!

like image 427
Milan Avatar asked Oct 20 '25 16:10

Milan


2 Answers

Maybe there is a better way but here the minimal code I could do to make it.

import {
  ApolloClient,
  ApolloLink,
  HttpLink,
  InMemoryCache,
} from '@apollo/client';

const httpLink = new HttpLink({ uri: MY_BASE_URL });

const namedLink = new ApolloLink((operation, forward) => {
  operation.setContext(() => ({
      uri: `${MY_BASE_URL}?${operation.operationName}`,
    })
  );
  return forward ? forward(operation) : null;
});

export const client = new ApolloClient({
  link: ApolloLink.from([namedLink, httpLink]),
  cache: new InMemoryCache(),
});

You'll have to name your query :

import { gql } from "@apollo/client";

const QUERY = gql`
  query QueryName {
    ...
  }
`;

Hope it'll help.

like image 87
bossno Avatar answered Oct 22 '25 06:10

bossno


uri prop of HttpLink can accept function which have operation as an arg so it can be done like this as well:

const httpLink = new HttpLink({ uri: (operation) => `${MY_BASE_URL}?${operation.operationName}` });
like image 41
Artem Pelenev Avatar answered Oct 22 '25 05:10

Artem Pelenev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!