when useQuery or useLazy Query hook is called we want apolloClient to add a new traceId to each graphql backend call. But we see that same traceId is passed in graphql calls
how to add a traceId or key-value as query parameter to apollo client
Thanks for your response Lenz
i tried your suggestions and I still see same traceId value for all my Graphql calls. These calls are made from child component using hooks useQuery, useLazyQuery. Is there a way to pass link or queryParameter with useQuery and useLazyQuery
const { loading, error, data } = useQuery(GetControlsMetaDataDashboard());
useEffect(() => {
if (!loading) {
const result = data?.ControlsMetaData;
if (result && result.length > 0) {
if (mock) {
const mockData = [
...result,
mock,
]
setMetaData(mockData);
}
}
}},[loading, error, data] )
yes , I have tried setContext with and without setTimeout wrapped around a promise. traceId remains the same for all the graphQl calls
One other option is adding a second parameter “client” to useLazyQuery which contains a new apolloClient object with a traceId(as shown below). When the backend call is made apolloclient still has the old traceId and is not appended with new one
type or paste c const traceIdLink = setContext(
request =>
new Promise((success, fail) => {
const queryParameter = "?traceId=" + uuid();
// do some async lookup here
setTimeout(() => {
success({ uri: getAPIEndpoint()?.concat(queryParameter) });
}, 10);
})
);
const client : any = new ApolloClient({
link: from([traceIdLink, new HttpLink({ uri: getAPIEndpoint() })]),
cache: new InMemoryCache(),
credentials: 'include'
});
const [fetchEncryptionAtRestAgg, { loading, error, data }] = useLazyQuery(apiCallMethodsControl[metaData.title](), client);ode here