# ApolloGateway with @defer

**URL:** https://community.apollographql.com/t/apollogateway-with-defer/7399
**Category:** Server
**Tags:** server, federation
**Created:** [April 18, 2024, 4:38pm UTC](https://community.apollographql.com/t/apollogateway-with-defer/7399 "2024-04-18T16:38:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![michaeldouglasdev](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/michaeldouglasdev/32/4911_2.png) [@michaeldouglasdev](https://community.apollographql.com/u/michaeldouglasdev)
#### Post date: [April 18, 2024, 4:38pm UTC](https://community.apollographql.com/t/apollogateway-with-defer/7399/1 "2024-04-18T16:38:11Z")

</div>

Is it possible to use defer with ApolloGateway?

I have this config on my ApolloGateway:

```auto
const gateway = new ApolloGateway({
    supergraphSdl: new IntrospectAndCompose({
      subgraphs: SubgraphContainer.list().map(({name, url}) => ({ name, url })),
    }),
    buildService: ({ name }) => {
      const subgraph = SubgraphContainer.get(name)
      const datasource = buildGraphQLDataSource(subgraph);

      return datasource;
    },
    __exposeQueryPlanExperimental: true,
    queryPlannerConfig: {
      incrementalDelivery: {
        enableDefer: true,
      }
    },
  })

```

But @defer does not works  
Error: Unknown directive "@defer".

I have installed graphql 17 using:

```auto
npm i --legacy-peer-dep graphql@17.0.0-alpha.2

```

In another project without Apollo Gateway, I just needed to add this snippet (including graphql 17) to work:

```auto
  const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
  })

  const transformedSchema = new GraphQLSchema({
    ...schema.toConfig(),
    directives: [...schema.getDirectives(), ...specifiedDirectives, GraphQLDeferDirective]

  })
  const server = new ApolloServer<Context>({
    schema: transformedSchema
  })

```

I have some directives and I apply them like this:

```auto
 const directiveTransformers = [
  //...
];

const transformedSchema = directiveTransformers.reduce((schema, transformer) =>
  transformer(schema), buildSubgraphSchema({
    typeDefs,
    resolvers,
    })
  )

```

I can’t add GraphQLDeferDirective from the ‘graphql’ package to my directiveTransformers array, because GraphQLDeferDirective is of type GraphQLDirective, and my transformers (created using mapSchema from ‘@graphql-tools/utils’) return GraphQLSchema

---

<div class="post-metadata">

### Author: ![dkuc](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/dkuc/32/2892_2.png) [@dkuc](https://community.apollographql.com/u/dkuc)
#### Post date: [April 19, 2024, 4:10am UTC](https://community.apollographql.com/t/apollogateway-with-defer/7399/2 "2024-04-19T04:10:31Z")

</div>

`@defer` is not supported by the Apollo Gateway and we only support it in the Router.

---

<div class="post-metadata">

### Author: ![michaeldouglasdev](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/michaeldouglasdev/32/4911_2.png) [@michaeldouglasdev](https://community.apollographql.com/u/michaeldouglasdev)
#### Post date: [April 19, 2024, 1:19pm UTC](https://community.apollographql.com/t/apollogateway-with-defer/7399/3 "2024-04-19T13:19:52Z")

</div>

Im using Apollo Gateway because I have some LocalGraphQLDataSources.  
Any suggestions for me to use router with LocalGraphQLDataSource? (local resolver and local schema)

---

<div class="post-metadata">

### Author: ![abernix](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/abernix/32/10_2.png) [@abernix](https://community.apollographql.com/u/abernix)
#### Post date: [April 22, 2024, 12:03pm UTC](https://community.apollographql.com/t/apollogateway-with-defer/7399/4 "2024-04-22T12:03:09Z")

</div>

@michaeldouglasdev We do not have an official recommendation for writing resolvers into the Router itself. `LocalGraphQLDataSource` was initially primarily intended to be used for testing but, overall, just wasn’t a very popular request since it prevented more practical and necessary independent scaling of subgraph services. There are some use-cases where it could make though!

We **don’t** recommend doing this at this time, however, creating something similar to `@apollo/gateway`’s `LocalGraphQLDataSource` with a truly local resolver and local schema in the Router **could** be achieved by using a Rust-GraphQL library (like `async-graphql`) and using the “`ControlFlow::Break`” pattern in a `subgraph_service` Router plugin. Alternatively, you maybe be able to do this as a coprocessor, but we would generally just recommend externalizing this to a minimal subgraph that lives along-side the Router (as a side-car in Kubernetes-land, or potentially a separate service if it necessitates it) which facilitates observing and scaling such a subgraph separately from the Router itself.

Hope this helps guide you in that direction should you choose to. As mentioned the Gateway does not support `@defer`.

---

<div class="post-metadata">

### Author: ![shanemyrick](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/shanemyrick/32/5474_2.png) [@shanemyrick](https://community.apollographql.com/u/shanemyrick)
#### Post date: [April 24, 2024, 4:43pm UTC](https://community.apollographql.com/t/apollogateway-with-defer/7399/5 "2024-04-24T16:43:16Z")

</div>

You could implement an override subgraph with Router coprocessors too: [https://www.apollographql.com/docs/router/customizations/coprocessor/](https://www.apollographql.com/docs/router/customizations/coprocessor/)
