-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic resolvers based on express request #2560
Comments
To get the v1 functionality, you need to create a custom class You are basically skipping the server's You have to do similar for subscriptions, since those requests take a different path. Note the import { renderPlaygroundPage } from '@apollographql/graphql-playground-html'
import { ApolloServer, makeExecutableSchema } from 'apollo-server-express'
import { graphqlExpress } from 'apollo-server-express/dist/expressApollo'
class DynamicServer extends ApolloServer {
applyMiddleware({ app, path }) {
app.use(path, async (req, res, next) => {
if (ENABLE_PLAYGROUND) {
// copy playground code from apollo applyMiddleware source
}
const customSchema = getYourCustomOrCachedSchemaBasedOnRequest()
// this.context is the current server / request context, you can keep or override below
const contextObj = { ...this.context, request: req } // ...other context etc
const gqlOptions = {
...this,
graphqlPath: path,
schema: await makeExecutableSchema(customSchema),
context: contextObj,
}
return graphqlExpress(super.createGraphQLServerOptions.bind(gqlOptions))(
req,
res,
next
)
})
} |
I'm going to close this in lieu of the above suggestion. It's also worth noting that, using the Express integration (i.e. |
@abernix Based on your last statement, are you saying that JIT schema generation and resolver mapping is supported by |
Hello everybody. I tried to follow the described solution above, but I realised that the It runs now, but the newly generated schema isn't picked up, but instead the default schema that I pass in when I init the server is picked up: So it seems like my newly generated schema can't override the initial one (which I have to pass in, otherwise it throws an error) Does maybe someone know what I am missing here?
|
In version 1 of apollo-server-express, the way to pass the schema and resolver set was to return the information in the
graphqlExpress
util, like:With this setup, it used to be easy to create dynamic resolver sets, based on the context of the express request.
In version 2 of apollo-server-express, schema's are provided when creating a new ApolloServer, before the server is actually running. Because of this change, we loose the ability to have this dynamic resolver setup.
How to accomplish this in version 2 (apart from shifting the logic to the resolvers itself) or why was support dropped for this?
The text was updated successfully, but these errors were encountered: