i am using apollo GraphQL for backend and frontend . i want to do dynamic selection of fields which is selected by client .
so with the help of info argument i get i created a function which give me selected fields by client based on that i fetched selected fields from db.
here is my implementation
in resolver
selection.fields = .extractSelection(info?.fieldNodes[0]?.selectionSet)
function implementation
.extractSelection = (selectionSet) => {
const subSelection = {}
if (selectionSet) {
selectionSet?.selections?.forEach(selection => {
if (selection?.selectionSet) {
if (selection.kind === 'InlineFragment') Object.assign(subSelection, .extractSelection(selection?.selectionSet))
else subSelection[selection?.name?.value] = .extractSelection(selection?.selectionSet)
} else {
subSelection[selection?.name?.value] = 1
}
})
}
return subSelection
}
i have some Doubt regarding this implementation .
is it good to execute this function for field selection per request?
is another way exist to do implementation of this thing?
I just need to know is it good way or what is other possible soloutions.