0

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?

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Newtt
  • 6,050
  • 13
  • 68
  • 106
  • 1
    Apollo Federation ? – xadm Jun 07 '20 at 15:25
  • @xadm That seems to help but I'm not sure how I would write my resolvers if I need to pass the query to the external Graphql source – Newtt Jun 07 '20 at 19:08
  • 1
    It links external graphql into your server, all external resources/resolvers should be available in main server - just configure it (docs?), no need to write any resolvers? – xadm Jun 07 '20 at 19:23
  • Apollo federation links multiple servers under your control into a single schema. It might not be the thing OP wants here. – Herku Jun 08 '20 at 14:06

1 Answers1

1

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...

Herku
  • 7,198
  • 27
  • 36