graphql-combine-query icon indicating copy to clipboard operation
graphql-combine-query copied to clipboard

[QUESTION] Duplicate variables

Open giulianok opened this issue 3 years ago • 4 comments

I'm having some trouble combining queries that have the same variable. https://github.com/domasx2/graphql-combine-query/blob/master/src/index.ts#L68

Looks like this is already supported by addN, is there any reason why does not work with add?

Happy to contribute

giulianok avatar Jul 24 '21 21:07 giulianok

Here are some ideas:

  • The add function could always rename the variables and add a postfix, e.g. id -> id_0 to avoid collision
  • Accept "global variables" in the combineQuery function and use them in all queries

giulianok avatar Jul 24 '21 23:07 giulianok

I combine queries with combineQuery function + add and I don't pass variables there, I pass them in apollo's useQuery afterwards like this:

// query.js file
const queries = []; // some imported queries here, that use common variables
const getQuery= () => {
  let combinedQuery = combineQuery("Query");
  queries.forEach((query) => (combinedQuery = combinedQuery.add(query)));

  return combinedQuery.document;
};
const query = getQuery();


// inside some component
const {loading, error, data} = useQuery(query, { // imported query here
    variables: {...someVariables},
  });

And after that I get duplicate variable definition variable for operations Query1 and Query2 error.

Does anyone know a workaround to fix this?

biggytech avatar Aug 26 '22 14:08 biggytech

I solved this issue with fragments without graphql-combine-query lib, like was mentioned here: https://github.com/apollographql/graphql-tag/issues/169#issuecomment-544996206

const oneQuery = gql`
    query Query(
      $variable: String!
    ) {
      ...firstFragment
      ...secondFragment
    }
    ${firstFragment}
    ${secondFragment}
  `;

biggytech avatar Aug 26 '22 15:08 biggytech