Skip to content

Commit

Permalink
feat: add mutation generic feature
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMunizOdoo committed Oct 9, 2023
1 parent 2e9aa5b commit d917a6b
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/sdk-api-client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export { sendResetUserPassword } from './user/sendResetUserPassword';
export { updatePassword } from './user/updatePassword';
export { changePassword } from './user/changePassword';
export { query } from './query';
export { mutation } from './mutation';
15 changes: 15 additions & 0 deletions packages/sdk-api-client/src/api/mutation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Endpoints, OdooIntegrationContext, MutationMetadataParams } from '../../types';

export const mutation: Endpoints['mutation'] = async <ApiParams, ApiResponseType>(context: OdooIntegrationContext, metadata: MutationMetadataParams, params?: ApiParams) => {
const mutation = context.config.queries[metadata.mutationName];

const response = await context.client.mutate<ApiResponseType, ApiParams>({
variables: params,
mutation,
fetchPolicy: 'no-cache',
errorPolicy: 'all'
});

return response;
};

1 change: 0 additions & 1 deletion packages/sdk-api-client/src/api/query/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DocumentNode, gql } from '@apollo/client';
import { Endpoints, OdooIntegrationContext, QueryMetadataParams } from '../../types';

export const query: Endpoints['query'] = async <ApiParams, ApiResponseType>(context: OdooIntegrationContext, metadata: QueryMetadataParams, params?: ApiParams) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/sdk-api-client/src/types/api/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export interface QueryMetadataParams {
queryName: string;
cacheKey?: string
}
export interface MutationMetadataParams {
mutationName: string;
}

/**
* Definition of all API-client methods available in {@link https://docs.vuestorefront.io/v2/advanced/context.html#context-api | context}.
Expand Down Expand Up @@ -80,6 +83,7 @@ export interface Endpoints {
getMailingContacts(context: OdooIntegrationContext, params?: QueryMailingContactsArgs, customQuery?: CustomQuery<'getMailingContacts'>): Promise<ApolloQueryResult<{ mailingContacts: { mailingContacts: MailingContact[] } }>>;

query<ApiParams, ApiResponseType>(context: OdooIntegrationContext, metadata: QueryMetadataParams, params?: ApiParams): Promise<ApolloQueryResult<ApiResponseType>>;
mutation<ApiParams, ApiResponseType>(context: OdooIntegrationContext, metadata: MutationMetadataParams, params?: ApiParams): Promise<FetchResult<ApiResponseType>>;

signUpUser(context: OdooIntegrationContext, params: MutationRegisterArgs, customQuery?: CustomQuery<'register'>): Promise<FetchResult<{ user: User}>>;
updateMyAccount(context: OdooIntegrationContext, params: UpdateMyAccountParams, customQuery?: CustomQuery<'updateMyAccount'>): Promise<FetchResult<{ partner: Partner}>>;
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './sendResetUserPassword';
export * from './updatePassword';
export * from './changePassword';
export * from './query';
export * from './mutation';
14 changes: 14 additions & 0 deletions packages/sdk/src/methods/mutation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FetchResult } from '@apollo/client';
import { MutationMetadataParams } from '@erpgap/odoo-sdk-api-client';
import { client } from '../../client';

/**
* Make the query
*
* @example
*/
export async function mutation<ApiParams, ApiResponseType>(metadata: MutationMetadataParams, params?: ApiParams): Promise<FetchResult<ApiResponseType>> {
const { data } = await client.post<FetchResult<ApiResponseType>>('mutation', [metadata, params]);

return data;
}
2 changes: 2 additions & 0 deletions playground/app/graphql/gql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./fragment-masking";
export * from "./gql";
10 changes: 8 additions & 2 deletions playground/app/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ProductVariant } from "@erpgap/odoo-sdk-api-client";
import { Partner, ProductVariant } from "~/graphql";

export type ProductVariantQueryResponse {
export type ProductVariantQueryResponse = {
productVariant: ProductVariant
}

export type LoginMutationResponse = {
login: {
partner: Partner
}
}
13 changes: 9 additions & 4 deletions playground/app/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<script setup lang="ts">
import { QueryProductVariantArgs, ProductVariantQueryResponse } from '~/graphql'
import { QueryProductVariantArgs, ProductVariantQueryResponse, MutationLoginArgs, LoginMutationResponse } from '~/graphql'
import { sdk } from '~/sdk.config';
const { data } = await sdk.odoo.query<QueryProductVariantArgs, ProductVariantQueryResponse>({ queryName: 'productVariantQuery' }, {
import { QueryName, MutationName } from '@erp-gap/server/src/types';
const { data } = await sdk.odoo.query<QueryProductVariantArgs, ProductVariantQueryResponse>({ queryName: QueryName.ProductVariantQuery }, {
productTemplateId: 39,
combinationId: [12, 305]
});
console.log(data.productVariant.displayName);
// const { data } = await useAsyncData('ttt', async () => await sdk.odoo.getCountries())
const { data: dataMut } = await sdk.odoo.mutation<MutationLoginArgs, LoginMutationResponse>({ mutationName: MutationName.LoginMutation }, {
email: "[email protected]",
password: "123"
});
console.log(dataMut?.login.partner.name);
</script>

Expand Down
5 changes: 3 additions & 2 deletions playground/server/middleware.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Integration } from '@vue-storefront/middleware';
import queries from './queries'
import { Queries } from './queries'
import { Mutations } from './mutations'

const odooIntegration: Integration = {
location: '@erpgap/odoo-sdk-api-client/server',
configuration: {
odooGraphqlUrl: 'https://vsfdemo15.labs.odoogap.com/graphql/vsf',
queries
queries: { ...Queries, ...Mutations }
}
};

Expand Down
23 changes: 23 additions & 0 deletions playground/server/mutations/LoginMutation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { gql } from '@apollo/client';
export default gql`
mutation($email: String!, $password: String!) {
login(email: $email, password: $password) {
partner{
id
name
street
street2
city
state
{
id
}
country
{
id
}
email
phone
}
}
}`;
15 changes: 15 additions & 0 deletions playground/server/mutations/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DocumentNode } from '@apollo/client';
import LoginMutation from './LoginMutation';

enum MutationName {
LoginMutation = 'LoginMutation'
}

const Mutations : Record<MutationName, DocumentNode> = {
LoginMutation
}

export {
Mutations,
MutationName
}
15 changes: 11 additions & 4 deletions playground/server/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { DocumentNode } from '@apollo/client';
import productVariantQuery from './productVariantQuery';
import ProductVariantQuery from './ProductVariantQuery';

const queries : Record<string, DocumentNode> = {
productVariantQuery
enum QueryName {
ProductVariantQuery = 'ProductVariantQuery'
}

const Queries : Record<QueryName, DocumentNode> = {
ProductVariantQuery
}

export default queries
export {
Queries,
QueryName
}
8 changes: 8 additions & 0 deletions playground/server/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { QueryName } from '../queries'
import { MutationName} from '../mutations'

export {
QueryName,
MutationName
}

0 comments on commit d917a6b

Please sign in to comment.