Multiple graphQL queries?

I can’t seem to find documentation, either Apollo, DatoCMS or general graphQL, on how to do multiple queries?

I understand for example it’s not currently possible to filter on a linked or nested value but I can split it into 2 queries, get the IDs in the first query and follow up with a filter by ID. But I can’t find any reference for the syntax for that.

For example I might want to do something like this, to query 2 different models and use the data in my template. Or possibly data from one in variables to use in the other. Any ideas?

export default {
    apollo: {
        allProperties: {
            query: gql`{
                allProperties {
                    bathrooms
                    beds
                    receptions
                }
            `} 
        },
        collection: {
            query: gql`{
                collection {
                    name
                    id
                }
            `} 
        }
    }
}
1 Like

This is more like what I’m trying to achieve…

query MyQuery {
  collection(filter: {url: {eq: $slug}}) {
    summary
    id
    name
  }
  allPlans(filter: {collection: {in: "[id of collection from above]"}}) {
    beds
    bathrooms
    garages
  }
}
1 Like

Hello @jacktcunningham_publisher (or @jack1 )

Because of the way that graphQL works, fields with the same parent will resolve at the same time making it impossible to directily refrence the result of a parallel query onto another, so we can’t really do this in a single request without modifying the graphQL schema.

There are however some options to achieve the desired behaviour as discussed in this article

Applying one of those options to your example we would get something like:

const { data: collectionData } = useQuery(collection, {
  variables: { slug }
});
const collectionId = collectionData?.id;

const { data: allPlansData } = useQuery(allPlans, {
  variables: { collectionId }
});

Or, as shown in Option 3 of the article, you can also use Apollo Link to handle variable passing for you.

Hope this helps!

2 Likes

This is great thanks! I’ll try option 2…
But as a sense check, if I have a fairly straight forward case where I want to organise one content type into categories, either using the url or with computed properties with the user filters. It makes sense that these categories are their own content type which will make them nested. So in this scenario I’d want to filter the query based on a nested value… That seems like a common use case, should this be the go to method in these cases?

1 Like

For now these are the ways to filter for nested values, as it is a current limitation on the system.
There is however an ongoing feature request for it at: Deep filtering on both CMA and CDA

Consider upvoting it if you think it would be a nice addition!

2 Likes

Hi, I’m revisiting this problem, I thought I had avoided it by waiting for !$apollo.queries.collections.loading but it was only rendering in the browser, not on the server.

Can you possibly explain in your example, where this goes in the context of my queries? Ie I don’t understand where I would be declaring consts in the Apollo syntax.

If there was a full example somewhere it would be super helpful.

BTW I upvoted the deep filtering. I’m not sure if it’s Apollo but this scenario definitely makes graphql seem weird and hard vs rest apis

SOLVED!
I solved this in the end using the Nuxt Vuex Store. By grabbing an index of the model I needed in the store I was able to find the id I needed to filter the second query.

1 Like

Hello @jacktcunningham_publisher

Sorry for the delayed response due to the holidays.
I’m glad to know it’s working now!
Let me know if you have any other doubts related to this