Skip to content

Commit

Permalink
Update GraphQL types package setup to include all types available (#722)
Browse files Browse the repository at this point in the history
* refactor(graphql-types): export types for all services

* refactor(graphql-types): export types for all services

* refactor(graphql-types): exporting different entry points

* refactor(graphql-types): add post-processing step

* refactor: update models graphql types

* refactor(graphql-types): remove support for different entry points

* refactor(graphql-types): remove support for different entry points

* refactor(generators): update generator to invlude graphql types package

* refactor(graphql-types): apply regexp security suggestion

* refactor(commons): adjust graphql types

* refactor(graphql-types): apply review feedback

* refactor(graphql-types): removed unnecessary dependencies
  • Loading branch information
CarlosCortizasCT authored Dec 5, 2024
1 parent 3aff52c commit 25e7de7
Show file tree
Hide file tree
Showing 40 changed files with 172,023 additions and 897 deletions.
2 changes: 2 additions & 0 deletions codegen.core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ extensions:
config:
typesPrefix: TCore
addUnderscoreToArgsType: true
useImplementingTypes: true
hooks:
afterAllFileWrite:
- prettier --write
- node ./graphql-types/src/types-post-processor.mjs
24 changes: 24 additions & 0 deletions codegen.ctp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
schema:
- '${MC_API_URL}/graphql':
headers:
Cookie: mcAccessToken=${MC_ACCESS_TOKEN}
X-Graphql-Target: ctp
X-Project-Key: ${CTP_PROJECT_KEY}
extensions:
codegen:
generates:
schemas/ctp.json:
plugins:
- introspection
graphql-types/src/generated/ctp.ts:
plugins:
- typescript
- typescript-operations
config:
typesPrefix: TCtp
addUnderscoreToArgsType: true
useImplementingTypes: true
hooks:
afterAllFileWrite:
- prettier --write
- node ./graphql-types/src/types-post-processor.mjs
2 changes: 2 additions & 0 deletions codegen.mc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ extensions:
config:
typesPrefix: TMc
addUnderscoreToArgsType: true
useImplementingTypes: true
hooks:
afterAllFileWrite:
- prettier --write
- node ./graphql-types/src/types-post-processor.mjs
2 changes: 2 additions & 0 deletions codegen.settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ extensions:
config:
typesPrefix: TMcSettings
addUnderscoreToArgsType: true
useImplementingTypes: true
scalars:
DateTime: string
Json: '{ [key: string]: unknown }'
hooks:
afterAllFileWrite:
- prettier --write
- node ./graphql-types/src/types-post-processor.mjs
35 changes: 34 additions & 1 deletion generators/src/new-test-model/new-test-model.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { render as renderTemplate } from 'squirrelly';
import { CodeGenerator } from '../types';
import { packageTemplatesData, modelTemplatesData } from './templates';

const servicesToTypePrefixMap = {
core: 'TCore',
ctp: 'TCtp',
mc: 'TMc',
settings: 'TMcSettings',
} as const;

function ensureDirectory(filePath: string) {
const dirPath = dirname(filePath);
if (!existsSync(dirPath)) {
Expand All @@ -24,6 +31,22 @@ export const newTestModelGenerator: CodeGenerator = {
});
const modelCodename = snakeCase(modelName).replaceAll('_', '-');

const { modelOwningService } = await prompts({
type: 'select',
name: 'modelOwningService',
message: 'What is the name of the service that owns this model',
choices: [
{ title: 'Core (Organization related models)', value: 'core' },
{ title: 'CTP (Project related models)', value: 'ctp' },
{ title: 'MC Gateway', value: 'mc' },
{ title: 'MC Settings', value: 'settings' },
],
});
const graphqlTypePrefix =
servicesToTypePrefixMap[
modelOwningService as keyof typeof servicesToTypePrefixMap
];

const { generationType } = await prompts({
type: 'select',
name: 'generationType',
Expand Down Expand Up @@ -54,8 +77,18 @@ export const newTestModelGenerator: CodeGenerator = {
);
}

// Get the current version of the packages in this repo
const corePackageJson = await import(
join(__dirname, '..', '..', '..', 'core', 'package.json')
);

// 2. Generate the files
const templatesData = { modelName, modelCodename };
const templatesData = {
modelName,
modelCodename,
graphqlTypePrefix,
packageVersion: corePackageJson.version,
};

console.log(''); // a simple line break
if (generationType === 'standalone') {
Expand Down
16 changes: 7 additions & 9 deletions generators/src/new-test-model/templates/model/types.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ import {
{{it.modelName}},
} from '@commercetools/platform-sdk';
import type { TBuilder } from '@commercetools-test-data/core';
import {
{{it.graphqlTypePrefix}}{{it.modelName}},
} from '@commercetools-test-data/graphql-types';


export type T{{it.modelName}}Rest = {{it.modelName}};

// TODO: Check wether the graphql version has different properties
// or types than the rest version.
// You might want to use the GraphQL Explorer for that
// (https://<merchant_center_domain>/<project_key>/graphql-explorer-commercetools/platform)
export type T{{it.modelName}}Graphql = {{it.modelName}} & {
__typename: '{{it.modelName}}';
};
export type T{{it.modelName}}Graphql = {{it.graphqlTypePrefix}}{{it.modelName}};

// TODO: Implement draft models if needed
// Otherwise remove this code
// export type T{{it.modelName}}DraftRest = OrderDraft;
// export type T{{it.modelName}}DraftGraphql = OrderDraft;
// export type T{{it.modelName}}DraftRest = {{it.modelName}}Draft;
// export type T{{it.modelName}}DraftGraphql = {{it.graphqlTypePrefix}}{{it.modelName}}Draft;

export type TCreate{{it.modelName}}Builder<
TModel extends
Expand Down
9 changes: 5 additions & 4 deletions generators/src/new-test-model/templates/package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@commercetools-test-data/{{it.modelCodename}}",
"version": "10.1.3",
"version": "{{it.packageVersion}}",
"description": "Data model for commercetools API {{it.modelName}}",
"bugs": "https://github.com/commercetools/test-data/issues",
"repository": {
Expand All @@ -19,9 +19,10 @@
"dependencies": {
"@babel/runtime": "^7.17.9",
"@babel/runtime-corejs3": "^7.17.9",
"@commercetools-test-data/commons": "10.1.3",
"@commercetools-test-data/core": "10.1.3",
"@commercetools-test-data/utils": "10.1.3",
"@commercetools-test-data/commons": "{{it.packageVersion}}",
"@commercetools-test-data/core": "{{it.packageVersion}}",
"@commercetools-test-data/graphql-types": "{{it.packageVersion}}",
"@commercetools-test-data/utils": "{{it.packageVersion}}",
"@commercetools/platform-sdk": "^7.0.0",
"@faker-js/faker": "^8.0.0"
}
Expand Down
Loading

0 comments on commit 25e7de7

Please sign in to comment.