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

Omit fields from a query #336

Closed
Goddchen opened this issue Jan 15, 2024 · 5 comments
Closed

Omit fields from a query #336

Goddchen opened this issue Jan 15, 2024 · 5 comments

Comments

@Goddchen
Copy link

I'm facing the following scenario: In some cases (like feature flags being set or not set) I want to omit a response fields completely from a query sent to the server. The reason: the server does not support this field yet, for example, so it would throw an exception, because it does not know about this field (yet).

How could I achieve this? I have already read about how to achieve it with inputs in the issues section, but how do I do it with query response fields?

Using two different queries for it is not really an option, because a query can contain multiple of those response fields and that would generate a lot of queries real quick 😉

Some example to illustrate this (hand-written pseudo code, can contain syntax errors 😉):

query FetchUser {
  fetch_user() {
    id: ID!
    imageUrl: String!
  }
}

Right now we'd do something like this, writing the query manually:

var $query = r'''
query FetchUser {
  fetch_user() {
    id: ID!
    $userImageFeatureFlag ? 'imageUrl: String!' : ''
  }
}
''';

Is this possible in any way with graphql_codegen?

Thanks

Copy link

👋 @Goddchen
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

@budde377
Copy link
Contributor

Hi @Goddchen - have you had a look at the @skip and @include directive. Alternatively you can always add more queries and call the right one, iiuc.

@Goddchen
Copy link
Author

Hey @budde377 . Yeah we did have a look at @skip and @include. We're using Apollo GraphQL server and unfortunately it will not simply skip the field if it is annotated with @skip, it will somehow evaluate it first and then complain that your query does not match the schema since the schema / the server does not know about the field that is to be skipped 🤦 So that doesn't work unfortunately.

As I mentioned, just using different queries works fine as long as you have only 1 or 2 fields. Since it grows exponentially and we sometimes have 5 or 6 affected fields, this is unfortunately not an option.

@budde377
Copy link
Contributor

Okay so if I understand the problem correctly you electively want to write your queries to multiple schemas depending on some input. Having multiple queries doesn't work for you because you foresee an exponential blow up.

In short the codegen doesn't support dynamic queries; the operations you write are the one that are sent.

There might be an argument for an operation preprocessor who could figure out this and only send the right query but I think it requires more work and thought.

Until then, I'd advise you to think if you can't impose some constraints on your api. I'd expect your use case is usually handled by establishing good release guaranties and deprecation policies. But I of course don't know your use case in detail.

@Goddchen
Copy link
Author

TLDR: it's not possible right now.

Yeah you're totally right. Thanks for looking into it.

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

2 participants