ardatan / graphql-mesh

🕸️ GraphQL Federation Framework for any API services such as REST, OpenAPI, Swagger, SOAP, gRPC and more...
https://the-guild.dev/graphql/mesh
MIT License
3.29k stars 342 forks source link

the imports are not properly generated for `customFetch` which fail running in docker environment #5297

Open xmlking opened 1 year ago

xmlking commented 1 year ago

I am using customFetch and additionalEnvelopPlugins with scripts pointing in my src as shown below. everything works fine locally, but when I am doing docker build, I am getting import path error for customFetch

This works in local run i.e, npm run dev but when dockerized and run, it is failing with path issues for customFetch

image

Workaround: I have to use this during docker build to make it work. (obviously not going to work for local run)

image

Note: additionalEnvelopPlugins has no issues.

I think the root cause is how imports are generated in .mesh/index.ts

image image
ardatan commented 1 year ago

Could you create a reproduction repo? Thanks!

xmlking commented 1 year ago

Dockerfile

FROM node:19-alpine as runtime

# install pnpm
RUN corepack enable; corepack prepare pnpm@8.1.0 --activate

WORKDIR /app

# clean install dependencies, no devDependencies, no prepare script
COPY .npmrc package.json pnpm-lock.yaml .meshrc.yml ./
RUN pnpm fetch --prod --unsafe-perm --ignore-scripts --unsafe-perm
RUN pnpm install -r --offline --prod

COPY . .

RUN pnpm build
RUN pnpm prune --prod --no-optional
############################################################
### stage_final
############################################################
FROM gcr.io/distroless/nodejs:18 as final

USER nonroot
ENV NODE_ENV production
WORKDIR /app

ENTRYPOINT ["/nodejs/bin/node"]
COPY --from=runtime --chown=nonroot:nonroot /app/.mesh ./.mesh
COPY --from=runtime --chown=nonroot:nonroot /app/config ./config
COPY --from=runtime --chown=nonroot:nonroot /app/src ./src
COPY --from=runtime --chown=nonroot:nonroot /app/tsconfig.json ./tsconfig.json
COPY --from=runtime --chown=nonroot:nonroot /app/package.json ./package.json
COPY --from=runtime --chown=nonroot:nonroot /app/node_modules ./node_modules

ENV PORT 4000
EXPOSE 4000

CMD  ["node_modules/@graphql-mesh/cli/cjs/bin.js", "start"]

tsconfig.json

{
    "compilerOptions": {
        "outDir": "dist",
        "skipLibCheck": true,
        "target": "esnext",
        "moduleResolution": "node",
        "module": "commonjs",
        "lib": ["esnext"],
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true
    },
    "include": ["src"],
    "exclude": ["node_modules"]
}
ardatan commented 1 year ago

Could you create a repo so we can run and reproduce?

xmlking commented 1 year ago

@ardatan sorry for the delay. here is the repo to reproduce this issue

To reproduce error, run:

git clone https://github.com/xmlking/graphql-mesh-specific 
cd graphql-mesh-specific 
pnpm i
pnpm build
pnpm start

Error

💥 🕸️  Mesh Error: Cannot find module './src/middleware/custom-fetch.ts'
Require stack:
- /graphql-mesh-specific/.mesh/index.ts

This should work without error

pnpm dev
saimon-moore commented 1 year ago

I debugged this a bit and the issue is basically here:

https://github.com/Urigo/graphql-mesh/blob/30ecdbde782acf9b33e35805bc0f7deacbf4b038/packages/config/src/utils.ts#L103

which then calls:

https://github.com/Urigo/graphql-mesh/blob/30ecdbde782acf9b33e35805bc0f7deacbf4b038/packages/config/src/utils.ts#L66

A very hacky fix:

const relativeModuleName = cross_helpers_1.path.isAbsolute(moduleName) ? (type === 'fetch' ? `../${name}` : moduleName ) : moduleName;

I don't have time to dedicate to finding all the ways this (or a better fix) could break things (i.e. a proper solution).

For now, I'll likely sed my way out of this but maybe this helps someone more knowleadgeable to fix this quicker.

Btw this doesn't only affect docker environments. This happens calling yarn run mesh build.