So I would like to know if I could access to nested fields in a directive function.
Let’s say I have a type Course like this:
type Course @key(fields: "_id") {
_id: ID
title: String
}
and in another subgraph I extend this type to add a field:
extend type Course @key(fields: "_id") {
_id: ID @external
totalProgression: Int
}
And I have a query Courses on which I add a directive @sort:
Courses: [Courses] @sort
When I query Courses:
Courses {
_id
title
totalProgression
}
I get the correct result. But the problem is in the directive function:
function sortDirectiveTransformer(schema, directiveName) {
return mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
let directive = getDirective(schema, fieldConfig, directiveName)
if (directive && directive[0]) {
const { resolve = defaultFieldResolver } = fieldConfig
fieldConfig.resolve = async function (source, args, context, info) {
const result = await resolve(source, args, context, info)
console.log(result)
return result
}
return fieldConfig
}
}
})
}
When I console.log(result) I don’t get the “totalProgression” field, I only get the “_id” and the “title”.
That’s because “totalProgression” isn’t returned in the “Courses” query, but it is resolved elsewhere.
I was wondering if there would be any way I could get the nested field in the directive function.
The objective here is to sort the courses based on their totalProgression.