1
votes

I am working on a react-native app with graphql using apollo and react-redux.

I have the following graphql query that returns the following info for logged in user (using JTW token):

enter image description here

I am trying to run the above query and get the response into the react-native app via redux.

here's my graphql/queries/me.js

import { gql } from 'react-apollo';

export default gql`
    {
        me {
            avatar
            username
            firstName
            lastName
        }
    }
`;

I am trying to use it on my HomeScreen that user is dispatched to after successful login like this:

// ...
import { graphql, compose, withApollo } from 'react-apollo';
import { connect } from 'react-redux';

import ME_QUERY from '../graphql/queries/me';


class HomeScreen extends Component {
    componentDidMount() {
        this._getUserInfo();
    }

    _getUserInfo = async () => {
        const { data: { me } } = await this.props.client.query({ query: ME_QUERY });
        this.props.getUserInfo(me);
    };

    render () {
        const { data } = this.props;
        if (data.loading) {
            return (
                <Root>
                    <ActivityIndicator size="large" />
                </Root>
            );
        }
        return (
            // snipped
        );
    };
}

export default withApollo(compose(
    connect(undefined, { getUserInfo }),
    graphql(GET_TWEETS_QUERY)
)(HomeScreen));

and here's my actions/user.js (redux action)

// ...

export function getUserInfo() {
    return {
        type: 'GET_USER_INFO',
        info
    }
}

and my reducer for user:

export default (state = initialState, action) => {
    switch (action.type) {
        // ...
        case 'GET_USER_INFO':
            return {
                ...state,
                info: action.info
            };
        default:
            return state;
    }
};

Once the getUserInfo query ran and we got the info, I am trying to use it on a component called HeaderAvatar like this:

import React, { Component } from 'react';
import styled from 'styled-components/native';
import Touchable from '@appandflow/touchable';
import { connect } from 'react-redux';

const AVATAR_SIZE = 30;
const AVATAR_RADIUS = AVATAR_SIZE / 2;

const Avatar = styled.Image`
    height: ${AVATAR_SIZE};
    width: ${AVATAR_SIZE};
    borderRadius: ${AVATAR_RADIUS};
`;

const Button = styled(Touchable).attrs({
    feedback: 'opacity',
    hitSlop: { top: 20, bottom: 20, right: 20, left: 20 }
})`
    marginLeft: 15;
    justifyContent: center;
    alignItems: center;
`;

class HeaderAvatar extends Component {
    state = { }
    render () {
        if (!this.props.info) {
            // snipped
        }
        return (
            <Button>
                <Avatar source={{ uri: this.props.info.avatar }} />
            </Button>
        );
    }
}

export default connect(state => ({ info: state.user.info }))(HeaderAvatar);

Any idea why I am not able to get the info field from the state object?

I know the query is executing and I am loading the info, I verified it by doing a console.log of me object inside the _getUserInfo() function and I can see the data.

P.S. sorry about the long post, I don't know how else to describe the problem.

This is how the error appears in the Android simulator: http://i.imgur.com/VdT2TJq.png

1

1 Answers

1
votes

You're seeing that error because you're using an undeclared variable inside your action creator:

export function getUserInfo() {
    return {
        type: 'GET_USER_INFO',
        info
    }
}

You just need to add info to the arguments taken by getUserInfo.