Skip to content
This repository has been archived by the owner on Jan 15, 2020. It is now read-only.

Handling user logout and registering gql tag #19

Closed
wants to merge 2 commits into from

Conversation

tomitrescak
Copy link

This pull request handles:

  • the removal of user from the context on server upon logout
  • registration of gal tag on client

@lorensr
Copy link
Contributor

lorensr commented Jun 22, 2016

Thanks Tomas! I didn't add user to context because when we add userId caching #6, we won't always have fetched the user, and ideally we won't be fetching the user every time, so as to improve latency. I think the best way to get the user is #4.

import { registerGqlTag } from 'apollo-client/gql';

// register gql tag for queries
registerGqlTag();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically I prefer not having comments that mainly repeat the code. In this case, I'd either assume the reader knows what a gql tag is or change the comment to maybe // setup query template literal tag, probably former

@tomitrescak
Copy link
Author

Can you elaborate on #4 for user caching? I actually created buy own caching mechanism, but it's very silly. would love to have something more robust.

@lorensr
Copy link
Contributor

lorensr commented Jun 26, 2016

Sure, something like this:

USER_CACHE_MAX = 1000
cache = lruCache(config.userCacheMax || USER_CACHE_MAX)

// inside apollo options function
cacheEntry = cache.get req.headers.authorization
if cacheEntry.user 
  isExpired = cacheEntry.expiresAt < new Date()
  if not isExpired
    options.context = {user}
else
  updateCache = (user) ->
    cache.set req.headers.authorization,
      user: user
      expiresAt: // todo

  cursor = Meteor.users.find({"services.resume.loginTokens.hashedToken": hashedToken})
  user = await cursor.findOne()
  if user
    updateCache(user)
    options.context = {user}

    handle = cursor.observe
      changed(newDoc, oldDoc): ->
        if token expired
          cache.del(req.headers.authorization)
          handle.stop()
        else
          if cache.has req.headers.authorization
            updateCache(newDoc)
          else
            handle.stop()
      removed(oldDoc): ->
        cache.del(req.headers.authorization)
        handle.stop()

@lorensr
Copy link
Contributor

lorensr commented Jun 26, 2016

@tomitrescak
Copy link
Author

@lorensr you are a star! Thank you!

@stubailo stubailo closed this Jul 1, 2016
@stubailo stubailo reopened this Jul 1, 2016
@stubailo
Copy link
Contributor

stubailo commented Jul 1, 2016

registerGqlTag is now deprecated, so we shouldn't use it anymore.

@tomitrescak
Copy link
Author

@stubailo what's the substitute?

1 similar comment
@tomitrescak
Copy link
Author

@stubailo what's the substitute?

@lorensr
Copy link
Contributor

lorensr commented Jul 1, 2016

@tomitrescak #18 (comment)

@tomitrescak
Copy link
Author

Nevermind, ... But I liked the central approach ... Call me old fashioned :)

4 similar comments
@tomitrescak
Copy link
Author

Nevermind, ... But I liked the central approach ... Call me old fashioned :)

@tomitrescak
Copy link
Author

Nevermind, ... But I liked the central approach ... Call me old fashioned :)

@tomitrescak
Copy link
Author

Nevermind, ... But I liked the central approach ... Call me old fashioned :)

@tomitrescak
Copy link
Author

Nevermind, ... But I liked the central approach ... Call me old fashioned :)

@lorensr
Copy link
Contributor

lorensr commented Jul 1, 2016

Weird, think your browser got into a submit loop 😆

image

@tomitrescak
Copy link
Author

Oh, sh.t I'm in Eastern Europe in a mountain cottage on Edge connection, things always get interesting this way I guess. Sorry! Going offline now.

@lorensr lorensr closed this Oct 11, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants