Change GraphQL Variables param dynamically from function value

Viewed 14

I am using a function in reactjs that will return a GrpahQL query result. My code is following

...
const fetchData = async (url, query, param, variable) => {
        return await axios.post(url, {
            query: query,
            variables: { param: variable }
        })
    };
...

I want to change the param of variables dynamically from the function value.

How can I pass the param to variables?

1 Answers

you are looking for

computed property names in Object MDN DOCS

so it would be something as

{ [param] : variable }

so now param refers to some variable value instead of "param" as a string

Related