skip to Main Content

I’m having a hard time making Apollo mutations from server to server.

I’ve spent quite a while today looking around for examples or suggestions on best practice for passing data between two API’s that are built with @apollo/server. One is a static express server and one is an AWS Lambda function using the @as-integrations dependency.
The static server supports a consumer site for my business and will be the first recipient of data from a mutation from the client, then will pass the data along to the lambda function for my CRM. It’s important to me that the data goes to the static server first then to the serverless function.
My first thought was to create an ApolloClient instance in the resolver within my static server then make a request to the lambda function, but I’m running into dependency issues because I haven’t installed React on my server. I understand the ease-of-integration for ApolloClient use in a client, but I should still be able to create a client within a server for API-to-API communication right?
Most of the information I’ve found so far is explaining how to make REST requests with Apollo using their methods, but is that the only/recommended way to communicate between API’s? Thank you for any insight or help you can offer!

2

Answers


  1. You don’t need any of the react stuff to use Apollo Client. You want to use client.query() instead of useQuery (the latter is a hook meant for use in React applications. This pattern is not particularly well documented in the Apollo client docs, there’s but one example but this is the pattern you must use in the server-server case.

    client.query returns a promise so you’ll have to let it resolve before using the results.

    client
      .query({
        query: gql`
          query MyQuery {
            id
            more fields…
          }
        `,
      })
      .then((result) => console.log(result));
    
    Login or Signup to reply.
  2. It turns out the issue was resolved by importing ApolloClient, etc. not from @apollo/client, as these imports have an inherent react dependency, but from @apollo/client/core.
    Like this:

    import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client/core';
    

    Not this:

    import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search