无法运行 tsc 构建的 graphql 服务器



我使用的是typescript+graphql+node.js服务器。在ts节点中运行不是问题。但在生产中,我想通过tsc构建到js。此外,我正在使用路径别名。

首先,我在dist中得到了一个关于路径的错误,因为别名路径。因此,我尝试逐包dist文件。

软件包.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"
}
}
]
]
}

然后,我得到了dist文件。但是,当我尝试时

node dist/App.js

我收到一条类似的错误信息

/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)

我想这是把graphql编译成js的问题。

问题出在哪里?

要确保文件夹typeDefs中的文件导出到文件夹dist

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

最新更新