2
votes

Vendors like GitHub are beginning to provide their own GraphQL APIs as an alternative to the existing REST APIs. However many apps shouldn't talk to these APIs directly and instead talk to first-party servers that in turn speak to these APIs.

With REST APIs it's trivial to proxy an entire third-party API from a single endpoint. But the existing GraphQL libraries all seem to need to know the full GraphQL schema in advance and there doesn't seem to be any way to specify queries or types to be defined at run time.

The even bigger problem seems to be that the entire GraphQL query will be parsed in advance. In order to pass on a sub-query to a third-party API that GraphQL fragment would have to be preserved or reconstructed.

Here's an example that doesn't work:

new gql.GraphQLObjectType({
  fields () {
    return {
      thirdPartyApiCall: {
        type: '???', // Type is actually defined upstream
        resolve () {
          // GraphQL sub-query is already parsed at this point
          return thirdPartyGraphQLApi(graphQLSubQueryGoesHere)
        }
      }
    }
  }
})

I think part of the answer could be to use introspection queries to generate the type in this example dynamically, once, when the schema is loaded (though of course this means it won't reflect changes upstream unless it is re-generated frequently) but I'm at a complete loss as to how to get the original GraphQL sub-query to pass on to the third party.

Is this currently solvable in GraphQL without writing a custom GraphQL parser/handler or does GraphQL currently not support this kind of delegation (i.e. wrapping 3rd party GraphQL APIs directly)?

1

1 Answers

2
votes

User @jbinto from the GraphQL community Slack helpfully pointed me at this currently unsolved GitHub issue on the graphql-js implementation: https://github.com/graphql/graphql-js/issues/490

Currently the answer seems to be "it can't be done" but the maintainers are looking into it, now that GitHub created the first major public GraphQL API.

It's worth following the development of the issue on GitHub and seeing what solutions arise from the discussion. The driver may support this scenario in the future or a userland solution may come along that solves it. There are a lot of open questions as to the specifics at the moment.