Search code examples
node.jsreactjsgraphqlslugstrapi

How can I search by slug using GraphQL? - "Unknown argument \"slug\" on field \"article\"


I'm fiddling around in GraphQL playground trying to build a query to retrieve an article based on its slug. My backend is running on Strapi and I've constructed a 'Article' type with a text field named 'slug'. I could access this by id (and this appears to work) but I'd prefer to access it by slug so I can build pages with human-friendly urls.

I have a dummy article with id of 9 and slug of "a".

My query looks like this:

query Articles {
    article (  slug: "a"  ) {
        title
    }
}

Upon doing this I recieve an error:

"message": "Unknown argument \"slug\" on field \"article\" of type \"Query\".",

This is confusing to me since the docs here indicate that I should be able to pass fields as arguments and search accordingly, but even if I interchange slug with title or author I recieve a similar error. The only thing I can reliably search by is ID:

query Articles {
    article (  id: "9"  ) {
        title
    }
}

This correctly retrieves the targeted article's information. Where am I going wrong here? I've tried all sorts now.

See my query / response here. Thanks a lot for any help.


Solution

  • with the find (aka the plural form, as the singular uses the findOne) you have to use a where filter:

    query Articles {
        article (where: { slug: "a" }) {
            title
        }
    }