graphql-code-generator: Cannot read property 'forEach' of undefined when running the "programmatic usage" sample

Describe the bug

Cannot read property 'forEach' of undefined when running the “programmatic usage” sample. Seems to be caused by documents being required by the codegen function.

To Reproduce Steps to reproduce the behavior:

Code is more or less copy pasted from https://graphql-code-generator.com/docs/getting-started/programmatic-usage

import fs from "fs";
import path from "path";

import { codegen } from "@graphql-codegen/core";
import { plugin as typescriptPlugin } from "@graphql-codegen/typescript";

import { buildSchema, printSchema, parse } from "graphql";

const schemaText = `

type DeletionResultInfo {
  success: Boolean
  Meta: MutationResultInfo
}

type MutationResultInfo {
  transaction: Boolean
  elapsedTime: Int
}

`;

async function createTypeScriptTypes(outputFile) {
  const schema = buildSchema(schemaText);

  const config = {
    // used by a plugin internally, although the 'typescript' plugin currently
    // returns the string output, rather than writing to a file
    filename: outputFile,
    schema: parse(printSchema(schema)),
    plugins: {
      typescript: {} // Here you can pass configuration to the plugin
    },
    pluginMap: {
      typescript: {
        plugin: typescriptPlugin
      }
    }
  };

  const output = await codegen(config);
  fs.writeFile(path.join(__dirname, outputFile), output, () => {
    console.log("Outputs generated!");
  });
}

createTypeScriptTypes("test.ts");

Running this via the esm package produces the error:

(node:51651) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘forEach’ of undefined at validateDocuments (/Users/adam.rackis/Documents/git/mongo-graphql-starter/node_modules/@graphql-codegen/core/dist/commonjs/codegen.js:83:11) at codegen (/Users/adam.rackis/Documents/git/mongo-graphql-starter/node_modules/@graphql-codegen/core/dist/commonjs/codegen.js:10:5) at createTypeScriptTypes (/Users/adam.rackis/Documents/git/mongo-graphql-starter/src/codeGen/createTypeScriptTypes.js:41:24) at Object.<anonymous> (/Users/adam.rackis/Documents/git/mongo-graphql-starter/src/codeGen/createTypeScriptTypes.js:47:1) at Generator.next (<anonymous>)

Looking in the code, it seems this is a result of validateDocuments being called and passed options.documents as the second argument, which has forEach called on it directly (which is undefined in the code above).

Expected behavior

Should produce ts typings.

Environment:

  • OS: MacOS High Sierra
  • @graphql-codegen/...: 1.2.1
  • @graphql-codegen/typescript: 1.2.1
  • NodeJS: 10.15.0

Additional context

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Fixed in 1.4.0 🎉