I have a graphql query and a working useState. How can I manage my 'title' useState with it? I'd like that the title would be in the query instead of "Bad boys" to send.
function Home() {
const [title, setTitle] = useState("");
const searchMovies = async (e) => {
e.preventDefault();
console.log("submitting");
const query = `{
searchMovies(query: "Bad boys") {
id
name
overview
releaseDate
cast {
id
person {
name
}
role {
... on Cast {
character
}
}
}
}
}`;
const url = "https://tmdb.sandbox.zoosh.ie/dev/graphql";
try {
request("https://tmdb.sandbox.zoosh.ie/dev/graphql", query).then((data) =>
console.log(data)
);
} catch (err) {
console.log(err);
}
};
The query is an interpolated string so you can pass variables into it like this:
const query = `{
searchMovies(query: ${title}) {
id
name
overview
releaseDate
cast {
id
person {
name
}
role {
... on Cast {
character
}
}
}
}
}`;