graphql-nexus / nexus

Code-First, Type-Safe, GraphQL Schema Construction
https://nexusjs.org
MIT License
3.4k stars 275 forks source link

Try to support running with ts-node #513

Open dantman opened 4 years ago

dantman commented 4 years ago

ts-node does not auto-import types from include because there's a notable performance penalty to doing so. As a result when running with ts-node the types file generated by Nexus' typegen option are not available, and thus any custom scalar method or plugin method will result in a type error. And ts-node still seems to be the de-facto way to run TypeScript in development.

ts-node does offer a way to support generated TypeScript definitions. By adding a folder to typeRoots. However for that to work the generated typescript definitions need to be structured as type packages not loose type definition files.

It would be really nice if using Nexus in development would work smoothly with ts-node without needing to use --files, manually include the generated types with /// <reference in every single schema definition file you write, or setup a complex alternative to ts-node.

I think this might be possible if Nexus generated its type definition files in a type package structure.

I'm not entirely certain how Nexus' generated types work. But I think it's possible that the declare global may not work with the type package structure.

One theoretical alternative I could think of would be to fake something like a @nexus/schema/generated module. The real @nexus/schema/generated module file would not export any types, or alternatively it would just re-export the current interfaces that are global declared interfaces so that @nexus/schema can import them from @nexus/schema/generated instead of relying on them being global. Then if a project opts-in to typegen exporting a type package instead of a loose type definition file Nexus can generate a {typegenRoot}/@nexus/schema/generated.d.ts and instead of declare global Nexus can export things inside of declare module "@nexus/schema/generated" blocks and this will theoretically override the @nexus/schema/generated with the generated types that Nexus needs.

Weakky commented 4 years ago

Hey @dantman,

Another workaround we've been using is to generate the typegen file in the node_modules/@types folder to trick it into thinking that it's a genuine module typing. I remember @tgriesser pointing out other issues with that approach, but that should work well for most use-cases.

eg: node_modules/@types/nexus-typegen/index.d.ts. Generating that file as an index.d.ts is critical for ts-node to pick it up if I remember correctly.

jasonkuhrt commented 4 years ago

Hi @dantman I was unable to reproduce. Please provide a repro repo. Users should be putting their typegen into the node_modules/@types/<whatever>/index.d.ts in most cases.

Sytten commented 4 years ago

I personally use ts-node-dev in transpile only mode. It works ok ish, sometimes it has race conditions. As for generating in node_modules, I am against it because IDE dont generally reload the TS server on changes in that folder. This is a problem for dev.

jasonkuhrt commented 4 years ago

IDE dont generally reload the TS server on changes in that folder.

This is not true for VSCode at least. Please share any references or repros you have because we might change the defaults before 1.0. It was very smooth for framework users and this was the strategy we used.

Sytten commented 4 years ago

Ok weird, maybe it is my setup then. But for example with prisma generation, I need to go into the file for the typegen to trigger, It also happens with the typegen of nexus sometimes even not in the node_modules. I dont think that the file watcher is enabled for node_modules by default.

jasonkuhrt commented 4 years ago

@Weakky had a theory (we never verified) that node_modules/@types/* was special cased by and/or VSCode/TS Language Server such that changes under that directory were picked up and affected TS typings state consistently.

I've definitely seen the issues you're referring to though. I actually had a rough time the other week on a demo project and it was using node_modules/@types in that case. But that was an exception in my experience.

So many moving parts and changing versions of tools integrating 🙈.

alchemyjamie commented 2 years ago

Im experiencing a similar issue. I have my nexus types being outputted to node_modules/@types/nexus-typegen/index.d.ts, and have "typeRoots": ["./typings", "./node_modules/@types" in my tsconfig.json, but am not able to reference nexus types such as NexusGenFieldTypes