How to change typeDefs and resolvers after startup when using apollo server?

I have hello world code with apollo server:

const startApolloServer = async (typeDefs: any, resolvers: any): Promise<void> => {
    const app = express();
    const httpServer = http.createServer(app);

    const config = {
        typeDefs,
        resolvers,
        // csrfPrevention: true,
        plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
    };

    const server = new ApolloServer(config);  

    await server.start();
    server.applyMiddleware({ app });
    await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
    console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
};

I can set typeDefs and resolvers during the boot proces in my app.

But is there any way how to change schema/resolvers after startup? For example in every request? Or apply some request middleware to resolve schema/resolvers?

Thank you

That’s not something Apollo Server supports.

Would you mind sharing a bit more about your use case? Why do you want the schema to change per request? My first impression is “you probably don’t actually want to do that”, it sounds unsafe and I don’t understand why you would want to. But I’m genuinely curious to hear your thoughts!

I am trying to build gql schema on the fly.

For example I have mutation to create new Query - I can change resolvers (using javascript proxy), but I can’t change schemas :frowning:

That’s an interesting idea, having the resolvers being resolved by proxy. Can you maybe share some more insights into why your clients of the graph would want to mutate the graph themselves?

What benefits do they get from being able to change the own API they are calling? Why not just run the code that you are saving via proxy themselves?


That being said, while you can not modify the schema in a single Apollo Server at runtime, through Apollo Federation and Apollo Uplink you can have a gateway instance that reloads it’s schema without server restarts whenever a subgraphs schema is updated. You could trigger this schema update through a Rover CLI command that anyone could run, even your clients