From 5f92f436cee8fb2c0a655cee529c2250e9c88483 Mon Sep 17 00:00:00 2001 From: Pascal Klesse Date: Fri, 16 Aug 2024 14:10:29 +0200 Subject: [PATCH] fixes --- src/generate.ts | 19 +++++++++---------- src/runtime/composables/gql-mutation.ts | 7 +++---- src/runtime/composables/gql-query.ts | 5 ++--- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/generate.ts b/src/generate.ts index 93ac583..c36b62d 100644 --- a/src/generate.ts +++ b/src/generate.ts @@ -108,16 +108,16 @@ export async function generateComposables(meta: GraphQLMeta): Promise { if (methods?.mutation) { for (const mutation of methods.mutation) { const types = meta.getTypesForMethod(mutation, 'Mutation'); - if (types.customTypes) { - customTypes.push(types.customTypes); - } + customTypes.push(types.customTypes); const inputFieldsType = types.returnType.replace('[]', ''); + const returnTypeIsDefaultType = defaultTypes.includes(types.returnType.toLowerCase()); + template.push( `export const use${capitalizeFirstLetter(mutation)}Mutation = (${ types.argType ? 'variables: { ' + types.argType + ' },' : '' - } fields?: InputFields<${inputFieldsType}>[] | null, log?: boolean): Promise> => gqlMutation<{${mutation}: ${ + } ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} log?: boolean): Promise> => gqlMutation<{${mutation}: ${ types.returnType - }}>('${mutation}', {${types.argType ? 'variables,' : ''} fields, log})`, + }}>('${mutation}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, log})`, ); } } @@ -125,17 +125,16 @@ export async function generateComposables(meta: GraphQLMeta): Promise { if (methods?.subscription) { for (const subscription of methods.subscription) { const types = meta.getTypesForMethod(subscription, 'Subscription'); - if (types.customTypes) { - customTypes.push(types.customTypes); - } + customTypes.push(types.customTypes); const inputFieldsType = types.returnType.replace('[]', ''); + const returnTypeIsDefaultType = defaultTypes.includes(types.returnType.toLowerCase()); template.push( `export const use${capitalizeFirstLetter(subscription)}Subscription = (${ types.argType ? 'variables: { ' + types.argType + ' },' : '' - } fields?: InputFields<${inputFieldsType}>[] | null, log?: boolean): Promise[] | null,`} log?: boolean): Promise> => gqlSubscription<{${subscription}: ${types.returnType}}>('${subscription}', {${types.argType ? 'variables,' : ''} fields, log})`, + }}, any>> => gqlSubscription<{${subscription}: ${types.returnType}}>('${subscription}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, log})`, ); } } diff --git a/src/runtime/composables/gql-mutation.ts b/src/runtime/composables/gql-mutation.ts index 600ecb1..5aca2c8 100644 --- a/src/runtime/composables/gql-mutation.ts +++ b/src/runtime/composables/gql-mutation.ts @@ -3,7 +3,7 @@ import type { UseMutationReturn } from '@vue/apollo-composable'; import { useMutation } from '@vue/apollo-composable'; import { mutation } from 'gql-query-builder'; import gql from 'graphql-tag'; -import { callWithNuxt, useNuxtApp } from 'nuxt/app'; +import { useNuxtApp } from 'nuxt/app'; import type { GraphQLMeta } from '../classes/graphql-meta.class'; import type { IGraphQLOptions } from '../interfaces/graphql-options.interface'; @@ -11,8 +11,7 @@ import type { IGraphQLOptions } from '../interfaces/graphql-options.interface'; import { hashPasswords } from '../functions/graphql-meta'; export async function gqlMutation(method: string, options: IGraphQLOptions = {}): Promise> { - const _nuxt = useNuxtApp(); - const { $graphQl } = _nuxt; + const { $graphQl } = useNuxtApp(); // Check parameters if (!method) { @@ -126,5 +125,5 @@ export async function gqlMutation(method: string, options: IGraphQLOpti console.debug('gqlMutation::documentNode ', documentNode); } - return callWithNuxt(_nuxt, useMutation, [documentNode, { fetchPolicy: 'no-cache', variables }]); + return useMutation(documentNode, { fetchPolicy: 'no-cache', variables }); } diff --git a/src/runtime/composables/gql-query.ts b/src/runtime/composables/gql-query.ts index f5085d3..d24d138 100644 --- a/src/runtime/composables/gql-query.ts +++ b/src/runtime/composables/gql-query.ts @@ -3,7 +3,7 @@ import type { AsyncData } from 'nuxt/app'; import { useAsyncQuery, useLazyAsyncQuery } from '#imports'; import { query } from 'gql-query-builder'; import gql from 'graphql-tag'; -import { callWithNuxt, useNuxtApp } from 'nuxt/app'; +import { useNuxtApp } from 'nuxt/app'; import type { GraphQLMeta } from '../classes/graphql-meta.class'; import type { IGraphQLOptions } from '../interfaces/graphql-options.interface'; @@ -37,7 +37,6 @@ export async function gqlQuery(method: string, options: IGraphQLOptions } const meta = $graphQl() as unknown as GraphQLMeta; - if (!meta) { return; } @@ -117,5 +116,5 @@ export async function gqlQuery(method: string, options: IGraphQLOptions variables: variables, }; - return callWithNuxt(_nuxt, config.lazy ? useLazyAsyncQuery : useAsyncQuery, [queryConfig]); + return config.lazy ? useLazyAsyncQuery(queryConfig) : useAsyncQuery(queryConfig); }