stepci/garph

InferResolversStrict not checking resolver params correctly

Closed this issue · 4 comments

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

Good finding, Mathias! Will investigate soon

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

You could also just manually cast the types

Screenshot 2023-06-12 at 20 01 54

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