arackaf / mongo-graphql-starter

Creates a fully functioning, performant, extensible GraphQL endpoint from a Mongo DB. Supports middleware, Mongo 4 transactions.
MIT License
423 stars 29 forks source link

Fragment Support #55

Closed natac13 closed 4 years ago

natac13 commented 4 years ago

Hey Adam,

Just opening some issues that I am wondering about as I attempt to implement the same logic you have from queryUtils.js and projectUtils.js

Are fragments supported when it comes to building the $project stage in getNestedQueryInfo and getSelections

When I was writing it I came to that stumbling block and had to do something like the following.

function getSelections(fieldNode, fragments) {
  return R.reduce((acc, sel) => {
    if (sel.kind === 'FragmentSpread') {
      return {
        ...acc,
        ...getSelections(fragments[sel.name.value]),
      };
    }
    return {
      ...acc,
      [sel.name.value]: R.isNil(sel.selectionSet)
        ? 1
        : { ...getSelections(sel) },
    };
  }, {})(R.pathOr([], ['selectionSet', 'selections'])(fieldNode));
}

I have changed from using the Map to an object like so { fieldA: 1, nested: { fieldB: 1 } } I am now passing in an fragments from ast in getNestedQueryInfo and then checking if the sel.kind is of FragmentSpread, if so pass in the Fragments fieldNode to getSelections

I just wanted to see your thoughts first, before I make more PRs to your project.

arackaf commented 4 years ago

I don't see any reason at all why that wouldn't work in principle. If you can get it working I'd love to add that - PR's welcome!

arackaf commented 4 years ago

PR is in progress so I'm closing this issue in favor of PR discussions