我正试图按照Nexus-Schema (nexusjs)文档网站向我的GraphQL应用程序添加标量类型。
我尝试使用文档和交互示例中提供的示例将许多不同的实现添加到我的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-date第三方库:
import { GraphQLDate } from 'graphql-iso-date'
export const DateTime = GraphQLDate使用graphql-scalars第三方库(如鬼例所示):
export const GQLDate = decorateType(GraphQLDate, {
rootTyping: 'Date',
asNexusMethod: 'date',
})我在对象定义中使用这种新的标量类型,如下所示:
const SomeObject = objectType({
name: 'SomeObject',
definition(t) {
t.date('createdAt') // t.date() is supposed to be available because of `asNexusMethod`
},
})在所有情况下,这些类型都是从类型文件导出并导入到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的作用域:
Found types {
GQLDate: Date,
...
}如果我在开发模式下运行这个应用程序,一切都会启动并运行良好。
ts-node-dev --transpile-only ./src/app.ts但是,每当我试图编译要部署到服务器的应用程序时,都会遇到错误。
ts-node ./src/app.ts && tsc注意:此错误发生在运行ts-node ./src/app.ts之前,然后才到达tsc。
生成过程中显示的错误如下:
/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')有没有人对以下两种方法有任何想法:
任何援助都将受到非常欢迎。谢谢!
发布于 2020-12-01 18:39:02
当将--transpile-only标志添加到nexus: issue命令中时,该问题似乎得到了解决。
这意味着反射命令将被更新为:
ts-node --transpile-only ./src/app.ts然后将build更新为:
env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc 还创建了一个github问题,可以在这里查看:https://github.com/graphql-nexus/schema/issues/690
https://stackoverflow.com/questions/64932457
复制相似问题