Apollo-ios: Question: How to support feature flags in queries.

Created on 13 Nov 2019  路  7Comments  路  Source: apollographql/apollo-ios

TL;DR
Question: Does anyone have any good methods of supporting Feature Flags for portions of the queries?

Problem:
We're using the apollo-codegen for both iOS and Android.
There is a tight coupling between the client and the server with respect to the schema.

Example:
1) Schema is updated to v1.1
2) Server is updated to v1.1
3) Client is updated to v1.1 and starts querying for the new fields
4) Server rolls back to v1.0

One option is to have a highly dynamic schema registry system on the server so as to not allow rollbacks. Let's table that idea for now.

Question: Does anyone have any good methods of supporting Feature Flags for portions of the queries?

Note: I know you can pass in parameters to switch off parts of the query but they don't work with fragments as there's no parameterized fragments ex: @skip(if: Boolean) doesn't work in fragments

Ideal Example might be

query {
   offers { 
       ...offer

   }
}

fragment offer on Offer {
   apr
@if global.flagApprovalOddsEnabled
   approvalOdds 
@endif 
}

Challenges

  • iOS and Android Code gen needs to be modified
question

Most helpful comment

I just tried this out using the fullstack tutorial server that we're using to build the new iOS tutorial, and it looks like this is doable. Here's an example query with a fragment and the passed-in boolean set to true, so the ID is returned:

Screen Shot 2019-12-04 at 1 31 19 PM

And then here's an example where it's set to false, so the ID is not returned:

Screen Shot 2019-12-04 at 1 31 38 PM

Does that help?

All 7 comments

Ouch. Yeah I think the idea that your server would roll back to 1.0 once 1.1 had made it that far out the door is not something GraphQL is particularly designed to support. Lemme ping a couple folks.

One thing I did think of: You can use directives that determine whether to include a fragment. You'd have to write two fragments, one for each version, but then you should be able to use the @include directive to pick which of the fragments to use.

(edit: Here's an example of using @include with a fragment)

@jbaxleyiii suggests trying something that defines the variable in the query and then uses it in the fragment, like:

query($showApproval: Boolean!) {
   offers { 
       ...offer
   }
}
fragment offer on Offer {
   apr
   approvalOdds  @include(if: $showApproval)
}

For what it's worth the spec for @include doesn't seem to have any prohibition on using it in a fragment.

@john-twigg-ck Does this answer your question?

@designatednerd
I don't think this works, because showApproval is not visible inside fragment offer on Offer

(Please tell me I'm wrong, because I hope I am)

This falls under the category of "Fragment Variables" which is at best _experimental feature_ in Apollo and GraphQL spec

https://github.com/apollographql/graphql-tag/blob/50a850e484a60d95ddb99801c39785031e55b7a2/README.md#experimental-fragment-variables

https://github.com/graphql/graphql-spec/issues/204

I'm open to other ideas.

I just tried this out using the fullstack tutorial server that we're using to build the new iOS tutorial, and it looks like this is doable. Here's an example query with a fragment and the passed-in boolean set to true, so the ID is returned:

Screen Shot 2019-12-04 at 1 31 19 PM

And then here's an example where it's set to false, so the ID is not returned:

Screen Shot 2019-12-04 at 1 31 38 PM

Does that help?

I am going to close this out since there is an example of what @john-twigg-ck has requested provided. John, if you have further questions here, please feel free to reopen. Anyone else, please open a new issue. Thank you!

Was this page helpful?
0 / 5 - 0 ratings