Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
¿Cómo enviar una variable común con cada solicitud en react-apollo?

Estoy tratando de enviar una variable común con cada solicitud a través de react-apollo. Así que probé con ApolloLink para agregar una variable en la operación, pero al inspeccionar la red no puedo ver la variable que va con la consulta.

Así es como he escrito el middleware:

 const authLink2 = new ApolloLink((operation, forward) => { operation.variables['entityId'] = '633'// localStorage.getItem('entityId'); operation.setContext(({ headers }) => { const token = localStorage.getItem(AUTH_TOKEN); return { headers: { ...headers, Authorization: token ? `JWT ${token}` : "", } } }); // until this log I can see the entityId variable has been appended console.log('operation', operation); return forward(operation); }); // creating ApolloClient export const client = new ApolloClient({ link: authLink2.concat(httpLink), cache });

Si esto no es posible con el software intermedio ApolloLink, ayúdeme a encontrar una forma de agregar la variable con cada solicitud.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

puede enviar cualquier cosa en el encabezado como este y acceder a él en el backend

 const httpLink = createHttpLink({ uri: `${configEnv.BASE_URL}/graphql`, }); const authLink = setContext((_, { headers }) => { // get the authentication token from local storage if it exists const token = localStorage.getItem(LOCAL_CONSTANT.TOKEN); const userToken = token ? JSON.parse(token) : null; // return the headers to the context so httpLink can read them return { headers: { ...headers, Authorization: userToken?.accessToken ? `Bearer ${userToken?.accessToken}` : '', testingVariable: '633' }, }; }); export const client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!