graphql-nexus / nexus

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

Mutation args and return types are of type any #892

Closed otrebu closed 3 years ago

otrebu commented 3 years ago

I don't think I am doing anything particularly wrong, but I don't have types for mutation args and return type in the resolve function.

See example below ( what is actually hovered is not the actual issue, the args within the resolve function are of type any, the root type is any and also the return type )

image

The nexus-typegen.ts file is successfully generated.

I have generated all my code from the sdl converter, now I am starting again to scratch to see if I can work out what is that breaks the typescript type reflection.

otrebu commented 3 years ago

I have been carefully looking at the issue, I think I must have been to quick in building up my schema and missed some imports.

I was referencing the input types directly in my mutations, which it is not enough on its own.

100AD commented 3 years ago

So this took me a long time to try to solve, but this got my type safety for arguments.

I had custom scalars

import { GraphQLDateTime } from 'graphql-scalars';
import { GraphQLUpload } from 'graphql-upload';

export const DateTime = GraphQLDateTime;
export const Upload = GraphQLUpload;

Which were defined as any in the generated typegen file

To solve this in your makeSchema declaration this code worked for me

const schema = makeSchema({
  // other stuff
  sourceTypes: {
    modules: [
      { module: '@types/graphql-upload/index.d.ts', alias: 'upload' }, // the location in node_modules where '@types/graphql-upload is defined
    ],
    mapping: {
      DateTime: 'Date', // I think Date as a type already exists in node so this worked
      Upload: 'upload.FileUpload', // FileUpload is the interface in @types/graphql-upload
    },
  },
});

This achieved type safety for me

otrebu commented 3 years ago

Oh yes @100AD ! I noticed that too, without the mapping you won't have type safety on those fields.

I am going to close this issue soon, I am pretty sure that what was going wrong was my own fault. Mainly to do with having missed to export/import my input types properly.

@tgriesser could be interesting maybe to be able to not have to import input types ( or object types that are returned in Mutations ) if they are used in a Mutation directly. Just a thought.

Sytten commented 3 years ago

Usually you just need to open the generated file for the typing to be picked-up, not much we can do

saanderson1987 commented 2 years ago

I'm getting a similar error:

Screen Shot 2022-09-05 at 8 52 35 PM

Screen Shot 2022-09-05 at 8 53 10 PM

killerdroid99 commented 7 months ago

Can this be resolved with zod to ensure type safety??