reactql / example-auth

User auth, session & JWT example for ReactQL
48 stars 17 forks source link

Missing ROOT_QUERY in Apollo Cache Proxy #11

Closed iKonrad closed 6 years ago

iKonrad commented 6 years ago

Hi,

First of all, thanks for this great library! I'm trying to replicate this authentication example with mongodb, and I'm almost there, but there's one little thing I can't get a grasp on.

Just to clarify, your code works fine. The problem is that I have no idea why.

See, in your loginMutation in session.js file, the login mutation has SessionType type, but it's returning an object with ok and session. Why? SessionType doesn't have the session field, so shouldn't it be:

return {
    ok: true,
    ...session,
}

Another thing is, for some reason, in the login.js in the update function, my proxy object doesn't have the ROOT_QUERY field after running the login mutation for some reason which crashes the proxy.readQuery() function.

In my login mutation, I've changed things a little and removed ok and errors fields as I'm relying on exceptions to provide errors and I've change the code to return session on its own rather than wrapping it in an object. Could that be an issue?

Here's my login mutation code:


const SessionType = new GraphQLObjectType({
  name: 'Session',
  description: 'User login session',
  fields: () => ({
    jwt: {
      type: GraphQLString,
      resolve(obj) {
        let session = obj.session !== undefined ? obj.session : obj;
        return session.getJWT();
      },
    },
    user: {
      type: UserType,
      async resolve(obj) {
        const cursor = User.findById(obj.userId).cursor();
        const user = await cursor.next();
        return user;
      },
    },
  }),
});

const SessionMutations = {
  login: {
    type: SessionType,
    args: {
      email: {
        type: GraphQLString,
      },
      password: {
        type: GraphQLString,
      },
    },
    async resolve(_, args, ctx) {
      const session = await login(args);
      // If getting the JWT didn't throw, then we know we have a valid
      // JWT -- store it on a cookie so that we can re-use it for future
      // requests to the server
      ctx.cookies.set(config.cookieName, session.getJWT(), {
        expires: session.expires,
      });

      // Return the session record from the DB
      return session;
    },
  },
};

export {
  SessionType,
  SessionMutations,
};

I'm deliberately not wrapping the login function in try{} so the exception is catched by Apollo rather than sending errors in SessionType.

Also, here are the screenshots of what's in the CacheProxy on yours and my projects:

Yours: screen shot 2018-01-03 at 23 10 58

Mine: screen shot 2018-01-03 at 23 31 37

And because the ROOT_QUERY is missing, it throws the Can't find field session on object (ROOT_QUERY) undefined. error.

Any ideas? Thanks again Konrad

iKonrad commented 6 years ago

Sorry, my bad. It seems that I just didn't call the session query on page load on the login screen, hence the ROOT_QUERY missing.