'How to get graphql requested fragment fields?

I can get normal requested fields like this:

const MyQueries = {
  allCustomers: (parent, args, context, info) => {
    const keys = info.fieldNodes[0].selectionSet.selections
      .map((item) => {
        if (!item.selectionSet) {
           return item.name.value;
        }
      })
      .filter((e) => e)
      .toString();

    console.log(keys);
  },
  ...
};

My request query:

  query RootQuery{
    getAllCustomers{
      id
      username
      mobile
      _suspended
      comments{
          id
          comment
      }
    }
  }

logs: id,username,mobile,_suspended

In my use case here I don't need comments with its nested fields that is why I filtered them out by if statement.

But when use graphql fragments:

query RootQuery{
  getAllCustomers{
    ...myFragment
  }
}

fragment myFragment on Customer {
    id
    username
    mobile
    _suspended
}

I get: myFragment as output, but expected output is: id,username,mobile,_suspended.

How can I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source