Let's say I have a Quote type. I'd like to be able to retrieve a random quote. I can get all the quotes using the following query:
query {
allQuotes {
id
text
}
}
How do I get a random record from the Quotes
It may be a good idea in your simplest case to just define getRandom resolver on the root-level Query type.
Another approach, given that you say you already have a Quote type defined, would be to define a resolver inside it.
Then, for the query from your example, the schema may resemble something like (I didn't test it):
type Quote {
id: Int!
text: String!
getRandom(): Quote
}
type Query {
allQuotes(): [Quote]
}
There is also a good example related to randomness in the official docs of GraphQL, try having a look at the Object Types section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With