graphql-tools
graphql-tools copied to clipboard
processImport seems unnecessarily slow due to graphql.print
processImport
: https://github.com/ardatan/graphql-tools/blob/master/packages/import/src/index.ts#L69
This function:
- parses a document from a file,
- iterates through each definition
- prints the definition to a string
- appends the string definition to a string of all definitions
- create a source from the string
- return the source
This results in unnecessarily bad performance as seen in this attached image:
However, this can simply be boiled down to:
- parse the document from a file
- append definitions to a new source
- return the source
Which results in a reduction of processing time:
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.
PRs are welcome!