ardatan / ardatan/graphql-tools
processImport seems unnecessarily slow due to graphql.print
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 830
- Avg merge
- 10h 59m
- Merged PRs (30d)
- 45
Description
`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:
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:
Code illustration:
```
export function processImport(
filePath: string,
cwd = cwdFactory(),
predefinedImports: Record = {},
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.
Contributor guide
Research direction
Start in packages/import/src/index.ts at processImport, then inspect how visitFile returns definitions and how the current implementation prints and reparses them. Done means preserving the returned DocumentNode behavior while avoiding the unnecessary print-and-parse work described in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100