Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation args and return types are of type any #892

Closed
otrebu opened this issue Apr 20, 2021 · 6 comments
Closed

Mutation args and return types are of type any #892

otrebu opened this issue Apr 20, 2021 · 6 comments
Labels
scope/types Having to do with types type/question

Comments

@otrebu
Copy link

otrebu commented Apr 20, 2021

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
Copy link
Author

otrebu commented Apr 23, 2021

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
Copy link

100AD commented Apr 24, 2021

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
Copy link
Author

otrebu commented Apr 26, 2021

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 Sytten added scope/types Having to do with types type/question labels Jul 26, 2021
@Sytten
Copy link
Collaborator

Sytten commented Jul 26, 2021

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

@Sytten Sytten closed this as completed Jul 26, 2021
@saanderson1987
Copy link

saanderson1987 commented Sep 6, 2022

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope/types Having to do with types type/question
Projects
None yet
Development

No branches or pull requests

5 participants