Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass cookie from apollo-server to apollo-clenet

Mutation

Mutation: {
  signUp: (_, { res }) => {
    try {
      res.cookie("jwt", "token", {
        httpOnly: true
      });
      return "Amasia";
    } catch (error) {
      return "error";
    }
  };
}

Apollo-clenet-react

const [addTodo, { loading, error, data }] = useMutation(gql);

  const [formSignUp, setFormSignUp] = useState({
    lastName: '',
    firstName: '',
    password: '',
    email: '',
  });

  const change = e => {
    const { value, name } = e.target;
    setFormSignUp({ ...formSignUp, [name]: value });
  };

When i make a request from react. Here is the answer I get from the server.

1)Data {"data": {"signUp": "Amasia"}}

2) Networkenter image description here

Application

Well when I look in Application Cookies, it is empty. enter image description here

What am I doing wrong Why are cookies empty?

like image 378
Silicum Silium Avatar asked Nov 24 '19 19:11

Silicum Silium


1 Answers

problem in apollo-client and apollo-server settings (apollo-cookie).

I set up like that.

apollo-client

import { render } from 'react-dom';
import React, { Suspense } from 'react';
import { ApolloClient } from 'apollo-client'
import { ApolloProvider } from 'react-apollo';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createHttpLink } from 'apollo-link-http';
import './i18n';

import Loading from './component/loading';
import RouteProj from './router';

const link = createHttpLink({
  uri: 'http://localhost:8000/graphql',
  credentials: 'include'
});

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link,
});


render(
  <ApolloProvider client={client}>
    <Suspense fallback={<Loading />}>
      <RouteProj/>
    </Suspense>
  </ApolloProvider>,
  document.getElementById('root'),
);

apollo-server

import cors from "cors";
import express from "express";
import { ApolloServer } from "apollo-server-express";
import mongoose from "mongoose";

import schema from "./schema";
import resolvers from "./resolvers";
import models from "./models";

  const app = express();

  var corsOptions = {
    origin: "http://localhost:3000",
    credentials: true
  };
  app.use(cors(corsOptions));

  const server = new ApolloServer({
    typeDefs: schema,
    resolvers,
    context: ({ res }) => ({
      res
    })
  });

  server.applyMiddleware({ app, path: "/graphql", cors: false });
  app.listen({ port: 8000 });
like image 180
Silicum Silium Avatar answered Sep 20 '22 04:09

Silicum Silium