Search code examples
graphqlscalar

GraphQL Nexus Schema (nexusjs) doesn't compile with scalar types


I am trying to follow the documentation on the Nexus-Schema (nexusjs) website for adding scalar types to my GraphQL application.

I have tried adding many of the different implementations to my src/types/Types.ts file using the samples provided in the documentation and the interactive examples. My attempts include:

Without a 3rd party libraries:

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
  },
})

With graphql-iso-date 3rd party library:

import { GraphQLDate } from 'graphql-iso-date'
export const DateTime = GraphQLDate

With graphql-scalars 3rd party library (as shown in the ghost example):

export const GQLDate = decorateType(GraphQLDate, {
  rootTyping: 'Date',
  asNexusMethod: 'date',
})

I am using this new scalar type in an object definition like the following:

const SomeObject = objectType({
  name: 'SomeObject',
  definition(t) {
    t.date('createdAt') // t.date() is supposed to be available because of `asNexusMethod`
  },
})

In all cases, these types are exported from the types file and imported into the makeSchema's types property.

import * as types from './types/Types'

console.log("Found types", types)

export const apollo = new ApolloServer({
    schema: makeSchema({
        types,
        ...
    context:()=>(
        ...
    })
})

The console.log statement above does show that consts declared in the types file are in scope:

Found types { 
  GQLDate: Date,
  ...
}

If I run the app in development mode, everything boots up and runs fine.

ts-node-dev --transpile-only ./src/app.ts

However, I encounter errors whenever I try to compile the app to deploy to a server

ts-node ./src/app.ts && tsc

Note: This error occurs occurs running just ts-node ./src/app.ts before it gets to tsc

The errors that shown during the build process are the following:

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

Does anyone have any ideas on either:

  • a) How can I work around this error? While long-term solutions are ideal, temporary solutions would also be appreciated.
  • b) Any steps I could follow to debug this error? Or ideas on how get additional information to assist with debugging?

Any assistance would be very much welcomed. Thanks!


Solution

  • The issue seems to be resolved when --transpile-only flag is added to the nexus:reflect command.

    This means the reflection command gets updated to:

    ts-node --transpile-only ./src/app.ts
    

    and the build comand gets updated to:

    env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc 
    

    A github issue has also been created which can be reviewed here: https://github.com/graphql-nexus/schema/issues/690