Skip to content

Commit 0909153

Browse files
taeyzzzThanetpon Kultontikorn
andauthored
recheck script generate config (#23)
* recheck script generate config * update readme and config * update --------- Co-authored-by: Thanetpon Kultontikorn <[email protected]>
1 parent 7cc1d68 commit 0909153

File tree

7 files changed

+54
-47
lines changed

7 files changed

+54
-47
lines changed

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,14 @@ npm install agoda-graphql-csharp-generator @graphql-codegen/cli graphql
88

99
## how to use
1010
- create codegen config -> https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config
11-
- use plugin -> `agoda-graphql-csharp-generator`
11+
- generated codegen config
12+
```
13+
generate-codegen-config --schema-url 'https://graphql-schema.com' --graphql-dir './Agoda.Graphql/SupplyApi/Queries' --graphql-project 'Agoda.Graphql' --yml-out './codegen.yml'
14+
```
15+
- it will generated codegen.yml in this file will be all config
16+
- then use `gql-gen --config codegen.yml` to generated graphql files
1217

1318
# Notes:
1419
- automate job to update semver version
1520
- automate job to create release and tags
1621

17-
18-
Found the Solution:
19-
- create custom plugin to generate share type class from schema
20-
- pass list of graphql files
21-
- create share type and use in each generated file
22-
- create custom plugin to generate query or mutation class using that share type
23-
- fix npm package to export 2 files
24-
- test with BAPI and SMAPI
25-
- make sure it's working with json converter

codegen.yml

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
overwrite: true
33
schema: "http://booking-query-high-qa.privatecloud.qa.agoda.is"
44
headers:
5-
API-Key: "uTSpgQLr9ZyuMSPCVca70SciyTtwBPaqTCsxSTBmrYDo8Opnp5"
6-
Client-ID: 23
5+
API-Key: "xxx"
6+
Client-ID: "xxx"
77
generates:
88
./Agoda.Graphql/BookingQueries/SharedTypes.generated.cs:
99
documents:
@@ -16,70 +16,62 @@ generates:
1616
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvMetaData.graphql"
1717
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetPropertyBookingByYCSCriteria.graphql"
1818
plugins:
19-
- "./dist/agoda-csharp-shared.js"
19+
- "agoda-graphql-csharp-generator/shared-types"
2020
config:
2121
namespace: "Agoda.Graphql.BookingQueries"
2222
Agoda.Graphql/BookingQueries/DiscountBreakdown/BookingDiscountsByBookingIds.generated.cs:
2323
documents:
2424
- "Agoda.Graphql/BookingQueries/DiscountBreakdown/BookingDiscountsByBookingIds.graphql"
2525
plugins:
26-
- "./dist/agoda-csharp-codegen.js"
26+
- "agoda-graphql-csharp-generator/operation"
2727
config:
2828
namespace: "Agoda.Graphql.BookingQueries.DiscountBreakdown"
29-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
3029
Agoda.Graphql/BookingQueries/DiscountBreakdown/DiscountBreakdownByBookingId.generated.cs:
3130
documents:
3231
- "Agoda.Graphql/BookingQueries/DiscountBreakdown/DiscountBreakdownByBookingId.graphql"
3332
plugins:
34-
- "agoda-graphql-csharp-generator/codegen"
33+
- "agoda-graphql-csharp-generator/operation"
3534
config:
3635
namespace: "Agoda.Graphql.BookingQueries.DiscountBreakdown"
37-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
3836
Agoda.Graphql/BookingQueries/GetBookingDetailsByBookingId.generated.cs:
3937
documents:
4038
- "Agoda.Graphql/BookingQueries/GetBookingDetailsByBookingId.graphql"
4139
plugins:
42-
- "agoda-graphql-csharp-generator/codegen"
40+
- "agoda-graphql-csharp-generator/operation"
4341
config:
4442
namespace: "Agoda.Graphql.BookingQueries"
45-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
4643
Agoda.Graphql/BookingQueries/PropertyBooking/GetBookingListByBookingIds.generated.cs:
4744
documents:
4845
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetBookingListByBookingIds.graphql"
4946
plugins:
50-
- "agoda-graphql-csharp-generator/codegen"
47+
- "agoda-graphql-csharp-generator/operation"
5148
config:
5249
namespace: "Agoda.Graphql.BookingQueries.PropertyBooking"
53-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
5450
Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvData.generated.cs:
5551
documents:
5652
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvData.graphql"
5753
plugins:
58-
- "agoda-graphql-csharp-generator/codegen"
54+
- "agoda-graphql-csharp-generator/operation"
5955
config:
6056
namespace: "Agoda.Graphql.BookingQueries.PropertyBooking"
61-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
6257
Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvDataWithoutPagination.generated.cs:
6358
documents:
6459
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvDataWithoutPagination.graphql"
6560
plugins:
66-
- "agoda-graphql-csharp-generator/codegen"
61+
- "agoda-graphql-csharp-generator/operation"
6762
config:
6863
namespace: "Agoda.Graphql.BookingQueries.PropertyBooking"
69-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
7064
Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvMetaData.generated.cs:
7165
documents:
7266
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetCsvMetaData.graphql"
7367
plugins:
74-
- "./dist/agoda-csharp-codegen.js"
68+
- "agoda-graphql-csharp-generator/operation"
7569
config:
7670
namespace: "Agoda.Graphql.BookingQueries.PropertyBooking"
77-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"
7871
Agoda.Graphql/BookingQueries/PropertyBooking/GetPropertyBookingByYCSCriteria.generated.cs:
7972
documents:
8073
- "Agoda.Graphql/BookingQueries/PropertyBooking/GetPropertyBookingByYCSCriteria.graphql"
8174
plugins:
82-
- "./dist/agoda-csharp-codegen.js"
75+
- "agoda-graphql-csharp-generator/operation"
8376
config:
8477
namespace: "Agoda.Graphql.BookingQueries.PropertyBooking"
85-
sharedTypesNamespace: "Agoda.Graphql.BookingQueries"

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"name": "agoda-graphql-csharp-generator",
3-
"version": "2.1.4",
3+
"version": "2.1.5",
44
"main": "dist/agoda-csharp-codegen.js",
55
"exports": {
6-
"./shared": "./dist/agoda-csharp-shared.js",
7-
"./codegen": "./dist/agoda-csharp-codegen.js"
6+
"./shared-types": "./dist/agoda-csharp-shared-types.js",
7+
"./operation": "./dist/agoda-csharp-operation.js"
88
},
99
"files": [
1010
"dist"
1111
],
1212
"scripts": {
1313
"generate-codegen-config": "ts-node ./src/generate.ts --schema-url 'https://smapi-qa-http.privatecloud.qa.agoda.is/v2/graphql' --graphql-dir './Agoda.Graphql/SupplyApi/Queries' --graphql-project 'Agoda.Graphql' --yml-out './codegen.yml'",
14-
"generate-codegen-config-with-headers": "ts-node ./src/generate.ts --schema-url 'http://booking-query-high-qa.privatecloud.qa.agoda.is' --graphql-dir './Agoda.Graphql/BookingQueries' --graphql-project 'Agoda.Graphql.BookingQueries' --yml-out './codegen.yml' --header 'API-Key: uTSpgQLr9ZyuMSPCVca70SciyTtwBPaqTCsxSTBmrYDo8Opnp5' --header 'Client-ID: 23'",
14+
"generate-codegen-config-with-headers": "ts-node ./src/generate.ts --schema-url 'http://booking-query-high-qa.privatecloud.qa.agoda.is' --graphql-dir './Agoda.Graphql/BookingQueries' --graphql-project 'Agoda.Graphql.BookingQueries' --yml-out './codegen.yml' --header 'API-Key: xxx' --header 'Client-ID: xxx'",
1515
"generate-graphql-csharp": "gql-gen --config codegen.yml",
1616
"build": "tsc"
1717
},

src/agoda-csharp-codegen.ts renamed to src/agoda-csharp-operation.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const isEnumTypeFromSchema = (schema: GraphQLSchema, typeName: string): boolean
3737
// Plugin configuration interface
3838
interface AgodaCSharpCodegenConfig {
3939
namespace?: string;
40-
sharedTypesNamespace?: string;
4140
}
4241

4342
// Type definitions
@@ -190,11 +189,11 @@ const getFieldTypeFromSchema = (parentType: GraphQLType | null, fieldName: strin
190189

191190
// Helper function to extract type name from TypeNode
192191
const extractTypeName = (typeNode: TypeNode): string => {
193-
if (typeNode.kind === 'NamedType') {
192+
if (typeNode.kind === Kind.NAMED_TYPE) {
194193
return typeNode.name.value;
195-
} else if (typeNode.kind === 'NonNullType') {
194+
} else if (typeNode.kind === Kind.NON_NULL_TYPE) {
196195
return extractTypeName(typeNode.type);
197-
} else if (typeNode.kind === 'ListType') {
196+
} else if (typeNode.kind === Kind.LIST_TYPE) {
198197
return extractTypeName(typeNode.type);
199198
}
200199
return 'Unknown';
@@ -439,7 +438,6 @@ export const plugin: PluginFunction<AgodaCSharpCodegenConfig> = (
439438
}
440439

441440
const operation = operationsDefinitions[0];
442-
const operationName = operation.name;
443441
const variables = operation.variables;
444442
const rawQuery = (operation.document.rawSDL || '').replace(/"/g, '""');
445443

@@ -537,7 +535,6 @@ using System.Collections.Generic;
537535
using Newtonsoft.Json;
538536
using Newtonsoft.Json.Converters;
539537
using Agoda.Graphql;
540-
using ${config.sharedTypesNamespace};
541538
542539
namespace ${namespace}
543540
{
File renamed without changes.

src/generate.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,35 @@ schema: "${schemaUrl}"
6969

7070
// Generate shared types section first
7171
// Use the graphqlDir to determine both the output path and namespace
72-
// Clean up the path by removing leading ./ and converting separators to dots
73-
const cleanGraphqlDir = graphqlDir.replace(/^\.\//, '').replace(/[\/\\]/g, '.')
74-
const sharedTypesNamespace = cleanGraphqlDir
72+
73+
// For output path, use the original graphqlDir
7574
const sharedTypesOutputFile = `${graphqlDir}/SharedTypes.generated.cs`
7675
const normalizedSharedTypesOutputFile = sharedTypesOutputFile.replace(/\\/g, '/')
7776

77+
// For namespace, clean up the path and handle project name matching
78+
const cleanGraphqlDir = graphqlDir.replace(/^\.\//, '').replace(/[\/\\]/g, '.')
79+
let sharedTypesNamespace: string
80+
81+
if (projectName) {
82+
// If project name is provided, find the matching part in the graphqlDir and use it + the rest
83+
const cleanGraphqlDirWithoutPrefix = cleanGraphqlDir.replace(/^(\.\.\/)+/, '').replace(/^\.\//, '')
84+
const projectNameDots = projectName.replace(/[\/\\]/g, '.')
85+
86+
// Find where the project name appears in the clean path
87+
const projectIndex = cleanGraphqlDirWithoutPrefix.indexOf(projectNameDots)
88+
89+
if (projectIndex !== -1) {
90+
// Use the project name + everything after it
91+
sharedTypesNamespace = cleanGraphqlDirWithoutPrefix.substring(projectIndex)
92+
} else {
93+
// If project name not found, use the full cleaned path as namespace
94+
sharedTypesNamespace = cleanGraphqlDirWithoutPrefix
95+
}
96+
} else {
97+
// Remove any relative path parts for namespace - make it generic
98+
sharedTypesNamespace = cleanGraphqlDir.replace(/^(\.\.\/)+/, '').replace(/^\.\//, '')
99+
}
100+
78101
config += ` ${normalizedSharedTypesOutputFile}:
79102
documents:
80103
`
@@ -84,7 +107,7 @@ schema: "${schemaUrl}"
84107
`
85108
}
86109
config += ` plugins:
87-
- "agoda-graphql-csharp-generator/shared"
110+
- "agoda-graphql-csharp-generator/shared-types"
88111
config:
89112
namespace: "${sharedTypesNamespace}"
90113
`
@@ -106,10 +129,9 @@ schema: "${schemaUrl}"
106129
documents:
107130
- "${normalizedGraphqlFile}"
108131
plugins:
109-
- "agoda-graphql-csharp-generator/codegen"
132+
- "agoda-graphql-csharp-generator/operation"
110133
config:
111134
namespace: "${namespace}"
112-
sharedTypesNamespace: "${sharedTypesNamespace}"
113135
`
114136
}
115137

0 commit comments

Comments
 (0)