React GraphQL & ApolloServer: Unhandled Rejection (Error): GraphQL error: Cannot access 'post' before initialization

I have a simple social media app that uses: GraphQL & ApolloServer as a backend where users can like posts. But an error occurs when a user who isn’t logged in tries to like a button and an error called:

Unhandled Rejection (Error): GraphQL error: Authorization header must be provided


This is the code for the likePost mutation…

    async likePost(_, { postId }, context) {
  const { username } = checkAuth(context);

  const post = await Post.findById(postId);
  if (post) {
    if (post.likes.find((like) => like.username === username)) {

      post.likes = post.likes.filter((like) => like.username !== username);
    } else {
        createdAt: new Date().toISOString()

    return post;
  } else throw new UserInputError('Post not found');


This is the code for checking authorization file.

   const { AuthenticationError } = require('apollo-server');

const jwt = require('jsonwebtoken');
const { SECRET_KEY } = require('../config');

module.exports = (context) => {
  // context = { ... headers }
  const authHeader = context.req.headers.authorization;
  if (authHeader) {
    // Bearer ....
    const token = authHeader.split('Bearer ')[1];
    if (token) {
      try {
        const user = jwt.verify(token, SECRET_KEY);
        return user;
      } catch (err) {
        throw new AuthenticationError('Invalid/Expired token');
    throw new Error("Authentication token must be 'Bearer [token]");

throw new Error(‘Authorization header must be provided’);

How do I go about solving this problem so that no error comes up when an unregistered user tries to like a post?

143 thoughts on “React GraphQL & ApolloServer: Unhandled Rejection (Error): GraphQL error: Cannot access 'post' before initialization”

  1. What i don’t realize is in truth how you are no longer actually a lot
    more smartly-favored than you might be right now.
    You’re very intelligent. You understand therefore considerably in the case
    of this topic, made me personally imagine it from numerous
    numerous angles. Its like men and women are not interested unless it’s something to accomplish with Lady gaga!
    Your personal stuffs excellent. All the time care for it up!

  2. Hi there! I could have sworn I’ve visited this website before but after looking at many of the posts I realized it’s
    new to me. Nonetheless, I’m definitely pleased I discovered
    it and I’ll be book-marking it and checking back regularly!

  3. Outstanding post however I was wanting to know if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate a little bit further.

  4. Howdy I am so grateful I found your blog, I really found you by accident, while I was browsing on Bing
    for something else, Nonetheless I am here now and would just like to say
    cheers for a tremendous post and a all round interesting
    blog (I also love the theme/design), I don’t have time to read it all at
    the moment but I have bookmarked it and also included your RSS feeds,
    so when I have time I will be back to read a lot more, Please do keep up the great job.

  5. Thanks for any other wonderful post. Where else may anyone get that type of
    info in such a perfect way of writing? I have a
    presentation subsequent week, and I’m on the look for such info.

  6. hey there and thank you for your information – I have definitely picked
    up something new from right here. I did however
    expertise a few technical issues using this site, since I experienced to reload the website many times previous to
    I could get it to load correctly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but sluggish loading instances times will
    sometimes affect your placement in google and can damage
    your quality score if ads and marketing with Adwords. Well I’m adding this RSS to my e-mail and can look out for a lot more
    of your respective exciting content. Ensure that you update this
    again soon. ps4 ps4