stepci / garph

Fullstack GraphQL Framework for TypeScript
https://garph.dev
MIT License
1.31k stars 17 forks source link

InferResolversStrict not checking resolver params correctly #58

Closed mlafeldt closed 1 year ago

mlafeldt commented 1 year ago

Hey, here's something I'd like to understand:

Given the following code, InferResolversStrict will happily accept getUsername as a resolver, although its arguments are wrong. I think this is related to the fact that the first arg is supposed to be parent: any, which checks against id: string. However, the number of args isn't enforced either (it should be 4 instead of 1).

(I don't think it's a problem with Deno's TS compiler options as those are pretty strict by default.)

Please enlighten me. 😄

import { graphql } from "npm:graphql"
import { buildSchema, g, InferResolversStrict } from "npm:garph"

function getUsername(id: string): string {
  return "User#" + id
}

const queryType = g.type("Query", {
  username: g.string().args({
    id: g.id(),
  }),
})

const resolvers: InferResolversStrict<
  { Query: typeof queryType },
  {}
> = {
  Query: {
    username: getUsername, // wrong
    // username: (_, args, __, ___) => getUsername(args.id), // correct
  },
}

const query = Deno.args[0]
const schema = buildSchema({ g, resolvers })
const result = await graphql({
  schema,
  source: query,
  rootValue: resolvers,
})

console.log(JSON.stringify(result, null, 2))
mishushakov commented 1 year ago

Good finding, Mathias! Will investigate soon

mishushakov commented 1 year ago

The number of arguments can be 0-4 and the return type has to be correct, otherwise you will be getting an error.

I couldn't reproduce. Do you still experience the error? Have you tried starting a new project w/o Deno?

Screenshot 2023-06-12 at 19 56 21 Screenshot 2023-06-12 at 19 56 27
mishushakov commented 1 year ago

You could also just manually cast the types

Screenshot 2023-06-12 at 20 01 54
mishushakov commented 1 year ago

Will be closing as it's not reproducible. Feel free to reopen, if you could give me a reproducible example