Search code examples
node.jstypescriptgraphqltsc

Can't run builded graphql server by tsc


i am using typescript + graphql + node.js server. it is not problem that run in ts-node. but in production, i want to build to js by tsc. also, I am using path alias.

First, i got a error about path in dist because alias path. So, i try bundle by babel for dist files.

package.json

"scripts": { ..
  "build": "tsc && babel dist -d dist",

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "inlineSourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "baseUrl": ".",
    "paths": {
      "src/*" : ["src/*"]
    },
    "outDir": "dist",
    "plugins": [
      {
        "transform": "typescript-transform-paths"
      }
    ]
  }
}

.babelrc

{
  "compact": false,
  "retainLines": true,
  "minified": false,
  "inputSourceMap": false,
  "sourceMaps": false,
  "plugins": [
    [
      "module-resolver",
      {
        "root": ["./dist"],
        "alias": {
          "src": "./dist"
        }
      }
    ]
  ]
}

Then, i got dist file. but, when i try

node dist/App.js

i got a error message like

/Users/dany/workspace/paperly/projects/strum-server/node_modules/graphql/validation/validate.js:108
    throw new Error(errors.map(function (error) {
    ^

Error: Unknown type "Query".
    at assertValidSDL (/Users/dany/workspace/paperly/projects/strum-server/node_modules/graphql/validation/validate.js:108:11)
    at Object.buildASTSchema (/Users/dany/workspace/paperly/projects/strum-server/node_modules/graphql/utilities/buildASTSchema.js:71:34)
    at Object.buildSchemaFromTypeDefinitions (/Users/dany/workspace/paperly/projects/strum-server/node_modules/graphql-tools/dist/generate/buildSchemaFromTypeDefinitions.js:23:28)
    at Object.makeExecutableSchema (/Users/dany/workspace/paperly/projects/strum-server/node_modules/graphql-tools/dist/makeExecutableSchema.js:26:29)
    at Object.<anonymous> (/Users/dany/workspace/paperly/projects/strum-server/dist/graphql/schema.js:11:27)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Module.require (internal/modules/cjs/loader.js:1080:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/dany/workspace/paperly/projects/strum-server/dist/App.js:28:34)

i guess that it is the problem about compiling graphql to js.

What is the problem?


Solution

  • To make sure file inside folder typeDefs is exported to folder dist

    import path from 'path'
    import { fileLoader, mergeTypes } from 'merge-graphql-schemas'
    
    const typesArray = fileLoader(path.join(__dirname, './**'))
    export default mergeTypes(typesArray, { all: true })