DEV Community

Harshit Kumar
Harshit Kumar

Posted on • Updated on • Originally published at

Creating a Proxy for your GraphQL Server

I needed to create a proxy server for my GraphQL service to be able to make some decisions before reaching my service.

This article will document how you can do the same with minimal amount of code ๐Ÿ˜
We'll be leveraging awesome utilities provided by graphql-tools


npm init -y
npm i @graphql-tools/delegate @graphql-tools/utils @graphql-tools/wrap apollo-server cross-undici-fetch graphql typescript
Enter fullscreen mode Exit fullscreen mode

Create an executor

Executor is a function used capable of retrieving GraphQL results (both introspection & fetching results during a query's execution).

In the executor, I've defined the GraphQL service's URL which I want to proxy - (an open API to fetch your favourite Anime characters)

const executor =  async ({ document , variables, context }: {document: any, variables: any, context: any}) => {
    const query = print(document)
  const fetchResult = await fetch('', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    body: JSON.stringify({ query, variables })
  return fetchResult.json()
Enter fullscreen mode Exit fullscreen mode

Define applicationProxyResolver

This function is passed to wrapSchema method. In this method, you should be able to make some decisions. For example - I wanted to validate some headers & only honour the requests if they had these headers. We'll use delegateToSchema to delegate the entire request to our original GraphQL service.

export const applicationProxyResolver = ({
}: {
    subschemaConfig: any,
    operation: any,
    transformedSchema: any,
}) => {
    return (_parent: any, _args: any, context: any, info: any) => {
        return delegate.delegateToSchema({
            schema: subschemaConfig,
            operationName: info!.operation!.name!.value,
Enter fullscreen mode Exit fullscreen mode

Let's fire up the graphQL proxy server ๐Ÿš€

const init = async () => {
    const schema = wrapSchema({
        schema: await introspectSchema(executor),
        createProxyingResolver: applicationProxyResolver

    const server = new ApolloServer({ 

    // The `listen` method launches a web server.
    server.listen(4001).then(({ url }) => {
        console.log(`๐Ÿš€  Server ready at ${url}`);


Enter fullscreen mode Exit fullscreen mode


You can find this code at Github.

Is there an easier way to do this? Please comment & let me know ๐Ÿ˜

Top comments (0)