refactor: introduced the useApi composable, refactored loading configuration #612
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
💅 Refactors
composablesuseCartcomposables that appeared when cart was emptyuseConfigloadConfig method in theuseConfigcomposableuseCustomQuerycomposable depraacatedthemeuseMagentoContigurationcomposables to load only configCurrencySelectorand theStoreSwitchercomponentsTobBarcomponent, moved all business logic to theuseTopBarcomposable🚀 Features
themeuseApicomposable (which replaces theuseCustomQuerycomposable) and the possibility to run GraphQL requests without using composables🏡 Chore
themegraphql-requestlightweight GraphQL clientMotivation and Context
Performance
First of all, we want to reduce main thread working but make components lazy laded and move composables execution to components that are not rendered on page load.
In this case:
useConfigcomposable loads data when user clicks on StoreSwitcher and opens stores modaluseCurrencycomposable load currency data when user click on currency switcher and opens currencies modalState management
Until now we were using the apiClient and composables to load every data.
On the one hand, composables produce singletons and allow to use of data in different places. On the other hand, there are many cases when we don't want to keep data globally and use those mechanisms that are redundant and cause some performance issues when the page is loading.
In addition, API defined in APiClient typically gets a lot of data that is not necessary for specific situations. For example,
storeConfigquery gets all config objects from Magento, but on page load, we need only a few properties.To avoid over-fetching we should use small queries. In this case, the query looks like this:
To do so we added the possibility to execute queries and mutations directly from the app.
onMountedhooks are fired on client-sideuseAsync, oruseFetchare fired on the server-sideBussiness logic in components
Adding business logic in components makes components hard to extend so the new approach assumes that components with business logic use compostable that returns all necessary things like methods, computed values, etc.
Take a look at the example
The
setupfunction returns business logic from theuseTopBarcomposable. In fact, it can look like this:but in my opinion, the first example is more readable.
Thanks to that, business logic is separated from visual representation, and it's a good first step to provide an extensibility framework in the future
Vuex or Pinia stores
This approach allows developers to add an additional building block and use Vuex or Pinia store for state management instead of using sharedRefs. This is not implemented yet, but I wanted to open a discussion.
Requesting other APIs
The
useApicomposable allow making a request to other endpoints that Magento endpoint so this adds some flexibility and additional options to integrate with other services.Note: CORS configuration
Magento does not allow to make requests to GraphQL API directly from the browser, and additional CORS configuration is needed on the Magento side. This open-source module allows to properly configure CORS.
Types of changes
Checklist: