I’ve seen graphql implementations that make use of the top level extensions
property of the response to include information such as the version number of the api in every response. For example:
{
"data": {
"user": {
"id": "123",
"name": "John Doe",
"email": "john.doe@example.com"
}
},
"extensions": {
"version": "1.2.3",
"timetamp": "2025-05-06T10:41:03.43"
}
}
Although this topic is covered in the graphql spec and briefly alluded to in the apollo docs I don’t really see how to implement it using Apollo server.
Does anyone have any examples of how to do this?
Our environment is
node: v20.18.0
@apollo/server: 4.11.2
Welcome @dland2000!
Is the set of extensions you want to send standard across all operations/resolvers or is it dynamic?
If it is standard, the easiest way would be to use a plugin and hook into willSendResponse
to modify the extensions on the way out. Something like…
plugins: [
{
async requestDidStart() {
return {
async willSendResponse({ response }) {
response.extensions = {
...response.extensions,
version: "1.2.3",
timestamp: new Date().toISOString(),
};
}
};
}
}
]
Thank you for the info. Yes I want it to be standard across all responses. I did get it working but I had to use a small tweak to your proposed solution:
plugins: [
{
async requestDidStart() {
return {
async willSendResponse(requestContext) {
const { response } = requestContext;
if (response.body.kind === 'single') {
response.body.singleResult.extensions = {
version: '1.2.3',
timestamp: new Date().toISOString()
};
}
}
};
}
}
]
Thanks again for the help.