ardatan / graphql-import-node

Import 'graphql' files in NodeJS
80 stars 14 forks source link

.graphql files not available after tsc build #11

Closed yalamber closed 4 years ago

yalamber commented 4 years ago

I am using this module and it seems to be working for development well. I have following script setup

"scripts": {
    "dev": "nodemon --exec ts-node src/main.ts",
    "build": "tsc",
    "start": "node dist/main.js"
  },

My main.ts file

import 'graphql-import-node'
import 'reflect-metadata'
import { appModule } from './modules/app';
import { ApolloServer } from 'apollo-server';

const { schema, context } = appModule;

const server = new ApolloServer({ 
  schema,
  context,
  introspection: true,
});

server.listen().then(server => {
  console.log(`Server listening on ${server.url}`)
});

tsconfig.json file

{
  "compilerOptions": {
    "baseUrl": ".",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "target": "es6",
    "lib": ["es6", "esnext", "es2015"],
    "noImplicitAny": false,
    "suppressImplicitAnyIndexErrors": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  },
  "files": ["src/main.ts"],
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

Now this works when I do npm run dev just fine. npm run build also builds in dist folder fine. But when I run npm start, It shows missing .graphql error. I see .graphql files are not being copied.

ardatan commented 4 years ago

Hi @yalamber ! When you run compiled JavaScript files from another directory, it looks for graphql files in that directory. Since you still have graphql files in your directory and don't have those in output directory, it fails. graphql-import-node is just a NodeJS extension that helps NodeJS to handle .graphql files in its builtin require command. So you are responsible to copy them because tsc will only create js files for you.

yalamber commented 4 years ago

Hello @ardatan thank you for quick reply, So I am using graphql modules https://graphql-modules.com/ and have .graphql files in different folders, So If I run post build script which copies those .graphql files to respective folders it should work fine right?

ardatan commented 4 years ago

@yalamber Yes. That should do the trick. Let me know if it doesn't :)

yalamber commented 4 years ago

Thanks @ardatan I added following scripts

"build": "tsc && npm run copy-graphql",
"copy-graphql": "copyfiles -u 1 src/modules/**/*.graphql dist",

this copied .graphql files to required directories in dist and works fine for now :)

ardatan commented 4 years ago

Sounds awesome! Feel free to open a new issue if you have more questions :)