Create Apollo-Server with Federated schema

637 Views Asked by At

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,
     }
  ]),
})
2

There are 2 best solutions below

3
Glen Thomas On BEST ANSWER

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)
  }]
})
2
Glen Thomas On

You do not need to use the gql function. The schema has already been parsed.

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

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

const server = new ApolloServer({
 schema: buildSubgraphSchema([
     {
        typeDefs: typeDefs,
        resolvers,
     }
  ]),
})