Apollo-server: [hapi] Dynamically fetch schema on request

Created on 14 Apr 2017  路  1Comment  路  Source: apollographql/apollo-server

Hi guys,
I've one question about graphQL server (using it with Hapi):
On my project, user can create entities types and add new fields.
These kind of actions generates a new schema with these new informations.
Re generating the schema is not an issue, but I don't know how to tell the server to use this new version.

Basically, all I would like is, in plugin options, being able to pass a function to retrieve the schema and not the schema itself.

Is there a way to do that? Or a way to "refresh" the server when the schema is updated, without having to fully restart the server.

Thanks!

Most helpful comment

Finally resolved this myself.
The graphqlOptions when you register your plugin accepts a function returning an object.

    server.register({
        register: graphqlHapi,
        options: {
            path: '/graphql',
            graphqlOptions: () => ({ schema: graphqlService.schema })
        }
    });

This function will be called on each query, so you can fetch a fresh schema on each query.
Make sure you have some caching somewhere to avoid generating the whole schema each time you receive a request.

>All comments

Finally resolved this myself.
The graphqlOptions when you register your plugin accepts a function returning an object.

    server.register({
        register: graphqlHapi,
        options: {
            path: '/graphql',
            graphqlOptions: () => ({ schema: graphqlService.schema })
        }
    });

This function will be called on each query, so you can fetch a fresh schema on each query.
Make sure you have some caching somewhere to avoid generating the whole schema each time you receive a request.

Was this page helpful?
0 / 5 - 0 ratings