Skip to content

Commit

Permalink
fix: reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Klesse committed Jun 17, 2024
1 parent a22e480 commit 37e2ab7
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 112 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [3.9.0](https://github.com/lenneTech/nuxt-base/compare/v3.8.0...v3.9.0) (2024-05-29)


### Features

* update packages ([19e82b6](https://github.com/lenneTech/nuxt-base/commit/19e82b6c744356244c697383362d7a666431884d))
* update packages ([0de5029](https://github.com/lenneTech/nuxt-base/commit/0de5029b15966abc1f4b45b2d40fc31ba7f63dd4))
* update packages ([82299c2](https://github.com/lenneTech/nuxt-base/commit/82299c22386ee79379aa3e3c820617d1aa67c5bc))


### Bug Fixes

* fix error in apollo plugin with wss communication ([40c68e3](https://github.com/lenneTech/nuxt-base/commit/40c68e316b25f99c86a16ef24ff2f4e09dbe7b6f))

## [3.8.0](https://github.com/lenneTech/nuxt-base/compare/v3.7.0...v3.8.0) (2024-04-11)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lenne.tech/nuxt-base",
"version": "3.8.1-dev.0",
"version": "3.9.1-dev.20",
"description": "Nuxt base module of @lenne.tech",
"repository": "https://github.com/lenneTech/nuxt-base",
"license": "MIT",
Expand Down
113 changes: 37 additions & 76 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { generate } from '@graphql-codegen/cli';
import type { Types } from '@graphql-codegen/plugin-helpers';
import type { Import } from 'unimport';

import { generate } from '@graphql-codegen/cli';
import { addTemplate } from '@nuxt/kit';
import { buildClientSchema, getIntrospectionQuery } from 'graphql';
import { ofetch } from 'ofetch';
import type { Import } from 'unimport';

import { GraphQLMeta } from './runtime/classes/graphql-meta.class';
import { addTemplate } from '@nuxt/kit';
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const { loadSchema } = require('@graphql-tools/load');
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const { GraphQLFileLoader } = require('@graphql-tools/graphql-file-loader');

export async function loadMetaServer(
config: Partial<{ public: { host: string; schema?: string } }>,
): Promise<GraphQLMeta> {
export async function loadMetaServer(config: Partial<{ public: { host: string; schema?: string } }>): Promise<GraphQLMeta> {
let schema;

if (!config.public.schema) {
const { data: result } = await ofetch(config.public.host, {
method: 'POST',
body: JSON.stringify({
query: getIntrospectionQuery({ descriptions: false }),
variables: {},
}),
method: 'POST',
});

schema = buildClientSchema(result);
Expand All @@ -36,35 +36,35 @@ export async function loadMetaServer(

export default async function generateGraphQLTypes(schema: string) {
const config: Types.Config = {
schema,
ignoreNoDocuments: true,
config: {
skipTypename: true,
declarationKind: 'interface',
maybeValue: 'T',
namingConvention: {
enumValues: 'change-case-all#upperCase',
},
maybeValue: 'T',
useTypeImports: true,
declarationKind: 'interface',
scalars: {},
primitives: {
ID: 'string',
Any: 'any',
String: 'string',
Float: 'number',
Int: 'number',
Boolean: 'boolean',
DateTime: 'Date',
Date: 'Date',
DateTime: 'Date',
Float: 'number',
ID: 'string',
Int: 'number',
JSON: '{ [key: string]: any }',
String: 'string',
Upload: 'File',
},
scalars: {},
skipTypename: true,
useTypeImports: true,
},
generates: {
[process.cwd() + '/src/types/schema.d.ts']: {
plugins: ['typescript'],
},
},
ignoreNoDocuments: true,
schema,
};

return await generate(config, false);
Expand All @@ -74,18 +74,10 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
const methods = meta.getMethodNames();
const template = [];
let customTypes = [];
template.push(
'import type { InputFields } from \'#base-types/fields\';\n',
);
template.push(
'import { gqlQuery, gqlMutation, gqlSubscription } from \'#imports\';\n',
);
template.push(
'import { UseMutationReturn, UseSubscriptionReturn } from \'@vue/apollo-composable\';\n',
);
template.push(
'import type { AsyncData } from \'nuxt/app\';',
);
template.push('import type { InputFields } from \'#base-types/fields\';\n');
template.push('import { gqlQuery, gqlMutation, gqlSubscription } from \'#imports\';\n');
template.push('import { UseMutationReturn, UseSubscriptionReturn } from \'@vue/apollo-composable\';\n');
template.push('import type { AsyncData } from \'nuxt/app\';');

if (methods?.query) {
for (const query of methods.query) {
Expand All @@ -98,9 +90,7 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
types.argType ? 'variables: { ' + types.argType + ' },' : ''
} fields?: InputFields<${inputFieldsType}>[] | null, lazy?: boolean, log?: boolean): Promise<AsyncData<{${query}: ${
types.returnType
}}, any>> => gqlQuery<{${query}: ${types.returnType}}>('${query}', {${
types.argType ? 'variables,' : ''
} fields, lazy, log})`,
}}, any>> => gqlQuery<{${query}: ${types.returnType}}>('${query}', {${types.argType ? 'variables,' : ''} fields, lazy, log})`,
);
}
}
Expand All @@ -113,13 +103,9 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
template.push(
`export const use${capitalizeFirstLetter(mutation)}Mutation = (${
types.argType ? 'variables: { ' + types.argType + ' },' : ''
} fields?: InputFields<${inputFieldsType}>[] | null, log?: boolean): Promise<UseMutationReturn<{${mutation}: ${
} fields?: InputFields<${inputFieldsType}>[] | null, log?: boolean): Promise<UseMutationReturn<{${mutation}: ${types.returnType}}, any>> => gqlMutation<{${mutation}: ${
types.returnType
}}, any>> => gqlMutation<{${mutation}: ${
types.returnType
}}>('${mutation}', {${
types.argType ? 'variables,' : ''
} fields, log})`,
}}>('${mutation}', {${types.argType ? 'variables,' : ''} fields, log})`,
);
}
}
Expand All @@ -131,17 +117,11 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
const inputFieldsType = types.returnType.replace('[]', '');

template.push(
`export const use${capitalizeFirstLetter(
subscription,
)}Subscription = (${
`export const use${capitalizeFirstLetter(subscription)}Subscription = (${
types.argType ? 'variables: { ' + types.argType + ' },' : ''
} fields?: InputFields<${inputFieldsType}>[] | null, log?: boolean): Promise<UseSubscriptionReturn<{${subscription}: ${
types.returnType
}}, any>> => gqlSubscription<{${subscription}: ${
types.returnType
}}>('${subscription}', {${
types.argType ? 'variables,' : ''
} fields, log})`,
}}, any>> => gqlSubscription<{${subscription}: ${types.returnType}}>('${subscription}', {${types.argType ? 'variables,' : ''} fields, log})`,
);
}
}
Expand Down Expand Up @@ -180,15 +160,9 @@ export async function getAllImports(meta: GraphQLMeta) {
customTypes = [...new Set([].concat(...customTypes))];

return [
...customTypes.map(
(type): Import => ({ from: '#base/default', name: type }),
),
...methods.query.map(
(fn): Import => ({ from: '#base', name: getMethodName(fn, 'Query') }),
),
...methods.mutation.map(
(fn): Import => ({ from: '#base', name: getMethodName(fn, 'Mutation') }),
),
...customTypes.map((type): Import => ({ from: '#base/default', name: type })),
...methods.query.map((fn): Import => ({ from: '#base', name: getMethodName(fn, 'Query') })),
...methods.mutation.map((fn): Import => ({ from: '#base', name: getMethodName(fn, 'Mutation') })),
...methods.subscription.map(
(fn): Import => ({
from: '#base',
Expand All @@ -211,22 +185,20 @@ export async function generateFiles(options: any, logger: any, nuxt: any, resolv
const meta = await loadMetaServer({ public: options });

// Generate graphql types
const generatedTypes = await generateGraphQLTypes(
options.schema ?? options.host,
);
const generatedTypes = await generateGraphQLTypes(options.schema ?? options.host);
addTemplate({
write: true,
filename: nuxt.options.rootDir + '/src/base/default.ts',
getContents: () => generatedTypes[0].content || '',
write: true,
});
logger.success('[@lenne.tech/nuxt-base] Generated base/default.ts');

// Generate composable types
const composables = await generateComposables(meta);
addTemplate({
write: true,
filename: nuxt.options.rootDir + '/src/base/index.ts',
getContents: () => composables || '',
write: true,
});
logger.success('[@lenne.tech/nuxt-base] Generated base/index.ts');

Expand All @@ -237,23 +209,12 @@ export async function generateFiles(options: any, logger: any, nuxt: any, resolv
imports.push(...(methods || []));
});

nuxt.options.alias['#base'] = resolver.resolve(
nuxt.options.rootDir,
'src',
'base',
);
nuxt.options.alias['#base'] = resolver.resolve(nuxt.options.rootDir, 'src', 'base');

nuxt.options.alias['#base/*'] = resolver.resolve(
nuxt.options.rootDir,
'src',
'base',
'*',
);
nuxt.options.alias['#base/*'] = resolver.resolve(nuxt.options.rootDir, 'src', 'base', '*');
}
} catch (e) {
console.error(e);
logger.warn(
'[@lenne.tech/nuxt-base] Generated failed. Please check your host.',
);
logger.warn('[@lenne.tech/nuxt-base] Generated failed. Please check your host.');
}
}
Loading

0 comments on commit 37e2ab7

Please sign in to comment.