gramps-graphql / gramps--legacy

The core data source combination engine of GrAMPS.

Home Page:https://gramps.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using prepare and extraContext is confusing

wujashek opened this issue · comments

Thanks for a great library !

While checking how to extract schema from gramps I've found great example here: https://gramps.js.org/api/gramps/

I had an issue with this part, seems context doesn't get a request object in such setup:

app.use('/graphql',
  bodyParser.json(),
  gramps.addContext,         // Add the extra context
  graphqlExpress({
    schema,                  // Use the merged schema...
    context: gramps.context, // ...and the GrAMPS context object
  }),
);

So I had to rewrite it to:

app.use('/graphql',
  bodyParser.json(),
  gramps.addContext,         // Add the extra context
  graphqlExpress(req => ({
    schema,                  // Use the merged schema...
    context: gramps.context(req), // ...and the GrAMPS context object
  })),
);

Also I've noticed extraContext callback is called twice for a single query.

addContext: (req, res, next) => {
      req.gramps = getContext(req);
      next();
    }

Maybe the idea is to pass req.gramps to apollo express middleware as config object ?

It would be great to see some clarification on above so I can work on PR to improve that ?