Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processImport seems unnecessarily slow due to graphql.print #5616

Open
tlivings opened this issue Oct 4, 2023 · 1 comment
Open

processImport seems unnecessarily slow due to graphql.print #5616

tlivings opened this issue Oct 4, 2023 · 1 comment

Comments

@tlivings
Copy link
Contributor

tlivings commented Oct 4, 2023

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.

@ardatan
Copy link
Owner

ardatan commented Oct 13, 2023

PRs are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants