ApolloGateway with @defer

Is it possible to use defer with ApolloGateway?

I have this config on my ApolloGateway:

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:

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:

  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:

 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

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

1 Like

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

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

1 Like

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

1 Like