dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.84k stars 1.33k forks source link

Generator does not work with federated schema #6099

Open talaltoukan opened 3 years ago

talaltoukan commented 3 years ago

Describe the bug The code generator throws an error when a federated schema is passed in.

To Reproduce Steps to reproduce the behavior: Hello, I am trying to break down my graphql schema into different files and modules. I am working with a federated schema. I have imported the different files and generated a GraphQLSchema object in an index.ts file like so:

import { gql } from "apollo-server";
import fs from "fs";
import path from "path";
import { resolvers } from "../server/resolvers";
import { buildFederatedSchema } from "@apollo/federation";

const { buildSchema } = require("graphql");

const mainSchema = gql(
  fs.readFileSync(path.join(__dirname, "./schema.graphql"), "utf-8")
);

const expiryDate = gql(
  fs.readFileSync(path.join(__dirname, "./expiryDate.graphql"), "utf-8")
);

export const schema = buildFederatedSchema({
  resolvers,
  typeDefs: [mainSchema, expiryDate],
});

To generate this schema for typescript, I use the code generator with the following codegen.yaml :

overwrite: true
schema:
  - "./src/typeDefs/index.ts"
documents: null
generates:
  ./generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
    config:
      contextType: "../src/interfaces/IContext#IContext"
      federation: true
      useIndexSignature: true

When I run the command to generate the code, I get the following error:

Failed to load schema from ./src/typeDefs/index.ts:

    Cannot read property 'concat' of undefined
    TypeError: Cannot read property 'concat' of undefined
at extendDefinition (/Users/talaltoukan/Developer/Ziina/ziina/node_modules/@graphql-toolkit/common/index.cjs.js:362:45)
at Object.printSchemaWithDirectives (/Users/talaltoukan/Developer/Ziina/ziina/node_modules/@graphql-toolkit/common/index.cjs.js:342:64)
at /Users/talaltoukan/Developer/Ziina/ziina/node_modules/@graphql-toolkit/core/index.cjs.js:629:46
at Array.map (<anonymous>)
dotansimha commented 3 years ago

Thank you for creating this @talaltoukan . Can you please provide a reproduction of the issue?

Also, we have an issue template that requires details on versions, please do not ignore these, since it seems like your are using an outdated version of codegen or it's dependencies (because of the @graphql-toolkit usage, which is no longer exists).