graphql-editor / graphql-zeus

GraphQL client and GraphQL code generator with GraphQL autocomplete library generation ⚡⚡⚡ for browser,nodejs and react native ( apollo compatible )
https://graphqleditor.com/docs/zeus
MIT License
1.95k stars 106 forks source link

Expression produces a union type that is too complex to represent #404

Closed lukepolo closed 3 weeks ago

lukepolo commented 3 months ago

I ran into similar issues as

https://github.com/graphql-editor/graphql-zeus/issues/189 https://github.com/graphql-editor/graphql-zeus/issues/229

import { Chain, ValueTypes, ZeusScalars } from "../../generated/zeus";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { HasuraConfig } from "../configs/types/HasuraConfig";

const scalars = ZeusScalars({
  uuid: {
    decode: (value: string) => {
      return value;
    },
  },
  bigint: {
    encode: (value: string) => {
      return value.toString();
    },
    decode: (value: string) => {
      return value;
    },
  },
});

@Injectable()
export class HasuraService {
  private config: HasuraConfig;

  constructor(readonly configService: ConfigService) {
    this.config = configService.get<HasuraConfig>("hasura");
  }

  public async query<Z extends ValueTypes["query_root"]>(
    gql: Z | ValueTypes["query_root"],
  ) {
    try {
      return await this.getClient()("query", {
        scalars,
      })(gql);
    } catch (error) {
      if (error?.response) {
        throw error?.response.errors.at(0).message;
      }
      throw error;
    }
  }

  public async mutation<Z extends ValueTypes["mutation_root"]>(
    gql: Z | ValueTypes["mutation_root"],
    variables?: Record<string, unknown>,
  ) {
    try {
      return await this.getClient()("mutation", {
        scalars,
      })(gql, { variables });
    } catch (error) {
      if (error?.response) {
        throw error?.response.errors.at(0).message;
      }
      throw error;
    }
  }

  private getClient() {
    return Chain(`${this.config.endpoint}/v1/graphql`, {
      headers: {
        "Content-Type": "application/json",
        "x-hasura-admin-secret": this.config.secret,
      },
    });
  }
}
src/hasura/hasura.service.ts:50:7 - error TS2590: Expression produces a union type that is too complex to represent.

50       return await this.getClient()("mutation", {

         ~~~~~~

[3:03:31 AM] Found 2 errors. Watching for file changes.

Ive attached my shcema if that is helpful!

zeus.zip

aexol commented 3 weeks ago

You need to read typescript compiler code to understand what happens here. Because I hacked the way the types are inferred you can't wrap it in functions your way - I am sorry. What you need to do is try to do the same with Thunder function, but again every other function you wrap the function into it is like multiplying the number of types