GraphQL Nexus Schema (nexusjs) 不使用标量类型进行编译

Kre*_*jko 5 scalar graphql

我正在尝试按照Nexus-Schema (nexusjs)网站上的文档向我的 GraphQL 应用程序添加标量类型。

\n

我尝试src/types/Types.ts使用文档和交互式示例中提供的示例将许多不同的实现添加到我的文件中。我的尝试包括:

\n

没有第三方库:

\n
const DateScalar = scalarType({\n  name: \'Date\',\n  asNexusMethod: \'date\',\n  description: \'Date custom scalar type\',\n  parseValue(value) {\n    return new Date(value)\n  },\n  serialize(value) {\n    return value.getTime()\n  },\n  parseLiteral(ast) {\n    if (ast.kind === Kind.INT) {\n      return new Date(ast.value)\n    }\n    return null\n  },\n})\n
Run Code Online (Sandbox Code Playgroud)\n

使用graphql-iso-date第 3 方库:

\n
import { GraphQLDate } from \'graphql-iso-date\'\nexport const DateTime = GraphQLDate\n
Run Code Online (Sandbox Code Playgroud)\n

使用第 3 方库(如Ghost 示例graphql-scalars所示):

\n
export const GQLDate = decorateType(GraphQLDate, {\n  rootTyping: \'Date\',\n  asNexusMethod: \'date\',\n})\n
Run Code Online (Sandbox Code Playgroud)\n

我在对象定义中使用这个新的标量类型,如下所示:

\n
const SomeObject = objectType({\n  name: \'SomeObject\',\n  definition(t) {\n    t.date(\'createdAt\') // t.date() is supposed to be available because of `asNexusMethod`\n  },\n})\n
Run Code Online (Sandbox Code Playgroud)\n

在所有情况下,这些类型都会从类型文件中导出并导入到makeSchema\ 的types属性中。

\n
import * as types from \'./types/Types\'\n\nconsole.log("Found types", types)\n\nexport const apollo = new ApolloServer({\n    schema: makeSchema({\n        types,\n        ...\n    context:()=>(\n        ...\n    })\n})\n\n
Run Code Online (Sandbox Code Playgroud)\n

上面的语句console.log确实表明const类型文件中声明的 s 在范围内:

\n
Found types { \n  GQLDate: Date,\n  ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n

如果我在开发模式下运行应用程序,一切都会启动并运行良好。

\n
ts-node-dev --transpile-only ./src/app.ts\n
Run Code Online (Sandbox Code Playgroud)\n

但是,每当我尝试编译应用程序以部署到服务器时,我都会遇到错误

\n
ts-node ./src/app.ts && tsc\n
Run Code Online (Sandbox Code Playgroud)\n

注意:此错误发生在运行ts-node ./src/app.ts之前tsc

\n

构建过程中显示的错误如下:

\n
/Users/user/checkouts/project/node_modules/ts-node/src/index.ts:500\n    return new TSError(diagnosticText, diagnosticCodes)\n           ^\nTSError: \xe2\xa8\xaf Unable to compile TypeScript:\nsrc/types/SomeObject.ts:11:7 - error TS2339: Property \'date\' does not exist on type \'ObjectDefinitionBlock<"SomeObject">\'.\n\n11     t.date(\'createdAt\')\n
Run Code Online (Sandbox Code Playgroud)\n

有谁对以下任一方面有任何想法:

\n
    \n
  • a) 我该如何解决这个错误?虽然长期解决方案是理想的,但临时解决方案也将受到赞赏。
  • \n
  • b) 我可以遵循什么步骤来调试此错误?或者关于如何获取额外信息来协助调试的想法?
  • \n
\n

任何帮助都将受到非常欢迎。谢谢!

\n

Kre*_*jko 2

--transpile-only当标记添加到 nexus:reflect 命令时,问题似乎得到了解决。

这意味着反射命令更新为:

ts-node --transpile-only ./src/app.ts
Run Code Online (Sandbox Code Playgroud)

并且构建命令更新为:

env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc 
Run Code Online (Sandbox Code Playgroud)

还创建了一个 github 问题,可以在此处查看:https ://github.com/graphql-nexus/schema/issues/690