0no-co / gql.tada

🪄 Magical GraphQL query engine for TypeScript
https://gql-tada.0no.co
MIT License
2.6k stars 43 forks source link

RFC: Helper to get any type in schema #390

Closed oceandrama closed 2 months ago

oceandrama commented 2 months ago

Summary

Now there are helpers to get result of query or mutation and its variables, but it's very hard to get type of concrete enum, input type etc. For example, if I want to get UserRole enum I need to do something like this

enum UserRole {
  USER
  ADMIN
}

input UserFilterInput {
  role: UserRole
}

type Query {
  users(filter: UserFilterInput ): [User!]!
}
import { graphql, type VariablesOf } from "gql.tada"

const query = graphql(`
  query users($filter: UserFilterInputType) {
      users(filter: $filter) {
          id
      }
  }`
)

type UserRole = NonNullable<NonNullable<VariablesOf<typeof query>["filter"]>["role"]>

Proposed Solution

It will be very usefull to have helper like

import { initGraphQLTada, TypeOf } from 'gql.tada'

const graphql = initGraphQLTada(...)

type UserRole = TypeOf<"UserRole">
type UserFilterInput = TypeOf<"UserFilterInput">
type User = TypeOf<"User">
kitten commented 2 months ago

See: https://gql-tada.0no.co/reference/gql-tada-api#graphql-scalar This works on any input type

As per TypeOf<"User">, you can't retrieve a concrete type for an object type. See: https://gql-tada.0no.co/reference/gql-tada-api#graphql-scalar