Suppose I want to query a list of posts that don’t...
# prisma-whats-new
r
Suppose I want to query a list of posts that don’t belong to my currently authenticated user. Can I do something like this, and how would I get the current user’s ID? Do I have to run a separate query first?
Copy code
posts: allPosts(filter: {
    user: {
      id_not: <current user ID>
    }
  }) {
    id
    title
    body
  }
}
n
@richeterre yes you need the current user id, then you can do it exactly with that query
you can use
Copy code
query {
  user {
    id
  }
}
to get it 🙂
r
Thanks @nilan! Was just wondering whether I could do this in a single query, but apparently not. So using Apollo Client, I would first query the user and then somehow trigger the other query?
n
Typically with Apollo Client you can do the user query once right after the user logs in
keep it in localStorage or a state manager like Redux or MobX
r
Right, it will rarely change while the user is signed in (only if they edit their settings or something). I’m using Redux already, so I can just throw it into my state 🙂
n
awesome 🙂
r
Thank you for the quick support, you guys are doing a fantastic job 🌻
❤️ 1
💚 1
c
Let me hijack your question, @richeterre : After you get the token back from the signIn mutation, where are you putting it in your own Redux store and how are you telling Apollo to look for it in subsequent requests?
r
@chicagoboy2011, I store it in
state.auth.token
and look it up for each request using Apollo middleware like so:
Copy code
networkInterface.use([{
  applyMiddleware(req, next) {
    if (!req.options.headers) {
      req.options.headers = {}
    }

    // Set Authorization header
    const token = store.getState().auth.token
    req.options.headers.authorization = `Bearer ${token}`
    next()
  },
}])
(see http://dev.apollodata.com/core/network.html#networkInterfaceMiddleware)
I’m setting up my store in the same file (
App.js
) as the middleware, so I can just access it via
store.getState()
here
c
It worked splendidly -- thank you!!