Search code examples
graphqlapolloapollo-server

Apollo GraphQL: Call a Mutation from the Server?


I need to call a mutation from a cron job running on the server. I found this SO post, but the accepted answer said there was no way to do it.

Then I found this on GitHub, from 2017:

graphql-server is a network wrapper for graphql core function. if you don't want to use it over network, you can just run it standalone:

import scheme from './scheme';

const query = `{
  me {
    name   
  }
}`;

const result = graphql(scheme, query);
console.log(result); 

function documentation can be found here

That looks pretty good! Is that the best practices approach in 2020?


Solution

  • I just found out about this approach as well. It's possible to create an Apollo client directly on the server.

    export const getApolloServerClient = () =>
    
      new ApolloClient({
    
        ssrMode: true,
    
        cache: new InMemoryCache(),
    
        link: new SchemaLink({ schema }),
    
      });