ardatan / ardatan/graphql-tools

(@graphql-tools/load-files) - improve error message on import to require fallback

Open
#3,785 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
5.4k
Forks
830
Avg merge
10h 59m
Merged PRs (30d)
45

Description

**Problem description.**
Hello!
I'm rewriting my app from common js to es6 modules. So I have to move from loadFileSync to async loadFile to import my schemas and resolvers. The problem I've encountered is that if you have any troubles with your imports within resolvers, you get the error, on this [line](https://github.com/ardatan/graphql-tools/blob/ed367305beba668fb44b0a3baa13a946a522eb3b/packages/load-files/src/index.ts#L228):
```javascript
const defaultRequireMethod = (path) => import(path).catch(async () => require(path));
^
ReferenceError: require is not defined
at file:///home/jackson/work/projects/fasty/node_modules/@graphql-tools/load-files/index.mjs:155:75
at async file:///home/jackson/work/projects/fasty/node_modules/@graphql-tools/load-files/index.mjs:162:33
at async Promise.all (index 0)
at async default (file:///home/jackson/work/projects/fasty/server/src/server/graphql/index.js:17:36)
at async default (file:///home/jackson/work/projects/fasty/server/src/server/index.js:7:35)
at async file:///home/jackson/work/projects/fasty/server/bin/index.js:10:15

```
Import fails due to errors in code and fallback to require. Obviously require is not defined cause I'm using es6 modules. I've spent some time figuring out what's wrong, cause error really doesn't help much to locate a problem.

**The solution that helped me:**
Simply added console.log of error thrown by import fail and got all the problems causing that:
```javascript
const defaultRequireMethod = (path: string) => import(path).catch(async (err) => {
console.log(err)
return require(path)
});
```

**Request:**
Make import fail errors more clear and informable.

**Question:**
Why fallback to ```require``` anyway, as using import with es6 modules will always lead to ```undefined require``` error? I'm a beginner programmer so the question is really to learn and understand. Thanks for your reply in advance.

Contributor guide

Open the contributing guide

Research direction

Start at packages/load-files/src/index.ts around line 228, where the async import fallback is defined. Reproduce an import failure and inspect how the caught error is replaced by the fallback error; done means the original import failure is exposed clearly instead of only reporting that require is unavailable.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.