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 can I use graphql-flutter with MultiProvider #339

Closed
jerryzhoujw opened this issue Jun 14, 2019 · 6 comments
Closed

How can I use graphql-flutter with MultiProvider #339

jerryzhoujw opened this issue Jun 14, 2019 · 6 comments

Comments

@jerryzhoujw
Copy link

jerryzhoujw commented Jun 14, 2019

Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<AuthState>.value(
          notifier: AuthState(),
        ),
        // GraphQLProvider(child: _client,),              //<-- error
        ChangeNotifierProvider<GraphQLClient>.value(      //<-- error
          notifier: _client,
        ),
        // ValueNotiferProvider.val
      ],
      child: MaterialApp(
           ....
      ),
    );
  }
}

How can I do this above? Just want to make auth server & graphQL service together.

I was plan to separate multi service(assume include Auth, graphQL, store and so on ) use MultiProvider, so I'd like use GraphQLProvider like this too, but it gives error. Any suggestion for separation like these?

@eusdima
Copy link
Contributor

eusdima commented Jun 14, 2019

@HofmannZ you did it for our apps, can you share with him how you did the multi provider?

@mainawycliffe
Copy link
Collaborator

mainawycliffe commented Jun 17, 2019

GraphQLProvider is just a widget, which wraps around an inherited widget. This inherited widget is the one that pump down the client to all descendants, so that when you use the Query widget, it can access the client from the GraphQLProvider.

One solution i can think of is to pass the client itself, something like:

MultiProvider(
  providers: [
    Provider<ValueNotifier<GraphQLClient>>.value(value: client),
  ],
  child: someWidget,
)

And the either use the dart client or create GraphQLProvider Widget at the appropriate location in the widget tree with getting the client from Provider. You can also use the GraphQLProvider widget as the direct child of MultiProvider.

On a package level, may be we can have something like QueryWithProvider widget, that reads the client value from provider instead of the inherited widget, but otherwise behaves the same. I think this can be done without breaking anything.

@jerryzhoujw
Copy link
Author

yes, I use GraphQLProvider as child of MultiProvider for now, Thank you @mainawycliffe

@franzejr
Copy link

Can you guys give a more detailed example on how to use it with MultiProvider? Thanks!

@mainawycliffe
Copy link
Collaborator

mainawycliffe commented Jul 18, 2019

For now, the easiest way I can think of is using GraphQLProvider as a child of MultiProvider. Something like:

MultiProvider(
  providers: [
    Provider<Foo>.value(value: foo),
    Provider<Bar>.value(value: bar),
    Provider<Baz>.value(value: baz),
  ],
  child: GraphQLProvider(...),
)

@Jaymal1
Copy link

Jaymal1 commented Sep 8, 2021

Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<AuthState>.value(
          notifier: AuthState(),
        ),
        // GraphQLProvider(child: _client,),              //<-- error
        ChangeNotifierProvider<GraphQLClient>.value(      //<-- error
          notifier: _client,
        ),
        // ValueNotiferProvider.val
      ],
      child: MaterialApp(
           ....
      ),
    );
  }
}

How can I do this above? Just want to make auth server & graphQL service together.

I was plan to separate multi service(assume include Auth, graphQL, store and so on ) use MultiProvider, so I'd like use GraphQLProvider like this too, but it gives error. Any suggestion for separation like these?

how did you solve it please?

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

5 participants