FilledStacks / flutter-tutorials

The repo contains the source code for all the tutorials on the FilledStacks Youtube channel.
MIT License
4.75k stars 1.76k forks source link

graphql-flutter #11

Closed 0x6a74 closed 5 years ago

0x6a74 commented 5 years ago

hey there. first of all, thank you so much for you effort on bringing flutter architecture to the next level.

I've just followed your tutorial #14 and i am trying to implement graphql-flutter within the proposed architecture. I have a service (ApiGraphqlService) which depends on the AuthenticationService and should be consumed in e.g. PostViewModel.

I've already found a possible solution: https://github.com/zino-app/graphql-flutter/issues/339. But i thought it could be done a more "proper way"?

FilledStacks commented 5 years ago

Hi there, thanks for the kind words.

I don't see where your confusion is so maybe I'm missing something. You are right that it can be done in a more "service-wrapped" way. Services just wrap functionality so to keep your code less dependent on a third party library you can create your GraphQLService and wrap the client functionality you need. Something like this:

class GraphQLService {
GraphQLClient _client;

  Future<Posts> getPosts() {
   var result = _client.query(...);
   return Post.fromResult(results);
  }

}

And then you inject your Service as a normal independent provider

Provider.value(value: GraphQLService());

Then you just inject that into the model as we do with all the other services. Does that answer your question. If it doesn't please be a bit more specific on what you're doing, what's happening and what you're expecting in terms of your architecture then I'll see if I can come up with a better solution.

0x6a74 commented 5 years ago

Thanks for the quick response. My major issue was to get the token (created/saved by the AuthService) into the GraphQLService. I'll now something like this:

class GraphQLService {
  GraphQLClient _client;
  GraphQLService({storageService...})

  Future _createClient(...) {}

  Future<Posts> getPosts() {
     _client = _createClient..
  }
}
FilledStacks commented 5 years ago

Yes that should definitely work. Goodluck, I'm happy to see the architecture being used. If you run into any kind of architectural limitations please let me know so that I can see what the best way would be to handle that situation.