graphql-mesh icon indicating copy to clipboard operation
graphql-mesh copied to clipboard

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

Open xmlking opened this issue 1 year ago • 5 comments

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

xmlking avatar Apr 05 '23 00:04 xmlking

Could you create a reproduction repo? Thanks!

ardatan avatar Apr 05 '23 00:04 ardatan

Dockerfile

FROM node:19-alpine as runtime

# install pnpm
RUN corepack enable; corepack prepare [email protected] --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"]
}

xmlking avatar Apr 05 '23 00:04 xmlking

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

ardatan avatar Apr 05 '23 00:04 ardatan

@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

xmlking avatar Apr 05 '23 16:04 xmlking

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.

saimon-moore avatar Jun 29 '23 07:06 saimon-moore