Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.DiscountBreakdown
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.DiscountBreakdown
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.PropertyBooking
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.PropertyBooking
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.PropertyBooking
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.PropertyBooking
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Agoda.Graphql;
using Agoda.Graphql.BookingQueries;

namespace Agoda.Graphql.BookingQueries.PropertyBooking
{
Expand Down
77 changes: 0 additions & 77 deletions codegen.yml

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "agoda-graphql-csharp-generator",
"version": "2.1.5",
"main": "dist/agoda-csharp-codegen.js",
"version": "2.1.6",
"main": "dist/agoda-csharp-operation.js",
"exports": {
"./shared-types": "./dist/agoda-csharp-shared-types.js",
"./operation": "./dist/agoda-csharp-operation.js"
Expand Down
80 changes: 7 additions & 73 deletions src/agoda-csharp-operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
visit,
Kind,
visit,
isNonNullType,
isListType,
getNamedType,
Expand All @@ -15,14 +14,8 @@ import {
TypeNode
} from 'graphql';
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers'

// Constants
const SCALAR_TYPES = ['String', 'Int', 'Float', 'Boolean', 'ID', 'Date', 'DateTime', 'LocalDate', 'LocalDateTime'];

// Helper function to check if a type is a scalar
const isScalarType = (typeName: string): boolean => {
return SCALAR_TYPES.includes(typeName);
};
import { isScalarType } from './graphqlUtils';
import { mapGraphQLTypeToCSharp } from './naming';

// Helper function to check if a type is an enum
const isEnumTypeFromSchema = (schema: GraphQLSchema, typeName: string): boolean => {
Expand Down Expand Up @@ -114,24 +107,7 @@ const convertGraphQLTypeToCSharp = (input: GraphQLType | TypeNode, schema: Graph
const typeName = getNamedType(currentType).name;

// Map GraphQL scalar types to C# types
let csharpType;
switch (typeName) {
case 'String': csharpType = 'string'; break;
case 'Int': csharpType = 'int'; break;
case 'BigInt': csharpType = 'long'; break;
case 'BigDecimal': csharpType = 'decimal'; break;
case 'Long': csharpType = 'long'; break;
case 'Float': csharpType = 'double'; break;
case 'Boolean': csharpType = 'bool'; break;
case 'Date': csharpType = 'DateTime'; break;
case 'DateTime': csharpType = 'DateTime'; break;
case 'ID': csharpType = 'string'; break;
case 'LocalDate': csharpType = 'DateTime'; break;
case 'LocalDateTime': csharpType = 'DateTime'; break;
default:
csharpType = toPascalCase(typeName);
break;
}
let csharpType = mapGraphQLTypeToCSharp(typeName);

// Handle nullability and lists
if (isList) {
Expand Down Expand Up @@ -187,39 +163,7 @@ const getFieldTypeFromSchema = (parentType: GraphQLType | null, fieldName: strin
}
};

// Helper function to extract type name from TypeNode
const extractTypeName = (typeNode: TypeNode): string => {
if (typeNode.kind === Kind.NAMED_TYPE) {
return typeNode.name.value;
} else if (typeNode.kind === Kind.NON_NULL_TYPE) {
return extractTypeName(typeNode.type);
} else if (typeNode.kind === Kind.LIST_TYPE) {
return extractTypeName(typeNode.type);
}
return 'Unknown';
};

// Function to generate C# enum from GraphQL enum
const generateEnumFromGraphQLType = (schema: GraphQLSchema, typeName: string): string => {
try {
const enumType = schema.getType(typeName);
if (!enumType || !('getValues' in enumType)) {
return '';
}

const enumValues = (enumType as any).getValues();
const enumValueNames = enumValues.map((value: any) => value.name).join(',\n ');

return `
public enum ${toPascalCase(typeName)}
{
${enumValueNames}
}`;
} catch (error) {
console.log(`Error generating enum for ${typeName}:`, error);
return '';
}
};
// Note: Enum generation is handled by the shared types plugin

// Unified function to generate C# class from GraphQL type
const generateClassFromGraphQLType = (
Expand All @@ -242,12 +186,9 @@ const generateClassFromGraphQLType = (
return result;
}

// Handle enum types
// Handle enum types - now handled by shared types plugin
if ('getValues' in graphqlType) {
const enumDefinition = generateEnumFromGraphQLType(schema, typeName);
if (enumDefinition) {
result.push(enumDefinition);
}
// Enums are generated by the shared types plugin
return result;
}

Expand Down Expand Up @@ -306,13 +247,6 @@ const generateClassFromGraphQLType = (
if (!isScalarType(namedTypeName) && !isEnumTypeFromSchema(schema, namedTypeName) && !processedTypes.has(namedTypeName)) {
const nestedTypes = generateClassFromGraphQLType(schema, namedTypeName, processedTypes, isInputType);
result.push(...nestedTypes);
} else if (isEnumTypeFromSchema(schema, namedTypeName) && !processedTypes.has(namedTypeName)) {
// Generate enum types that are referenced
const enumDefinition = generateEnumFromGraphQLType(schema, namedTypeName);
if (enumDefinition) {
result.push(enumDefinition);
processedTypes.add(namedTypeName);
}
}

properties.push(` [JsonProperty("${fieldName}")]
Expand Down
48 changes: 5 additions & 43 deletions src/agoda-csharp-shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,10 @@ import {
TypeNode
} from 'graphql';
import { PluginFunction, Types } from '@graphql-codegen/plugin-helpers'
import { isScalarType } from './graphqlUtils';
import { mapGraphQLTypeToCSharp, toPascalCase } from './naming';
import { extractTypeName } from './graphqlUtils';

// Constants
const SCALAR_TYPES = ['String', 'Int', 'Float', 'Boolean', 'ID', 'Date', 'DateTime', 'LocalDate', 'LocalDateTime', 'BigInt', 'BigDecimal', 'Long'];

// Helper function to check if a type is a scalar
const isScalarType = (typeName: string): boolean => {
return SCALAR_TYPES.includes(typeName);
};

// Helper function to check if a type is an enum
const isEnumTypeFromSchema = (schema: GraphQLSchema, typeName: string): boolean => {
Expand All @@ -39,11 +35,6 @@ interface AgodaCSharpSharedConfig {
namespace?: string;
}

const toPascalCase = (str: string): string => {
if (!str || typeof str !== 'string') return '';
return str.charAt(0).toUpperCase() + str.slice(1);
};

const convertGraphQLTypeToCSharp = (input: GraphQLType | TypeNode, schema: GraphQLSchema | null = null): string => {
let schemaType: GraphQLType;

Expand Down Expand Up @@ -92,24 +83,7 @@ const convertGraphQLTypeToCSharp = (input: GraphQLType | TypeNode, schema: Graph
const typeName = getNamedType(currentType).name;

// Map GraphQL scalar types to C# types
let csharpType;
switch (typeName) {
case 'String': csharpType = 'string'; break;
case 'Int': csharpType = 'int'; break;
case 'BigInt': csharpType = 'long'; break;
case 'BigDecimal': csharpType = 'decimal'; break;
case 'Long': csharpType = 'long'; break;
case 'Float': csharpType = 'double'; break;
case 'Boolean': csharpType = 'bool'; break;
case 'Date': csharpType = 'DateTime'; break;
case 'DateTime': csharpType = 'DateTime'; break;
case 'ID': csharpType = 'string'; break;
case 'LocalDate': csharpType = 'DateTime'; break;
case 'LocalDateTime': csharpType = 'DateTime'; break;
default:
csharpType = toPascalCase(typeName);
break;
}
let csharpType = mapGraphQLTypeToCSharp(typeName);

// Handle nullability and lists
if (isList) {
Expand All @@ -123,18 +97,6 @@ const convertGraphQLTypeToCSharp = (input: GraphQLType | TypeNode, schema: Graph
return csharpType;
};

// Helper function to extract type name from TypeNode
const extractTypeName = (typeNode: TypeNode): string => {
if (typeNode.kind === 'NamedType') {
return typeNode.name.value;
} else if (typeNode.kind === 'NonNullType') {
return extractTypeName(typeNode.type);
} else if (typeNode.kind === 'ListType') {
return extractTypeName(typeNode.type);
}
return 'Unknown';
};

// Function to generate C# enum from GraphQL enum
const generateEnumFromGraphQLType = (schema: GraphQLSchema, typeName: string): string => {
try {
Expand Down Expand Up @@ -317,4 +279,4 @@ namespace ${config.namespace}
${allGeneratedTypes.join('\n\n')}
}
`;
};
};
14 changes: 14 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const SCALAR_TYPES = [
'String',
'Int',
'Float',
'Boolean',
'ID',
'Date',
'DateTime',
'LocalDate',
'LocalDateTime',
'BigInt',
'BigDecimal',
'Long'
];
Loading