Using apollo in node - (no react)
Asked Answered
A

2

7

I want to use apollo to do some batch updates. And I need to start the batch script from node. eg node myscript.js

I can't figure out how to do this.  Is there a simple example that does what is described on the Get started: https://www.apollographql.com/docs/react/get-started/ but without react ?

(My code is working just fine when using react)

Astrionics answered 19/10, 2020 at 13:34 Comment(0)
A
17

This is how to use Apollo Client ver 3.x from node vs using it from React

To use it from node you must import from @apollo/client/core in React you import from @apollo/client So initializing in node can look like this:

const STRAPIURI = "http://192.168.39.174:1337/graphql";
import fetch from 'cross-fetch';
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client/core';

const apolloClient = new ApolloClient({
  link: new HttpLink({
    uri: `${STRAPIURI}`, fetch,
    credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
  }),
  cache: new InMemoryCache()
});

You also need to import gql from the same location.

import { gql } from '@apollo/client/core';
Astrionics answered 28/10, 2020 at 8:33 Comment(1)
However, it does not work with subscribe to an apollo server.Reitman
P
2

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.

Panhandle answered 20/10, 2020 at 21:23 Comment(4)
From what I understand the code example is for apollo client version 2.xAstrionics
From what I understand the code example is for apollo client version 2.x I wanted to use version 3 of apollo client. This is the version I'm using in my react code and for simplicity I'd like to use the same version in the node part. On the migration page apollographql.com/docs/react/migrating/… there is a section "Using Apollo Client without React". There it says that import should be done like this: import { ApolloClient } from '@apollo/client/core';Astrionics
@TerjeChristensen yes the above code is written considering apollo v2. I think version 3 will require some changes, but I have not tested it by myself.Panhandle
Yes v3 is a different thing. I was using V2.x in my react part, but changed to V3. This was not to hard. But changing the node part is not documented. If you look at the npm page for v2 npmjs.com/package/apollo-client There is a section "Learn how to use Apollo Client with your favorite framework" The link "Vanilla JS" points to a non existing page.Astrionics

© 2022 - 2024 — McMap. All rights reserved.