How to query for loggedInUser in React component?



Hi, I’m working off of the advanced node-graphql-server hooked up to a create-react-app frontend.

I’m trying to build a navbar component that will fetch data on the user that’s currently logged in. I’ve set up a query in my navbar component that successfully fetches the logged in user’s username using the {me} query, but I can’t seem to get it to render in the browser.

Relevant code snippets:

Application schema

type Query {
    me: User
type User {
    id: ID!
    email: String!
    username: String!


const { getUserId } = require('../utils')
const Query = {
    me(parent, args, ctx, info) {
        const id = getUserId(ctx)
        return ctx.db.query.user({ where: { id } }, info)

Navbar component

import React, { Component } from 'react'
import { withRouter } from 'react-router'
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'

class Header extends Component {
    render() {
        const loggedInUser =

        return (

    query LoggedInUserQuery {
        me {

export default graphql(LOGGED_IN_USER_QUERY, { 
    name: 'loggedInUserQuery',
}) (withRouter(Header))

When I run this locally in Chrome, I get an error:

TypeError: Cannot read property 'username' of undefined

I hope that makes sense. I’m not sure how to fix this issue (first time app builder here). Can someone help me figure out what I’m doing wrong?

Any help or direction is much appreciated!


if you console.log(loggedInUserQuery) what do you see?


Here’s what I get with “console.log(loggedInUserQuery)”:

const loggedInUserQuery = this.props.loggedInUserQuery

Alternatively, here’s what I get with “console.log(loggedInUser)”:

const loggedInUser =


Issue resolved!

In my previous comment, it seemed odd that console.log(loggedInUserQuery) was outputting two sets of data - one with the “me” props and one without.

Turns out I just needed to control the loading state of my query. I did this by adding:

const loggedInUserQuery = this.props.loggedInUserQuery
const loggedInUser =

if (loggedInUserQuery.loading) {
    return null

return (

Username now properly renders in my navbar component.

Hope this helps any others starting out with React/Apollo/Prisma. Thanks @wesbos for nudging me towards debugging via console.


Can you to resolve an issue with my router? Or else it should be best for contacting a Router Customer Service Number ? please do reply to me on behalf of this


How to buy Retail Software ?