express-graphql icon indicating copy to clipboard operation
express-graphql copied to clipboard

Reliance upon graphqlParams being mutable

Open slaymance opened this issue 6 years ago • 0 comments

I'm currently working on a GraphQL implementation where we allow clients to send us a queryId which we use on the server to lookup a specific graphql query, as opposed to sending the API a query the client has created themselves.

In order to use a GraphQL query looked up at runtime on the server, we set it directly to the graphQLParams.query argument of the graphqlHTTP options function. As an example:

~Client request~
POST myserver.com/graphql?queryId=helloWorld

~Server side~
const persistedQueries = {
  helloWorld: `query { helloWorld }`
};

app.use('/graphql', graphqlHTTP(req, res, graphQLParams) => {
  graphQLParams.query = persistedQueries[req.params.queryId];

  return { schema };
});

In this example, graphQLParams.query is explicitly set with a persisted query looked up at runtime, which is then the query that gets run against the GraphQL schema, as opposed to relying upon the client to send its own query.

Is the graphQLParams argument something that should be mutable? Can we rely upon the argument being directly mutable to achieve what we do here? Thanks for any help!

slaymance avatar Oct 24 '18 20:10 slaymance