Getting Complete Query String From Inside Of Resolver

- 1 answer

I'm new to nodejs and apollo server, so dont judje me.

Problem sounds exactly same as title: "how to get graphql string inside resolver function?".

Actually, you have four args in every resolver: parent, args, context, info. Some info here:

I made my mind to write function, that gathers nested object inside context to regenerate query string. Why I need it? Good question. I'm writing microservice, so when i got nested query to field wich is outside from current microservice I pass query by http.

My resolver:

eventByID: async (root, args, context) => {
const event = await EventModel.findById(;
event.creator = await nestedContextProvider(context, 'creator', event.creator);
return eventFascade(event); //just fascade for object - nothing serious


It refers to nestedContextProvider for solving nested context:

const nestedQueryTraverser = (nestedQueryArray) => {
const nestedQueryTraversed = => (
element.selectionSet === undefined
  : `${}{${nestedQueryTraverser(element)}}`));
return nestedQueryTraversed;

const nestedContextProvider = async (context, checkField, ID) => {
if (context.operation.selectionSet.selections[0].selectionSet.selections
.find(selector => === checkField)) {
let nestedFieldsArr = context.operation.selectionSet.selections[0]
  .selectionSet.selections.find(selector => === checkField);
nestedFieldsArr = nestedQueryTraverser(nestedFieldsArr);
const a = (await users(ID, nestedFieldsArr));
return ID;

So it works for me, but I know there must be better solution.

Any ideas?



The graphql package includes a print function that takes any AST and returns the string representation, so you can do something like this:

const { print } = require('graphql')

function anyResolver (parent, args, context, info) {
  const operationString = print(info.operation)
  // Fragments are not included in the operation, but we still need to print
  // them otherwise our document will reference non-existing fragments
  const fragmentsString = Object.keys(info.fragments)
    .map(fragmentName => print(info.fragments[fragmentName]))
  const documentString = `${operationString}\n\n${fragmentsString}`