GraphQL-Nexus模式(nexusjs)不';不能使用标量类型编译
我正试图通过网站向我的GraphQL应用程序添加标量类型 我已经尝试使用文档中提供的示例和交互式示例将许多不同的实现添加到我的GraphQL-Nexus模式(nexusjs)不';不能使用标量类型编译,graphql,scalar,Graphql,Scalar,我正试图通过网站向我的GraphQL应用程序添加标量类型 我已经尝试使用文档中提供的示例和交互式示例将许多不同的实现添加到我的src/types/types.ts文件中。我的尝试包括: 如果没有第三方库: const DateScalar = scalarType({ name: 'Date', asNexusMethod: 'date', description: 'Date custom scalar type', parseValue(value) { return
src/types/types.ts
文件中。我的尝试包括:
如果没有第三方库:
const DateScalar = scalarType({
name: 'Date',
asNexusMethod: 'date',
description: 'Date custom scalar type',
parseValue(value) {
return new Date(value)
},
serialize(value) {
return value.getTime()
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(ast.value)
}
return null
},
})
使用graphql iso日期第三方库:
import { GraphQLDate } from 'graphql-iso-date'
export const DateTime = GraphQLDate
使用graphql标量
第三方库(如图所示):
我在对象定义中使用这种新的标量类型,如下所示:
const SomeObject = objectType({
name: 'SomeObject',
definition(t) {
t.date('createdAt') // t.date() is supposed to be available because of `asNexusMethod`
},
})
/Users/user/checkouts/project/node_modules/ts-node/src/index.ts:500
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/types/SomeObject.ts:11:7 - error TS2339: Property 'date' does not exist on type 'ObjectDefinitionBlock<"SomeObject">'.
11 t.date('createdAt')
在所有情况下,这些类型都从类型文件导出,并导入到makeSchema
的types
属性中
import * as types from './types/Types'
console.log("Found types", types)
export const apollo = new ApolloServer({
schema: makeSchema({
types,
...
context:()=>(
...
})
})
上面的console.log
语句确实表明类型文件中声明的const
s在范围内:
Found types {
GQLDate: Date,
...
}
如果我在开发模式下运行应用程序,一切都会启动并正常运行
ts-node-dev --transpile-only ./src/app.ts
但是,每当我试图编译应用程序以部署到服务器时,都会遇到错误
ts-node ./src/app.ts && tsc
注意:此错误发生在运行ts节点。/src/app.ts
到tsc
生成过程中显示的错误如下:
const SomeObject = objectType({
name: 'SomeObject',
definition(t) {
t.date('createdAt') // t.date() is supposed to be available because of `asNexusMethod`
},
})
/Users/user/checkouts/project/node_modules/ts-node/src/index.ts:500
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/types/SomeObject.ts:11:7 - error TS2339: Property 'date' does not exist on type 'ObjectDefinitionBlock<"SomeObject">'.
11 t.date('createdAt')
/Users/user/checkout/project/node\u modules/ts node/src/index.ts:500
返回新的错误(diagnosticText、diagnosticCodes)
^
t错误:⨯ 无法编译类型脚本:
src/types/SomeObject.ts:11:7-错误TS2339:类型“ObjectDefinitionBlock”上不存在属性“date”。
11 t.日期(“创建日期”)
是否有人对以下任一方面有任何想法:
- a) 我如何解决这个错误?虽然长期解决方案是理想的,但临时解决方案也将受到欢迎
- b) 我可以按照哪些步骤调试此错误?或者关于如何获取附加信息以帮助调试的想法
我们非常欢迎任何援助。谢谢 将--transfile only
标志添加到nexus:reflect命令时,问题似乎得到了解决
这意味着反射命令将更新为:
ts-node --transpile-only ./src/app.ts
env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc
生成命令将更新为:
ts-node --transpile-only ./src/app.ts
env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc
还创建了一个github问题,可在此处查看:当将--transfile only
标志添加到nexus:reflect命令时,该问题似乎已得到解决
这意味着反射命令将更新为:
ts-node --transpile-only ./src/app.ts
env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc
生成命令将更新为:
ts-node --transpile-only ./src/app.ts
env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc
还创建了一个github问题,可在此处查看: