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

How get input data in UpdateCacheHandler? #14

Closed
Feduch opened this issue Apr 1, 2020 · 1 comment
Closed

How get input data in UpdateCacheHandler? #14

Feduch opened this issue Apr 1, 2020 · 1 comment

Comments

@Feduch
Copy link

Feduch commented Apr 1, 2020

Hi!

How get input data from mutation in body UpdateCacheHandler?

final UpdateCacheHandler<$MyMutation> myMutationHandler = (
  CacheProxy proxy,
  QueryResponse<$MyMutation> response,
) {
  final query = MyQueryToUpdate();
  final result = proxy.readQuery(query);

  /// update the result
  proxy.writeQuery(query, result);
};
@smkhalsa
Copy link
Member

smkhalsa commented Apr 1, 2020

You can access the mutation via the queryRequest property on QueryResponse.

Also, if you want to pass in additional data that isn't an input to the mutation, you can pass in a Context object when creating the mutation.

For example:

/// Create a ContextEntry subclass
class UserContextEntry extends ContextEntry {
  final String userId;

  UserContextEntry(this.userId);

  @override
  List<Object> get fieldsForEquality => [userId];
}

/// When instantiating your mutation...
final mutation = MyMutation(
  updateCacheHandlerKey: "myMutationKey",
  /// ...pass in an instance of the ContextEntry subclass
  context: Context.fromList([UserContextEntry("user123")]),
);

final UpdateCacheHandler<$MyMutation> myMutationHandler = (
  CacheProxy proxy,
  QueryResponse<$MyMutation> response,
) {
  final query = MyQueryToUpdate();
  final result = proxy.readQuery(query);

  /// Access the data passed in through ContextEntry
  final userId = response.queryRequest.context.entry<UserContextEntry>().userId;

  /// update the result
  proxy.writeQuery(query, result);
};

I recognize that subclassing ContextEntry as demonstrated above just to pass in a userId String is a bit verbose. I'm open to suggestions to improve the API (@klavs any thoughts here)?

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