Coder Social home page Coder Social logo

Comments (3)

stubailo avatar stubailo commented on May 18, 2024

I'm looking into updateQueries but have had no luck with it

That's generally the most manual approach available if the normalization isn't doing it automatically and you don't want to refetch. What's your use case and what have you tried?

from react-fullstack-graphql.

stephenkao avatar stephenkao commented on May 18, 2024

Hmmm, hokay! Here's the context:

I have an online store with a set of products that can be added to or removed from the current user's wishlist. Using this.state.isOnWishList for optimistic feedback was fine as long as there was only one rendered component per product, but now I'm in a situation in which there can be multiple components rendered per product.

I render the product page like:

import React, { Component } from 'react';
import { graphql } from react-apollo;

const productQuery = gql`
  query Product($ids: [Int]) {
    products(ids: $ids) {
      id
      isOnWishList
      ...
    }
  }
`;

@graphql(productQuery)
class ProductPage extends Component { ... }

Now there's a WishlistButton that's deeply nested inside ProductPage:

import React, { Component } from 'react';
import { graphql } from react-apollo;

const addToWishListMutator = gql`
  mutation AddToWishList($productId: Int!) {
    addToWishlist (productId: $productId)
  }
`;

@graphql(addToWishListMutator, { name: 'addToWishList' })
class WishListButton extends Component {
  toggleWishList = (e) => {
    // Omitting removal for simplicity
    this.addToWishList();   
  }

  render() {
    const { product }  = this.props; // passed down through the hierarchy
    const { isOnWishList } = product;
    
    return (
        <button>
          { isOnWishList ? 'On Wish List' : 'Add to Wish List' }
        </button>
    );
  }
}

Now after clicking the WishListButton, it calls addToWishList, which makes the GraphQL mutation call. In my ideal case, just using dataIdFromObject in the Apollo client setup should make this mutation update the corresponding product record in the cache, and the button should update to say "On Wish List"...but it doesn't. I also added an updateQueries in the mutator decorator call, but it doesn't look like it's invoked ever. The only thing that's worked so far is explicitly defining another query productWishListQuery and passing down productWishListQuery.refetch to be invoked after the mutator is called.

Would you happen to know what's wrong with my setup?

from react-fullstack-graphql.

marktani avatar marktani commented on May 18, 2024

This issue is outdated. Please open a new issue if you have problems.

from react-fullstack-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.