can’t persist user session in apollo-graphql with express-session

can’t persist user session in apollo-graphql with express-session


0

I have nextjs-nodejs project, where I use apollo-graphql and Prisma, so when I create a user, I should send a session to "getSession" function, which is a mutation, and this mutation uses express-session to save the user info like that:

   getSession: async (
      _: any,
      args: any,
      { prisma, session }: GraphqlContext
    ) => {
      const { sessionUser } = args;

      try {
        if (!sessionUser) return false;

        await (session.user = sessionUser);
        await session.save();

        console.log("[getSession Mutation] ============================= ");
        console.log(session);

        return true;
      } catch (error) {
        throw error;
      }
    },
  },

and when I console it in the terminal I get a complete session without a doubt, but when I try to use the user object from the session in another function, I can’t see the user object:

 getUser: async (_: any, args: any, { prisma, session }: GraphqlContext) => {
      const { userId } = args;

      console.log("[getUser Query] ============================= ");
      console.log(session);

      try {
        // If I have userId this means that I want special user
        if (userId) {
          const users = await prisma.user.findUnique({
            where: {
              id: userId,
            },
            include: {
              conversations: true,
              participants: true,
            },
          });

          return [users];
        }

        // else it means that I want all users from mongodb
        const users = await prisma.user.findMany({
          include: {
            conversations: true,
            participants: true,
          },
        });

        return users.map((el) => {
          return el;
        });
      } catch (error) {
        throw error;
      }
    },

I want to access user session information in every mutation / Query function.


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *