Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Klesse committed Aug 18, 2024
1 parent 7133824 commit 4332dbe
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 65 deletions.
13 changes: 0 additions & 13 deletions src/runtime/composables/gql-meta.ts

This file was deleted.

33 changes: 13 additions & 20 deletions src/runtime/composables/gql-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ 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 { tryUseNuxtApp, useNuxtApp } from 'nuxt/app';

import type { GraphQLMeta } from '../classes/graphql-meta.class';
import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';

export function gqlMutation<T = any>(method: string, options: IGraphQLOptions = {}): UseMutationReturn<T, any> {
const useGqlMeta = () => {
console.log('nuxtApp', tryUseNuxtApp());
const nuxtApp = useNuxtApp();

if (!nuxtApp._meta) {
throw new Error('GraphQLMeta is not available.');
}

return nuxtApp?._meta as GraphQLMeta;
};
import { hashPasswords } from '../functions/graphql-meta';
import { useGqlMetaState } from '../states/gql-meta';

export async function gqlMutation<T = any>(method: string, options: IGraphQLOptions = {}): Promise<UseMutationReturn<T, any>> {
// Check parameters
if (!method) {
throw new Error('No method detected');
Expand All @@ -40,17 +30,20 @@ export function gqlMutation<T = any>(method: string, options: IGraphQLOptions =
console.debug('gqlMutation::fields ', fields);
}

const meta = useGqlMeta();
const { meta } = useGqlMetaState();
if (!meta.value) {
throw new Error('GraphQLMeta is not available.');
}

// if (config.hashPasswords) {
// config.variables = await hashPasswords(config.variables);
// }
if (config.hashPasswords) {
config.variables = await hashPasswords(config.variables);
}

const argType = meta.getArgs(method);
const argType = meta.value.getArgs(method);
const builderInput = {};
const metaFields = meta.getFields(method);
const metaFields = meta.value.getFields(method);
const availableFields = [];
const variables = meta.parseVariables(config.variables, argType.fields, config.log);
const variables = meta.value.parseVariables(config.variables, argType.fields, config.log);

if (!fields) {
for (const [key] of Object.entries(metaFields.fields)) {
Expand Down
25 changes: 9 additions & 16 deletions src/runtime/composables/gql-query.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { useAsyncQuery, useLazyAsyncQuery } from '#imports';
import { query } from 'gql-query-builder';
import gql from 'graphql-tag';
import { type AsyncData, useNuxtApp } from 'nuxt/app';
import { type AsyncData } from 'nuxt/app';

import type { GraphQLMeta } from '../classes/graphql-meta.class';
import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';

import { hashPasswords } from '../functions/graphql-meta';
import { useGqlMetaState } from '../states/gql-meta';

export async function gqlQuery<T = any>(method: string, options: IGraphQLOptions = {}): Promise<AsyncData<T, any>> {
const useGqlMeta = () => {
const nuxtApp = useNuxtApp();

if (!nuxtApp._meta) {
throw new Error('GraphQLMeta is not available.');
}

return nuxtApp?._meta as GraphQLMeta;
};

// Check parameters
if (!method) {
throw new Error('No method detected');
Expand All @@ -41,17 +31,20 @@ export async function gqlQuery<T = any>(method: string, options: IGraphQLOptions
console.debug('gqlQuery::variables ', config.variables);
}

const meta = useGqlMeta();
const { meta } = useGqlMetaState();
if (!meta.value) {
throw new Error('GraphQLMeta is not available.');
}

if (config.hashPasswords) {
config.variables = await hashPasswords(config.variables);
}

const argType = meta.getArgs(method);
const argType = meta.value.getArgs(method);
const builderInput = {};
const metaFields = meta.getFields(method);
const metaFields = meta.value.getFields(method);
const availableFields = [];
const variables = meta.parseVariables(config.variables, argType.fields, config.log);
const variables = meta.value.parseVariables(config.variables, argType.fields, config.log);

if (!fields) {
for (const [key] of Object.entries(metaFields.fields)) {
Expand Down
24 changes: 8 additions & 16 deletions src/runtime/composables/gql-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@ import type { UseSubscriptionReturn } from '@vue/apollo-composable';
import { useSubscription } from '@vue/apollo-composable';
import { subscription } from 'gql-query-builder';
import gql from 'graphql-tag';
import { useNuxtApp } from 'nuxt/app';

import type { GraphQLMeta } from '../classes/graphql-meta.class';
import type { IGraphQLOptions } from '../interfaces/graphql-options.interface';

import { hashPasswords } from '../functions/graphql-meta';
import { useGqlMetaState } from '../states/gql-meta';

export async function gqlSubscription<T = any>(method: string, options: IGraphQLOptions = {}): Promise<UseSubscriptionReturn<T, any>> {
const useGqlMeta = () => {
const nuxtApp = useNuxtApp();

if (!nuxtApp._meta) {
throw new Error('GraphQLMeta is not available.');
}

return nuxtApp?._meta as GraphQLMeta;
};

// Check parameters
if (!method) {
throw new Error('No method detected');
Expand All @@ -36,17 +25,20 @@ export async function gqlSubscription<T = any>(method: string, options: IGraphQL
};

const fields = config.fields as unknown as string[];
const meta = useGqlMeta();
const { meta } = useGqlMetaState();
if (!meta.value) {
throw new Error('GraphQLMeta is not available.');
}

if (config.hashPasswords) {
config.variables = await hashPasswords(config.variables);
}

const argType = meta.getArgs(method);
const argType = meta.value.getArgs(method);
const builderInput = {};
const metaFields = meta.getFields(method);
const metaFields = meta.value.getFields(method);
const availableFields = [];
const variables = meta.parseVariables(config.variables, argType.fields, config.log);
const variables = meta.value.parseVariables(config.variables, argType.fields, config.log);

if (!fields) {
for (const [key] of Object.entries(metaFields.fields)) {
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/plugins/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { callWithNuxt, defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from 'nu
import type { GraphQLMeta } from '../classes/graphql-meta.class';

import { loadMeta } from '../functions/graphql-meta';
import { useGqlMetaState } from '../states/gql-meta';

export default defineNuxtPlugin({
name: 'graphql',
Expand All @@ -19,6 +20,7 @@ export default defineNuxtPlugin({
}

nuxtApp._meta = meta;
useGqlMetaState().meta.value = meta;

return {
provide: {
Expand Down
11 changes: 11 additions & 0 deletions src/runtime/states/gql-meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useState } from 'nuxt/app';

import type { GraphQLMeta } from '../classes/graphql-meta.class';

const gqlMetaState = () => useState<GraphQLMeta | null>('gql_meta_state', () => null);

export function useGqlMetaState() {
return {
meta: gqlMetaState(),
};
}

0 comments on commit 4332dbe

Please sign in to comment.