1

I need to provide a graph client for Storefront API but I can only build the client after making a network call.

@Provides
@Singleton
fun getGraphClient(context: Context, client: Client): GraphClient {
    return GraphClient.build(
        context = context,
        shopDomain = client.shopifyDomain,
        accessToken = client.storefrontAccessToken
    )
}

How can I after making a network call to get the client pass it to hilt to return the graph client?

Akram Hussain
  • 472
  • 8
  • 23

1 Answers1

0

I solved this by manual dependency injection like this:


@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    var graphClient: GraphClient? = null

    fun provideGraphClient(context: Context, client: Client) {
        graphClient = GraphClient.build(
            context = context,
            shopDomain = client.shopifyDomain,
            accessToken = client.storefrontAccessToken
        )
    }
}

And then after the API call

AppModule.provideGraphClient(context, client)

And inject into any repository like this:

class SomeRepositoryImpl @Inject constructor(
    @ApplicationContext val applicationContext: Context,
    private val graphClient: GraphClient? = AppModule.graphClient
) : SomeRepository {

....

}
Akram Hussain
  • 472
  • 8
  • 23