First you need create an apollo client, very similar to react.
Check the below code.
import { ApolloClient } from 'apollo-client';
import fetch from 'node-fetch';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
import config from './config';
const httpLink = createHttpLink({
uri: `${config.apiUrl}/graphql`,
fetch,
});
const authLink = setContext(() => {
const token = config.serverToken;
return {
headers: {
Authorization: token,
},
};
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
export default client;
Once is the client is done, you can use the below code to do a mutation
import gql from 'graphql-tag';
const mutation = gql(`mutation {
CreateTodo(
data: {
task: "${task}"
}
) {
id
task
}
}`);
const response = await client.mutate({mutation});
You can similarly use it for query as well. We are using node fetch to make the query or mutation.
Let me know if it helps.