Newtt
Newtt

Reputation: 6190

Use Apollo server as a pass through of a query from client

I have a use case where I have apollo-server-express running with a React based apollo-client. I have an external graphql-datasource for some queries. Currently, I've configured apollo-datasource-graphql to be used as a data source for my apollo-server-express. However, this requires duplication of work on the resolver in Apollo as well as the resolver on my external graphql system.

Is there a way for me to pass queries made in the client through the Apollo Server and to the external graphql data source?

Upvotes: 0

Views: 699

Answers (1)

Herku
Herku

Reputation: 7666

Maybe you could access the GraphQL AST from the fourth resolver argument (resolveInfo) and pass it into a GraphQL client?

Here is some prototype code:

import { print } from 'graphql/language/printer';

function forwardOperationResolver(root, args, context, resolveInfo) {
  return fetch('https://remote.host/graphql', {
    method: 'POST',
    body: JSON.stringify({
      query: print(resolveInfo.operation),
      variables: resolverInfo.variableValues,
    }),
  })
    .then(response => response.json())
    .then(response => {
      if (response.errors) {
        // Handle errors
      }
      return response.data;
    });
}

Downside: This breaks a few things that usually work in GraphQL like partial results and error locations...

Upvotes: 1

Related Questions