Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Update Apollo Helper - Fix type check
Browse files Browse the repository at this point in the history
  • Loading branch information
sawakisei committed May 20, 2019
1 parent 6ea941f commit af84398
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
16 changes: 7 additions & 9 deletions uit.hotel.client/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import { ApolloProvider } from '~/interfaces/ApolloProvider';

declare module 'vuex' {
interface Store<S> {
app: ComponentOptions<Vue> & {
router: VueRouter;
$apolloHelpers: ApolloHelpers;
$apolloProvider: ApolloProvider;
apolloProvider: ApolloProvider;
};
app: ComponentOptions<Vue>;
$router: VueRouter;
}
}

declare module '*.vue' {
export default Vue;
declare module 'vue/types/vue' {
interface Vue {
$apolloHelpers: ApolloHelpers;
$apolloProvider: ApolloProvider;
apolloProvider: ApolloProvider;
}
}

declare global {
interface Window {
onNuxtReady: (callback: () => void) => void;
Expand Down
38 changes: 26 additions & 12 deletions uit.hotel.client/modules/apollo/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient, ApolloError, MutationOptions } from 'apollo-client';
import { FetchResult } from 'apollo-link';
import Vue from 'vue';
import { MutationTree, Store } from 'vuex';
import { notify } from '~/plugins/notify';
import { ApolloHelpers } from '../../interfaces/ApolloHelpers';
import { ApolloNotify } from './interfaces/ApolloNotify';
import { ServerError } from './interfaces/ServerError';

export function apolloClient(store: any): ApolloClient<InMemoryCache> {
if (store.app.apolloProvider) return store.app.apolloProvider.defaultClient;
else if (store.app.$apolloProvider)
return store.app.$apolloProvider.defaultClient;
throw new Error('Không thể tìm thấy apolloProvider hoặc $apolloProvider');
type HasAppType = { app: Vue };
type AppContextType = MutationTree<any> | Store<any> | HasAppType;

const hasApp = (context: object | HasAppType): context is HasAppType =>
'app' in context;

// store/user (mutation login / logout)
export function apolloHelpers(context: AppContextType) {
if (!hasApp(context))
throw new Error('Phải truyền vào một thể hiện của Store');
return context.app.$apolloHelpers;
}

export function apolloHelpers(store: any): ApolloHelpers {
return store.app.$apolloHelpers;
// store/user
export function apolloClient(
context: AppContextType,
): ApolloClient<InMemoryCache> {
if (!hasApp(context))
throw new Error('Phải truyền vào một thể hiện của Store');
const provider = context.app.$apolloProvider || context.app.apolloProvider;
if (!provider) throw new Error('Không thể tìm thấy apolloProvider');
return provider.defaultClient;
}

export function apolloClientNotify(store: any): ApolloNotify {
const client = apolloClient(store);
// mixin/mutable và store/user
export function apolloClientNotify(context: AppContextType): ApolloNotify {
const client = apolloClient(context);
return {
async mutate<TType, TVariables>(
options: MutationOptions<TType, TVariables>,
): Promise<
FetchResult<TType, Record<string, any>, Record<string, any>>
> {
try {
const result = await client.mutate<TType, TVariables>(options);
return result;
return await client.mutate<TType, TVariables>(options);
} catch (error) {
if (
error instanceof ApolloError &&
Expand Down

0 comments on commit af84398

Please sign in to comment.