0no-co / gql.tada

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

Query obj type filled with 'unknown' elements #345

Closed mr-zwets closed 2 months ago

mr-zwets commented 2 months ago

Describe the bug

Hello, I hope I am correct i am correct in filing this as a bug report.

First, the setup experience with gql-tada has been really smooth and I really like what I already got working 💯

Problem

The problem (bug) I'm running into is that all return elements from a query are being typed as unknown

output: {
        transaction_hash: unknown;
        output_index: unknown;
        nonfungible_token_commitment: unknown;
        transaction: {
            outputs: {
                nonfungible_token_commitment: unknown;
                nonfungible_token_capability: unknown;
                fungible_token_amount: unknown;
            }[];
            inputs: {
                ...;
            }[];
        };
    }[];

Because they return as type unknown I would have to typecast all the object elements when accessing the properties.

This seems to be because the auto generated schema graphql-env.d.ts includes unknown for the basic types

    'Boolean': unknown;
    'String': unknown;
    '_text': unknown;
    'bigint': unknown;
    'bytea': unknown;

Details

I am using a GraphQl backend called "chaingraph" with the following URL: https://demo.chaingraph.cash/v1/graphql

This Url was also used to auto-generate the schema.

As you might see from the query, the project is a blockchain indexer.

Chaingraph is a multi-node blockchain indexer and GraphQL API.

Reproduction

I re-created a minimal repo for my issue called 'chaingraph-tada' https://github.com/mr-zwets/chaingraph-tada

Thanks

Thanks in advance 😄

Reproduction

https://github.com/mr-zwets/chaingraph-tada

gql.tada version

"gql.tada": "^1.8.2"

Validations

JoviDeCroock commented 2 months ago

Custom scalars are expected to be unknown. You have to tell us how they are typed check the init function here https://gql-tada.0no.co/reference/gql-tada-api

mr-zwets commented 2 months ago

Ah thanks, that helps!

so I need to use initGraphQLTada() with an AbstractSetupSchema ?

It seems setupSchema also accepts a scalars config object.

I have no experience using scalars, so I'm not exactly sure what the keys would be... I assume need to define the 8 unknown types and that I can just put string as type (and adjust later if needed)?

Excuse me for wrongly filing a bug report and thank you for your time

mr-zwets commented 2 months ago

Okay I think I got it! 😃 The types for the 'customs scalars' are now those provided by myself!

for reference, I created a schema.ts file

import type { introspection } from './generated/graphql-env.d.ts';

declare module 'gql.tada' {
  interface setupSchema {
    introspection: introspection;
    scalars: {
      Boolean: string
      String: string
      _text: string
      bigint: string
      bytea: string
    };
  }
}

Thanks again for pointing me in the right direction! Will close the issue 👍

JoviDeCroock commented 2 months ago

You don't need to polyfill String/Boolean as those are defaults