graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

processImport seems unnecessarily slow due to graphql.print

Open tlivings opened this issue 9 months ago • 1 comments

processImport : https://github.com/ardatan/graphql-tools/blob/master/packages/import/src/index.ts#L69

This function:

  1. parses a document from a file,
  2. iterates through each definition
  3. prints the definition to a string
  4. appends the string definition to a string of all definitions
  5. create a source from the string
  6. return the source

This results in unnecessarily bad performance as seen in this attached image:

Clinic_Flame

However, this can simply be boiled down to:

  1. parse the document from a file
  2. append definitions to a new source
  3. return the source

Which results in a reduction of processing time:

Clinic_Flame

Code illustration:

export function processImport(
    filePath: string,
    cwd = cwdFactory(),
    predefinedImports: Record<string, string> = {},
    visitedFiles: VisitedFilesMap = new Map(),
  ): DocumentNode {
    const set = visitFile(filePath, join(cwd + '/root.graphql'), visitedFiles, predefinedImports);

    const document = {
        kind: Kind.DOCUMENT,
        definitions: [],
    };

    for (const defs of set.values()) {
        for (const def of defs) {
            document.definitions.push(def);
        }
    }

    return document;
}

As far as I can tell this is functionally the same without adding the overhead of printing and then re-parsing every definition.

tlivings avatar Oct 04 '23 13:10 tlivings

PRs are welcome!

ardatan avatar Oct 13 '23 11:10 ardatan