graphql-combine-query
graphql-combine-query copied to clipboard
[QUESTION] Duplicate variables
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
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
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?
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}
`;