Skip to content

Commit

Permalink
fix: update graphql errors
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoMunizOdoo committed Dec 13, 2023
1 parent a001b47 commit 672061a
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 109 deletions.
26 changes: 0 additions & 26 deletions docs/tips copy/customTypes.md

This file was deleted.

15 changes: 0 additions & 15 deletions docs/tips copy/i18n.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/nuxt-layer/server/middleware/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Mutations } from '~/server/mutations';
export default defineEventHandler((event) => {

const config : MiddlewareConfig = {
// https://odoo-cla.ce.promptequation.com/graphql/vsf
odooGraphqlUrl: `${process.env.ODOO_BASE_URL}graphql/vsf`,
queries: { ...Queries, ...Mutations },
headers: {
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk-api-client/src/api/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import consola from 'consola';

export const query: Endpoints['query'] = async <ApiParams, ApiResponseType>(context: OdooIntegrationContext, metadata: QueryMetadataParams, params?: ApiParams) => {

if(!metadata || !metadata.queryName) {
const msg = 'Developer Error: queryName is required'
if (!metadata || !metadata.queryName) {
const msg = 'Developer Error: queryName is required';
consola.error(msg);
throw msg;
}

if(!context.config.queries || Object.keys(context.config?.queries)?.length == 0) {
const msg = 'Developer Error: queries must be configured (MiddlewareConfig.queries)'
if (!context.config.queries || Object.keys(context.config?.queries)?.length == 0) {
const msg = 'Developer Error: queries must be configured (MiddlewareConfig.queries)';
consola.error(msg);
throw msg;
}

const query = context.config.queries[metadata.queryName];

if(!query) {
const msg = `Developer Error: query ${metadata.queryName} is not configured in middleware`
if (!query) {
const msg = `Developer Error: query ${metadata.queryName} is not configured in middleware`;
consola.error(msg);
throw msg;
}
Expand Down
19 changes: 3 additions & 16 deletions packages/sdk-api-client/src/setup/clientSetup.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@

import { ApolloClient, ApolloLink, InMemoryCache, createHttpLink } from '@apollo/client';
import { onError } from "@apollo/client/link/error";
import { MiddlewareConfig } from '../index';
import consola from 'consola';

import fetch from 'cross-fetch';

const buildClient = (settings: MiddlewareConfig) => {

const errorLink = onError(({ graphQLErrors, networkError, operation }) => {
if (graphQLErrors) {
graphQLErrors.map((error) => consola.error({label: '[GRAPHQL ERROR]', ...error, operation }));
}

if (networkError) {
consola.error({ label: '[NETWORK ERROR]', message: networkError });
}
});

const httpLink = createHttpLink({
uri: settings.odooGraphqlUrl,
credentials: 'include',
Expand All @@ -40,7 +28,6 @@ const buildClient = (settings: MiddlewareConfig) => {
});

const apolloLink = ApolloLink.from([
errorLink,
afterwareLink.concat(httpLink)
]);

Expand All @@ -51,11 +38,11 @@ const buildClient = (settings: MiddlewareConfig) => {
credentials: 'include',
defaultOptions: {
query: {
errorPolicy: "all",
fetchPolicy: "no-cache",
errorPolicy: 'all',
fetchPolicy: 'no-cache'
},
mutate: {
errorPolicy: "all",
errorPolicy: 'all'
}
}
});
Expand Down
17 changes: 5 additions & 12 deletions packages/sdk-api-client/src/setup/logBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Logger } from 'winston';
import consola from 'consola';

interface LooseObject {
label?: string,
Expand All @@ -17,22 +17,15 @@ function getGqlString(doc: any) {
}

export default ({ label, message, locations, path, operation }: LooseObject) : void=> {
const logger : Logger = (process as any).winstonLog;

const log : LooseObject = {
label,
message: message,
path: path
};
const log : LooseObject = { label, message: message, path: path };

if (locations) {
log.location = locations?.map(item => `line: ${item.line} | column: ${item.column}`).join(' ');
}

if (process.env.NODE_LOG_LEVEL === 'TRACE') {
log.query = getGqlString(operation.query);
log.variables = operation.variables;
}
log.query = getGqlString(operation.query);
log.variables = operation.variables;

logger.error(log);
consola.error(log);
};
24 changes: 12 additions & 12 deletions packages/sdk/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ type Methods = typeof methods;
* Initialize the Odoo connector.
*/
export const odooConnector = (options: Options): Methods => {
let mutation = null
let query = null
let mutation = null;
let query = null;

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

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

if(options.ofetch) {
if (options.ofetch) {
mutation = async <ApiParams, ApiResponseType>(metadata: MutationMetadataParams, params?: ApiParams): Promise<ApiResponseType> => {
return await options.ofetch('/api/odoo/mutation', { method: 'POST', body: [metadata, params], cache: 'no-cache' })
}
return await options.ofetch('/api/odoo/mutation', { method: 'POST', body: [metadata, params], cache: 'no-cache' });
};

query = async <ApiParams, ApiResponseType>(metadata: QueryMetadataParams, params?: ApiParams): Promise<ApiResponseType> =>{
return await options.ofetch('/api/odoo/query', { method: 'POST', body: [metadata, params], cache: 'no-cache' })
}
return await options.ofetch('/api/odoo/query', { method: 'POST', body: [metadata, params], cache: 'no-cache' });
};
}

client.defaults.baseURL = options.apiUrl
client.defaults.baseURL = options.apiUrl;

return { query, mutation };
};
15 changes: 10 additions & 5 deletions playground-nuxt/app/graphql/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Partner, ProductVariant } from "~/graphql";
import { _AsyncData } from 'nuxt/dist/app/composables/asyncData';
import { H3Error } from 'h3';
import { Partner, ProductVariant } from '~/graphql';

export type ProductVariantQueryResponse = {
export type SalesPersonResponse = _AsyncData<
{
productVariant: ProductVariant
}
},
H3Error
>;

export type LoginMutationResponse = {
export type LoginMutationResponse = {
login: {
partner: Partner
}
}
}
18 changes: 7 additions & 11 deletions playground-nuxt/app/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<script setup lang="ts">
import { QueryProductVariantArgs, ProductVariantQueryResponse, MutationLoginArgs, LoginMutationResponse } from '~/graphql'
import { QueryProductVariantArgs, ProductVariantQueryResponse, MutationLoginArgs, LoginMutationResponse } from '~/graphql';
import { QueryName } from '~/server/queries';
import { MutationName } from '~/server/mutations';
const { $sdk } = useNuxtApp();
onMounted(async () => {
const data = await $sdk().odoo.mutation<MutationLoginArgs, LoginMutationResponse>({ mutationName: MutationName.LoginMutation }, {
email: "[email protected]",
password: "123"
});
// onMounted(async () => {
const data = await $sdk().odoo.query<QueryProductVariantArgs, any>({ queryName: QueryName.GetProductTemplateList }, {});
console.log(data);
const { data: asa } = await $sdk().odoo.query<QueryProductVariantArgs, ProductVariantQueryResponse>({ queryName: QueryName.LoadUserQuery }, {});
})
console.log(data);
const asa = await $sdk().odoo.query<QueryProductVariantArgs, any>({ queryName: QueryName.GetCategories }, {});
console.log(asa);
// });
</script>

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions playground-nuxt/app/plugins/3.sdk.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { initSDK, buildModule } from '@vue-storefront/sdk';
import { OdooModule, OdooModuleType } from '../../../packages/sdk/src';
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/`,
ofetch: $fetch
ofetch: useFetch
})
};

Expand Down
41 changes: 41 additions & 0 deletions playground-nuxt/app/server/queries/GetCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { partnerFragment } from './fragments';
import { gql } from '@apollo/client/core';

export default gql`
query(
$search: String
$filter: CategoryFilterInput
$currentPage: Int
$pageSize: Int
$sort: CategorySortInput
) {
categories(
search: $search
filter: $filter
currentPage: $currentPage
pageSize: $pageSize
sort: $sort
) {
categories {
id
name
slug
childs {
id
name
slug
childs {
id
name
slug
}
}
parent {
id
name
slug
}
}
}
}
`;
37 changes: 37 additions & 0 deletions playground-nuxt/app/server/queries/GetProductTemplateList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { gql } from '@apollo/client/core';

export default gql`
query(
$filter: ProductFilterInput
$currentPage: Int
$pageSize: Int = 0
$search: String
$sort: ProductSortInput
) {
products(
filter: $filter
currentPage: $currentPage
pageSize: $pageSize
search: $search
sort: $sort
) {
totalCount
attributeValues {
id
name
displayType
name
htmlColor
search
attribute{
id
name
}
}
products {
id
}
}
}
`;
10 changes: 8 additions & 2 deletions playground-nuxt/app/server/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { DocumentNode } from '@apollo/client';
import ProductVariantQuery from './ProductVariantQuery';
import LoadUserQuery from './LoadUserQuery';
import GetCategories from './GetCategories';
import GetProductTemplateList from './GetProductTemplateList';

enum QueryName {
ProductVariantQuery = 'ProductVariantQuery',
LoadUserQuery = 'LoadUserQuery'
LoadUserQuery = 'LoadUserQuery',
GetCategories = 'GetCategories',
GetProductTemplateList = 'GetProductTemplateList'
}

const Queries : Record<QueryName, DocumentNode> = {
ProductVariantQuery,
LoadUserQuery
LoadUserQuery,
GetCategories,
GetProductTemplateList
};

export {
Expand Down

0 comments on commit 672061a

Please sign in to comment.