typescriptgraphqlapollo-servergraphql-tools

Create Apollo-Server with Federated schema


I am trying to create an Apollo-Server with a Federated Schema of TypeDefs loaded from a GraphQL file. However, I am getting this error

Argument of type 'GraphQLSchema' is not assignable to parameter of type 'string | TemplateStringsArray'.

I believe that I am loading the schema from the file with the wrong function loadSchema. How can I load the schema from the file with the correct type to feed it as TypeDefs for the Federated Schema?

import { buildSubgraphSchema } from '@apollo/federation';
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
import { loadSchema } from '@graphql-tools/load';
import { ApolloServer, gql } from 'apollo-server';
...

const typeDefs = await loadSchema(`${__dirname}/auth.graphql`, {
  loaders: [new CodeFileLoader()],
});

const server = new ApolloServer({
 schema: buildSubgraphSchema([
     {
        typeDefs: gql(typeDefs),    // Error here `typeDefs`
        resolvers,
     }
  ]),
})

Solution

  • I am also loading the subgraph schema from file and do it like this:

    import { parse } from "graphql";
    import { ApolloGateway } from "@apollo/gateway";
    import * as fs from "fs";
    
    const sdlStringFromFile = fs.readFileSync(
            `${__dirname}/auth.graphql`
        );
    
    new ApolloGateway({
      localServiceList: [{
        name: "ServiceA",
        url: "https://servicea.com/graphql",
        typeDefs: parse(sdlStringFromFile)
      }]
    })