diff --git a/packages/sdk-api-client/src/api/index.ts b/packages/sdk-api-client/src/api/index.ts index bcb88696..4b233d94 100644 --- a/packages/sdk-api-client/src/api/index.ts +++ b/packages/sdk-api-client/src/api/index.ts @@ -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'; diff --git a/packages/sdk-api-client/src/api/mutation/index.ts b/packages/sdk-api-client/src/api/mutation/index.ts new file mode 100644 index 00000000..1b21bebf --- /dev/null +++ b/packages/sdk-api-client/src/api/mutation/index.ts @@ -0,0 +1,15 @@ +import { Endpoints, OdooIntegrationContext, MutationMetadataParams } from '../../types'; + +export const mutation: Endpoints['mutation'] = async (context: OdooIntegrationContext, metadata: MutationMetadataParams, params?: ApiParams) => { + const mutation = context.config.queries[metadata.mutationName]; + + const response = await context.client.mutate({ + variables: params, + mutation, + fetchPolicy: 'no-cache', + errorPolicy: 'all' + }); + + return response; +}; + diff --git a/packages/sdk-api-client/src/api/query/index.ts b/packages/sdk-api-client/src/api/query/index.ts index b914b400..6ee77a72 100644 --- a/packages/sdk-api-client/src/api/query/index.ts +++ b/packages/sdk-api-client/src/api/query/index.ts @@ -1,4 +1,3 @@ -import { DocumentNode, gql } from '@apollo/client'; import { Endpoints, OdooIntegrationContext, QueryMetadataParams } from '../../types'; export const query: Endpoints['query'] = async (context: OdooIntegrationContext, metadata: QueryMetadataParams, params?: ApiParams) => { diff --git a/packages/sdk-api-client/src/types/api/endpoints.ts b/packages/sdk-api-client/src/types/api/endpoints.ts index 4c0317fa..73d290e0 100644 --- a/packages/sdk-api-client/src/types/api/endpoints.ts +++ b/packages/sdk-api-client/src/types/api/endpoints.ts @@ -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}. @@ -80,6 +83,7 @@ export interface Endpoints { getMailingContacts(context: OdooIntegrationContext, params?: QueryMailingContactsArgs, customQuery?: CustomQuery<'getMailingContacts'>): Promise>; query(context: OdooIntegrationContext, metadata: QueryMetadataParams, params?: ApiParams): Promise>; + mutation(context: OdooIntegrationContext, metadata: MutationMetadataParams, params?: ApiParams): Promise>; signUpUser(context: OdooIntegrationContext, params: MutationRegisterArgs, customQuery?: CustomQuery<'register'>): Promise>; updateMyAccount(context: OdooIntegrationContext, params: UpdateMyAccountParams, customQuery?: CustomQuery<'updateMyAccount'>): Promise>; diff --git a/packages/sdk/src/methods/index.ts b/packages/sdk/src/methods/index.ts index 055b333f..7a4fb06f 100644 --- a/packages/sdk/src/methods/index.ts +++ b/packages/sdk/src/methods/index.ts @@ -27,3 +27,4 @@ export * from './sendResetUserPassword'; export * from './updatePassword'; export * from './changePassword'; export * from './query'; +export * from './mutation'; diff --git a/packages/sdk/src/methods/mutation/index.ts b/packages/sdk/src/methods/mutation/index.ts new file mode 100644 index 00000000..c5e10cad --- /dev/null +++ b/packages/sdk/src/methods/mutation/index.ts @@ -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(metadata: MutationMetadataParams, params?: ApiParams): Promise> { + const { data } = await client.post>('mutation', [metadata, params]); + + return data; +} diff --git a/playground/app/graphql/gql/index.ts b/playground/app/graphql/gql/index.ts new file mode 100644 index 00000000..f5159916 --- /dev/null +++ b/playground/app/graphql/gql/index.ts @@ -0,0 +1,2 @@ +export * from "./fragment-masking"; +export * from "./gql"; \ No newline at end of file diff --git a/playground/app/graphql/types.ts b/playground/app/graphql/types.ts index c9d73cba..7d9589cf 100644 --- a/playground/app/graphql/types.ts +++ b/playground/app/graphql/types.ts @@ -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 + } } \ No newline at end of file diff --git a/playground/app/pages/index.vue b/playground/app/pages/index.vue index 817f2475..889df5d4 100644 --- a/playground/app/pages/index.vue +++ b/playground/app/pages/index.vue @@ -1,15 +1,20 @@ diff --git a/playground/server/middleware.config.ts b/playground/server/middleware.config.ts index f9aa0bb6..4c2360f3 100644 --- a/playground/server/middleware.config.ts +++ b/playground/server/middleware.config.ts @@ -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 } } }; diff --git a/playground/server/mutations/LoginMutation.ts b/playground/server/mutations/LoginMutation.ts new file mode 100644 index 00000000..8d6882d9 --- /dev/null +++ b/playground/server/mutations/LoginMutation.ts @@ -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 + } + } + }`; diff --git a/playground/server/mutations/index.ts b/playground/server/mutations/index.ts new file mode 100644 index 00000000..d22d32e7 --- /dev/null +++ b/playground/server/mutations/index.ts @@ -0,0 +1,15 @@ +import { DocumentNode } from '@apollo/client'; +import LoginMutation from './LoginMutation'; + +enum MutationName { + LoginMutation = 'LoginMutation' + } + +const Mutations : Record = { + LoginMutation +} + +export { + Mutations, + MutationName +} \ No newline at end of file diff --git a/playground/server/queries/productVariantQuery.ts b/playground/server/queries/ProductVariantQuery.ts similarity index 100% rename from playground/server/queries/productVariantQuery.ts rename to playground/server/queries/ProductVariantQuery.ts diff --git a/playground/server/queries/index.ts b/playground/server/queries/index.ts index 183a2985..d58588b3 100644 --- a/playground/server/queries/index.ts +++ b/playground/server/queries/index.ts @@ -1,8 +1,15 @@ import { DocumentNode } from '@apollo/client'; -import productVariantQuery from './productVariantQuery'; +import ProductVariantQuery from './ProductVariantQuery'; -const queries : Record = { - productVariantQuery +enum QueryName { + ProductVariantQuery = 'ProductVariantQuery' + } + +const Queries : Record = { + ProductVariantQuery } -export default queries \ No newline at end of file +export { + Queries, + QueryName +} \ No newline at end of file diff --git a/playground/server/src/types.ts b/playground/server/src/types.ts new file mode 100644 index 00000000..dcdcc988 --- /dev/null +++ b/playground/server/src/types.ts @@ -0,0 +1,8 @@ +import { QueryName } from '../queries' +import { MutationName} from '../mutations' + +export { + QueryName, + MutationName +} +