reactjsgraphqlapollo-clientmutation

Show ApolloClient mutation result


This is my first time using Apollo and React so I'll try my best.

I have a GraphQl API from which I consume some data through ApolloClient mutations. The problem is that I don't know how to show the resulting information outside of the .result. I've tried to do so with a class that has a function to consume some data and a render to show it.

The mutation works and shows the data on the console but the page remains blank when the page is loaded, so the problem I've been stuck on is, how do I show this data?

Btw, if there's any advice on how to insert data from a form using this same mutation method I'd pretty much appreciate it.

Thanks in advance.

import React, { useEffect, useState, Component } from 'react';
import { graphql } from 'react-apollo';
import './modalSignUp.css';
import{header} from './Header.js';
import ReactDOM from "react-dom";
import { ApolloProvider, Query, mutation } from "react-apollo";
import { ApolloClient, InMemoryCache, gql, useMutation } from '@apollo/client';


export const client = new ApolloClient({
    uri: 'http://localhost:4011/api',
    cache: new InMemoryCache(),
});

client.mutate({
    mutation: gql`
      mutation signin{
      login(data:{
      username:"elasdfg",
      password:"12345678"}){
      id,roles,email,username}
  }
  `
}).then(result => console.log(result));


export class UserList extends Component {
    displayUsers() {
        console.log(this.result)
        var data = this.props.data;

        return data.login.map((user) => {
            return (
                <li>{user.email}</li>
            );
        })
    }
    render() {
        return (
            <div>
                <li>
                    {this.displayUsers()}
                </li>
            </div>
        );
    }
}

const root = ReactDOM.createRoot(document.getElementById('root')); 
root.render(<Header />);

Mutation result

enter image description here

I've tried to use a class to fetch the data given by the mutation and later render it in the component. I've also tried passing the result to a variable but I had no success with that.

I'm just expecting to see the data resulting from the mutation


Solution

  • You should request data inside the component and then save it to the state.

    export class UserList extends Component {
      constructor() {
        this.state = {
          newData: null,
        };
        this.mutateData = this.mutateData.bind(this);
      }
    
      mutateData() {
        client
          .mutate({
            mutation: gql`
              mutation signin {
                login(data: { username: "elasdfg", password: "12345678" }) {
                  id
                  roles
                  email
                  username
                }
              }
            `,
          })
          .then((result) => {
            this.setState({ newData: result });
          });
      }
    
      componentDidMount() {
        this.mutateData();
      }
    
      render() {
        // do something with new data
      }
    }