0

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,
     }
  ]),
})
Hazem Alabiad
  • 1,032
  • 1
  • 11
  • 24

2 Answers2

0

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,
     }
  ]),
})
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • When I remove the `gql` function, I mm getting another error `Type 'GraphQLSchema' is missing the following properties from type 'DocumentNode': kind, definitionsts(2739)` – Hazem Alabiad Sep 14 '21 at 09:22
  • I believe that `buildSubgraphSchema` is expecting the `typeDefs` to be of type `DocumentNode`. So, I guess that we either need to use the correct function when loading or convert `graphqlschema` to `documentnode` – Hazem Alabiad Sep 14 '21 at 09:26
0

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)
  }]
})
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • Is this way the best practice to upload the subgraphs? – Hazem Alabiad Sep 14 '21 at 09:49
  • @HazemAlabiad if you were to ask Apollo for best practice I expect their answer would be to use Apollo Managed Federation and have the Apollo server load the schema from the registry at runtime. – Glen Thomas Sep 14 '21 at 09:52
  • How about if I want to print or output the federated single, federated, composed schema into a single file? – Hazem Alabiad Sep 17 '21 at 11:06