Spring Boot GraphQL Tutorial #15 - SelectionSet

preview_player
Показать описание
A very powerful feature of graphql spring boot is that you have access to the query's selection set. A selection set is the set of fields the user requested.

Having direct access to the fields can enable you to make performance optimized queries requesting only the needed data. For example an SQL backed system may be able to use the field sub selection to only retrieve the columns that have been asked for.

If you look ahead in the selection set, you may be able to optimize further and collapse two backend resource calls into one. For example, if you can retrieve the sellingLocations data within the products API call. You can group everything into one API query instead of two.

query {
products {
# the fields below represent the selection set
name
description
sellingLocations {
state
}
}
}

The selection set (DataFetchingFieldSelectionSet) contains many useful utility methods such as: contains, containsAnyOf, containsAnyOf. These can be used as the predicate to make your API call selection.

To get the requested field names you can stream the fields, filter and collect into a set.

See you in the next episode!

Cheers!
Philip

Рекомендации по теме
Комментарии
Автор

Had a question around the best way to implemet of Unions.
Considering the below schema and the request.

Use Case: The GraphQL server should return only the requested Test.

Do I have to search for the 'SoftTest' field in the GraphQL request and then return the appropriate result in the response ? Is there a better way to implement the above use case or I have to use SelectionSet here ?

Thank you



Schema

union Test = HardTest | SoftTest

type HardTest {
value : String!
}

type SoftTest {
value: String!
}

type Query {
tests: [Test!]!
}


Request


{
tests {
... on SoftTest {
value
}
}
}

rahulkulkarni
join shbcf.ru