Skip to content

Commit

Permalink
feat(apollo): update header to more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMunizOdoo committed Oct 22, 2023
1 parent 10d86db commit 7eba38e
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 52 deletions.
7 changes: 1 addition & 6 deletions packages/sdk-api-client/src/setup/clientSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ const buildClient = (settings: MiddlewareConfig) => {
}
});


const httpLink = createHttpLink({
uri: settings.odooGraphqlUrl,
credentials: 'include',
fetch,
fetchOptions: settings.fetchOptions,
headers: {
Cookie: `session_id=${settings.sessionAuth}`,
'resquest-host': settings.requestHost,
'REAL-IP': settings.realIp
}
headers: settings.headers
});

const afterwareLink = new ApolloLink((operation, forward) => {
Expand Down
4 changes: 1 addition & 3 deletions packages/sdk-api-client/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { DocumentNode } from '@apollo/client';
export interface MiddlewareConfig {
odooGraphqlUrl: string;
fetchOptions?: any;
sessionAuth?: string,
requestHost?: string,
realIp?: string,
headers?: Record<string, string>
queries?: Record<string, DocumentNode>

}
1 change: 1 addition & 0 deletions packages/sdk/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Methods = typeof methods;
* Initialize the Odoo connector.
*/
export const odooConnector = (options: Options): Methods => {

client.defaults.baseURL = options.apiUrl

return methods;
Expand Down
67 changes: 47 additions & 20 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
import { odooConnector } from './connector';
import type { Options } from './types';
import { ApolloQueryResult, FetchResult } from '@apollo/client';
import { MutationMetadataParams, QueryMetadataParams } from '@erpgap/odoo-sdk-api-client';
import type { Module } from '@vue-storefront/sdk';

/**
* Boulerplate module type.
*/
import type { Options } from './types';
export interface OdooModuleType extends Module {

/**
* The connector of the Odoo module.
*/
connector: ReturnType<typeof odooConnector>;
}

/**
* Odoo module.
*/
export const OdooModule = (options: Options): OdooModuleType => ({
connector: odooConnector({
apiUrl: options.apiUrl
}),
utils: {},
subscribers: {}
});
export const OdooModule = (options: Options): OdooModuleType => {

const mutation = async<ApiParams, ApiResponseType>(metadata: MutationMetadataParams, params?: ApiParams): Promise<FetchResult<ApiResponseType>> => {
const { data } = await options.fetch.post<FetchResult<ApiResponseType>>('mutation', [metadata, params]);

return data;
}

const query = async <ApiParams, ApiResponseType>(metadata: QueryMetadataParams, params?: ApiParams): Promise<ApolloQueryResult<ApiResponseType>> => {
const { data } = await options.fetch.post<ApolloQueryResult<ApiResponseType>>('query', [metadata, params]);

return data;
}

return {
connector: odooConnector({
apiUrl: options.apiUrl
}),
utils: {},
subscribers: {}
}
};


const mutation = async<ApiParams, ApiResponseType>(metadata: MutationMetadataParams, params?: ApiParams): Promise<FetchResult<ApiResponseType>> => {
const { data } = await client.post<FetchResult<ApiResponseType>>('mutation', [metadata, params]);

return data;
}

const query = async <ApiParams, ApiResponseType>(metadata: QueryMetadataParams, params?: ApiParams): Promise<ApolloQueryResult<ApiResponseType>> => {
const { data } = await client.post<ApolloQueryResult<ApiResponseType>>('query', [metadata, params]);

return data;
}

const odooConnector = (options: Options) => {
client.defaults.baseURL = options.apiUrl

return { mutation, query };
};




export { client } from './client';

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/methods/mutation/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FetchResult } from '@apollo/client';
import { MutationMetadataParams } from '@erpgap/odoo-sdk-api-client';
import { client } from '../../client';
import { client } from '../../index';

/**
* Make the query
*
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface Options {
* The API URL of the client-side environment.
*/
apiUrl: string;
fetch?: any;
}
26 changes: 15 additions & 11 deletions playground-nuxt/app/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import { MutationName } from '~/server/mutations';
const { $sdk } = useNuxtApp();
const go = async () => {
const { data } = await $sdk().odoo.mutation<MutationLoginArgs, LoginMutationResponse>({ mutationName: MutationName.LoginMutation }, {
email: "ergap@odoo.com",
password: "123"
});
// await useFetch('/api/odoo/mutation', {
// method: 'POST',
// body: [
// { mutationName: MutationName.LoginMutation }, {
// email: "ergap@odoo.com",
// password: "123"
// }
// ]
// })
const { data } = await $sdk().odoo.mutation<MutationLoginArgs, LoginMutationResponse>({ mutationName: MutationName.LoginMutation }, {
email: "ergap@odoo.com",
password: "123"
});
const { data: asa } = await $sdk().odoo.query<QueryProductVariantArgs, ProductVariantQueryResponse>({ queryName: QueryName.LoadUserQuery }, {});
console.log(data?.login);
const { data: asa } = await $sdk().odoo.query<QueryProductVariantArgs, ProductVariantQueryResponse>({ queryName: QueryName.LoadUserQuery }, {});
console.log(asa.productVariant.displayName);
};
</script>
Expand Down
4 changes: 2 additions & 2 deletions playground-nuxt/app/plugins/3.sdk.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { initSDK, buildModule } from '@vue-storefront/sdk';
import { OdooModule, OdooModuleType } from '@erpgap/odoo-sdk';
import { OdooModule, OdooModuleType } from '../../../packages/sdk/src';

export default defineNuxtPlugin(async (nuxtApp) => {
const config = useRuntimeConfig();

const sdkConfig = {
odoo: buildModule<OdooModuleType>(OdooModule, {
apiUrl: `${config.public.middlewareUrl}api/odoo/`
Expand Down
22 changes: 14 additions & 8 deletions playground-nuxt/app/server/middleware/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import { Mutations } from '~/server/mutations';

export default defineEventHandler((event) => {

const config : MiddlewareConfig = {
odooGraphqlUrl: `${process.env.ODOO_BASE_URL}graphql/vsf`,
queries: { ...Queries, ...Mutations },
realIp: getRequestIP(event),
sessionAuth: parseCookies(event).session_id,
requestHost: getRequestHost(event)
};
if(event.method == 'POST') {
const config : MiddlewareConfig = {
odooGraphqlUrl: `${process.env.ODOO_BASE_URL}graphql/vsf`,
queries: { ...Queries, ...Mutations },
headers: {
Cookie: `session_id=${parseCookies(event).session_id}`,
'resquest-host': getRequestHost(event),
'REAL-IP': getRequestIP(event) || ''
}
};

event.context.apolloClient = createApiClient(config);
}


event.context.apolloClient = createApiClient(config);
});

0 comments on commit 7eba38e

Please sign in to comment.