Skip to content

Mutation property type error in generated react query file #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dadamu opened this issue Nov 11, 2022 · 9 comments · Fixed by #85
Closed

Mutation property type error in generated react query file #80

dadamu opened this issue Nov 11, 2022 · 9 comments · Fixed by #85

Comments

@dadamu
Copy link

dadamu commented Nov 11, 2022

The generated react query from cw721-base has the error in useCw721BaseMintMutation function from the Cw721BaseMintMutation with wrong type msg property.
The msg in Cw721BaseMintMutation is using generated msg whose properties are in snake-case, it mismatches the client mint function to use the object with camel-case naming:

Here is an example:

export interface Cw721BaseMintMutation {
  client: Cw721BaseClient;
  msg: MintMsgForNullable_Empty;
  args?: {
    fee?: number | StdFee | "auto";
    memo?: string;
    funds?: Coin[];
  };
}
export function useCw721BaseMintMutation(options?: Omit<UseMutationOptions<ExecuteResult, Error, Cw721BaseMintMutation>, "mutationFn">) {
  return useMutation<ExecuteResult, Error, Cw721BaseMintMutation>(({
    client,
    msg,
    args: {
      fee,
      memo,
      funds
    } = {}
  }) => client.mint(msg, fee, memo, funds), options);

The structure of msg are snake case naming:

export interface MintMsgForNullable_Empty {
  extension?: Empty | null;
  owner: string;
  token_id: string;
  token_uri?: string | null;
}

The client mint required args are camel case naming:

mint = async ({
    extension,
    owner,
    tokenId,
    tokenUri
  }: {
    extension?: Empty;
    owner: string;
    tokenId: string;
    tokenUri?: string;
  }, fee: number | StdFee | "auto" = "auto", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
    return await this.client.execute(this.sender, this.contractAddress, {
      mint: {
        extension,
        owner,
        token_id: tokenId,
        token_uri: tokenUri
      }
    }, fee, memo, funds);
  };
@dadamu dadamu changed the title Mutation property type error in generated react query Mutation property type error in generated react query file Nov 11, 2022
@pyramation
Copy link
Collaborator

pyramation commented Nov 11, 2022

so the client is ok, it's a mismatch between the react-query interface and implementation?

@adairrr have you experienced this? I think it could be a relatively simple fix. We may need to duplicate the interface if we're reusing it, and make a snake case version.

also, could be a use-case for https://github.com/sindresorhus/type-fest and we may not even need to duplicate the interface if I understand it correctly.

@dadamu
Copy link
Author

dadamu commented Nov 11, 2022

@pyramation yes, client is ok. The error happens in react-query files.

@adairrr
Copy link
Contributor

adairrr commented Nov 23, 2022

@dadamu can you send me the json schemas from which you're generating the code?

@dadamu
Copy link
Author

dadamu commented Nov 24, 2022

@adairrr sure, here it is https://github.com/CosmWasm/cw-nfts/tree/main/contracts/cw721-base/schema

@adairrr
Copy link
Contributor

adairrr commented Nov 25, 2022

I was able to repro the issue - should be an easy fix. It's interesting to me that the msg uses the actual type instead of expanding it like everywhere else.

The schema references the type that's used:

    {
      "description": "Mint a new NFT, can only be called by the contract minter",
      "type": "object",
      "required": [
        "mint"
      ],
      "properties": {
        "mint": {
          "$ref": "#/definitions/MintMsg_for_Nullable_Empty"
        }
      },
      "additionalProperties": false
    },

@adairrr
Copy link
Contributor

adairrr commented Nov 25, 2022

At the revoke mutation, it uses the pascal case as well:

export interface Test721RevokeMutation {
  client: Test721Client;
  msg: {
    spender: string;
    tokenId: string;
  };
  args?: {...};
}

Ah, but there's no ref here.

    {
      "description": "Remove previously granted Approval",
      "type": "object",
      "required": [
        "revoke"
      ],
      "properties": {
        "revoke": {
          "type": "object",
          "required": [
            "spender",
            "token_id"
          ],
          "properties": {
            "spender": {
              "type": "string"
            },
            "token_id": {
              "type": "string"
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }, 

@pyramation
Copy link
Collaborator

@dadamu try this

Successfully published:
 - @cosmwasm/[email protected]

@dadamu
Copy link
Author

dadamu commented Nov 28, 2022

@pyramation Thanks for the help! However, after this changes, non-cw721 schema codegen will be error:

  var msgType = (0, _utils.createTypedObjectParams)(context, jsonschema).typeAnnotation;
                                                                        ^
TypeError: Cannot read properties of undefined (reading 'typeAnnotation')
    at createReactQueryMutationArgsInterface (~/node_modules/wasm-ast-types/main/react-query/react-query.js:209:73)
    at ~/node_modules/wasm-ast-types/main/react-query/react-query.js:250:13
    at Array.reduce (<anonymous>)
...

The whole schemas I used are here, both cw721-schemas and schemas.

In addition, StdFee and Coin are not imported properly after the last changes, I guess it is because the context.addUtil are removed here.

@adairrr
Copy link
Contributor

adairrr commented Nov 28, 2022

Oof @dadamu that was totally my fault, apologies. Opened a new PR with the fixed changes.
#86

manu0466 pushed a commit to desmos-labs/contract-utils that referenced this issue Dec 12, 2022
Deps: #33
This PR re enables the react query generation, the previous error fixed
by hyperweb-io/ts-codegen#80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants