score:4

Accepted answer

the issue is that given some query like "naruto", your current code results in the following text:

media(type:anime, search: naruto ) {

this is not valid syntax since string literals should be surrounded by double quotes (").

don't use string interpolation to provide dynamic values to the query. these should always be expressed as variables and included as a separate object inside your request alongside query.

you need to define the variable as part of your operation, providing the appropriate type

var query = `query search ($searchquery: string!) {

then you can use the variable anywhere inside the operation:

media(type:anime, search: $searchquery) {

now just pass the variable value along with your request.

body: json.stringify({
  query,
  variables: {
    searchquery,
  }
})

note that the variable name is prefixed with a $ inside the graphql document, but outside of it, we don't do that.

score:0

media() looks like a function, so in that case the correct syntax would be:

media(type="anime", search=searchquery)

or if the argument of media() is an object

media({type: "anime", search: searchquery})

also, you don't need to use ${} around searchquery since searchquery is already a string. the usage for that would be something like

`${searchstring}` or `foo${searchstring}bar`

using the `` around the ${} utility to represent a string and its variable inside the string literal.

hope it helps!


Related Query

More Query from same tag