Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get client with typePolicies fields #6875

Closed
magdyB7r opened this issue Aug 21, 2020 · 3 comments
Closed

get client with typePolicies fields #6875

magdyB7r opened this issue Aug 21, 2020 · 3 comments

Comments

@magdyB7r
Copy link

magdyB7r commented Aug 21, 2020

I was migrating to ac3 but stuck with using the client to get remote data like resolvers just like those two examples

here I need the client to get the active template from the server, how can I get the client with typePolicies?


getColors: {
    async read(_, {cache}) {
      const template = await getActiveTemplate(cache,client);
      if (!template) {
        return {};
      }

      const settings = template.settings ? JSON.parse(template.settings) : [];
      const colors = settings.find((setting) => setting.type === 'colors');

      if (!colors) {
        return {};
      }

      let colorConfig = {};

      forEach(colors.fields, (color) => {
        colorConfig = Object.assign({}, colorConfig, {
          [color.key]: color.value,
        });
      });

      return colorConfig;
    },
  },

and here with mutation same, I need the client

 export const cartMutation = {
      addProductToCart: (_root, {product}, {cache, client}) => {
       const cart = cartItems();

   const {status, message, newCart} = checkItemAddCart(cart, product);


     updateCart(cache, newCart);

     authCart(client, {quantity: 1, productId: product.id}, newCart);
   }

   // const newCart = addProductToCart(cart, product);
   // localStorage.setItem("CartItems", JSON.stringify(newCartItems));

   // const {me} = client.cache.readQuery({
   //   query: CURRENT_CUSTOMER,
   // });
   return newCart;
 },
}

the ugly solution with this is


  const client = useApolloClient();
  cartMutation.addProductToCart(client);

how can I use client easily with those two examples?

@magdyB7r
Copy link
Author

what is the best practice to do async field to replace this

   getColors: async (_root, args, {cache, client}) => {
      const template = await getActiveTemplate(cache, client);
      if (!template) {
        return {};
      }

      const settings = template.settings ? JSON.parse(template.settings) : [];
      const colors = settings.find((setting) => setting.type === 'colors');

      if (!colors) {
        return {};
      }

      let colorConfig = {};

      forEach(colors.fields, (color) => {
        colorConfig = Object.assign({}, colorConfig, {
          [color.key]: color.value,
        });
      });

      return colorConfig;
    },

@benjamn
Copy link
Member

benjamn commented Aug 21, 2020

@magdyB7r See apollographql/apollo-feature-requests#383 for a recommended workaround. If you don't like that approach, feel free to keep using local resolver functions for now.

@hwillson
Copy link
Member

hwillson commented May 5, 2021

It doesn't sound like there is an outstanding issue here, so closing. Thanks!

@hwillson hwillson closed this as completed May 5, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants