0
votes

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

3 Answers

0
votes

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

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)
  }]
})
0
votes

I could fix the issue, here is the code:

import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core/dist/plugin/landingPage/graphqlPlayground';
import { loadSchema } from '@graphql-tools/load';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { ApolloServer, gql } from 'apollo-server';
...

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

const server = new ApolloServer({
  schema: buildFederatedSchema([
    {
      typeDefs: gql(printSubgraphSchema(typeDefs)),
      resolvers,
    },
  ]),
  context: ({ req }: any) => ({
    organizationId: req.headers['organization'],
    websiteId: req.headers['website'],
  }),
  plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
});
    
...