Im trying to export a named gql query to be used in another react component in order to call it to update the apollo cache. The code works if I use PRODUCTS_QUERY since I export const PRODUCTS_QUERY. However, I would prefer to use the named gql variable 'productsQuery' for better naming convention. However, when I use 'productsQuery' in place of PRODUCTS_QUERY, it will throw an error. Anyone has successfully used the named variable? Please advise. Thanks in advance
In Home.js
export const PRODUCTS_QUERY = gql`
{
products {
id
price
pictureUrl
name
}
}
`;
export default graphql(PRODUCTS_QUERY, { name: "productsQuery" })(Home);
In AddProduct.js
// import { PRODUCTS_QUERY } from "./Home";
import { productsQuery } from "./Home";
try {
let response = await this.props.mutate({
variables: {
name,
price,
picture
},
// - - - - - - - - THIS PART WORKS - - - - - - -
// update: (store, { data: { createProduct } }) => {
// const data = store.readQuery({ query: PRODUCTS_QUERY });
// data.products.push(createProduct);
// store.writeQuery({ query: PRODUCTS_QUERY, data });
// }
// - - - - - - - - THIS PART FAILS - - - - - - -
update: (store, { data: { createProduct } }) => {
const data = store.readQuery({ query: productsQuery });
data.products.push(createProduct);
store.writeQuery({ query: productsQuery, data });
}
});
console.log("RESPONSE", response);
} catch (err) {
console.log("ERROR", err);
}
this.props.navigation.navigate("Home");
};
Any way I can export in a manner which allows me to use named variable 'productsQuery' rather than 'PRODUCTS_QUERY' since purpose of naming is to make naming convention standardized. Thanks in advance