使用联合模式创建 Apollo-Server

Create Apollo-Server with Federated schema

我正在尝试使用从 GraphQL 文件加载的 TypeDef 联合架构创建一个 Apollo-Server。但是,我收到此错误

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

我认为我正在使用错误的函数从文件加载架构 loadSchema。 我如何从具有正确类型的文件中加载模式以将其作为联合模式的 TypeDef 提供?

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

您不需要使用gql功能。架构已被解析。

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

我还从文件中加载子图架构,并按如下方式执行:

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