Skip to content
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

Gql() variables are not type-safe #335

Open
ValentinH opened this issue Sep 7, 2022 · 10 comments
Open

Gql() variables are not type-safe #335

ValentinH opened this issue Sep 7, 2022 · 10 comments

Comments

@ValentinH
Copy link

While playing with the new changes introduced in v5, I noticed that Apollo queries and mutations variables can now be made typesafe by using the typedGql helper (as documented here).

However, I noticed that the barebone version of using the Gql function isn't type-safe. Is it a known limitation?

From the examples (https://github.com/graphql-editor/graphql-zeus/blob/master/examples/typescript-node/src/index.ts#L241-L273):

  const aliasedQueryExecute = await Gql('query')(
    {
      listCards: {
        __alias: {
          namy: {
            name: true,
          },
          atak: {
            attack: [
              { cardID: $('cardIds', '[String!]!') },
              {
                name: true,
                __alias: {
                  bbb: {
                    Defense: true,
                  },
                  ccc: {
                    Children: true,
                  },
                },
              },
            ],
          },
        },
        id: true,
      },
    },
    {
      variables: {
        cardIds: ['aaa'],
      },
    },
  );

It is using the $() function and providing a graphql type but I can pass any data to cardIds without any type error.

@IceBlizz6
Copy link

Possible duplicate of #232 (???)

@ValentinH
Copy link
Author

No this is a different topic.

@bintoll
Copy link

bintoll commented Oct 18, 2024

I also came to this topic with the same question, seems that unfortunately params are not typesafe

@IceBlizz6
Copy link

Sad to hear that this hasn't been solved, I'm not very familiar with this codebase but i hope they will try to solve it.
I still think this should be a priority 1 issue.
A type safe GraphQL client library loses a lot of its value without type safety.

I eventually ended up just creating my own type safe library which have worked fine for a year now, so it's clearly possible to do. Since i started that i noticed that there are many other existing libraries popped up, so maybe try to check out some of those to see if they have covered this need.
Otherwise i may just end up publishing the library i created in the near future.

@ValentinH
Copy link
Author

On our side, we moved to https://the-guild.dev/graphql/codegen. It's great, nothing to complain about since then.

@bintoll
Copy link

bintoll commented Oct 18, 2024

On our side, we moved to https://the-guild.dev/graphql/codegen. It's great, nothing to complain about since then.

I am using this now with typed-document-node and it works great, but recently got the requirement to strictly mange data that FE requests from BE, so it is needed to build queries dynamically, and is not possible with codegen, unfortunatelly

@ValentinH
Copy link
Author

What is dynamic: the returned data or the query args?

@aexol
Copy link
Collaborator

aexol commented Oct 18, 2024

They are typesafe if you use with typed document node. @bintoll If you have dynamic queries do it without a lib. By the way @ValentinH of course codegen is a great library, but It's like comparing apples and oranges - 2 different use cases.

@bintoll
Copy link

bintoll commented Oct 18, 2024

What is dynamic: the returned data or the query args?

The data that is returned in response from BE should be dynamic in my case and I need a possibility to build a query with js.

@bintoll
Copy link

bintoll commented Oct 18, 2024

They are typesafe if you use with typed document node. @bintoll If you have dynamic queries do it without a lib. By the way @ValentinH of course codegen is a great library, but It's like comparing apples and oranges - 2 different use cases.

Снимок экрана 2024-10-18 в 16 28 30

How this could be typesafe if all parameters has Variable<any, string> in index.ts after generating the code?
So $('Uid', 'Date!') does not raise an error, that Uid can only be ID! as per schema

Your suggestion no not to use lib unfortunately is not relevant in my case, I would like to use the typesafe dynamic query build, to build type document node in order to use later it request.

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

No branches or pull requests

4 participants