Skip to content

Commit 1810cfc

Browse files
Copilothotlong
andcommitted
Fix code review issues in JSON scalar and capitalize
- Extract parseLiteral as standalone function to fix recursive calls - Add filter to capitalize function to handle empty segments - Improve null handling in parseLiteral Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent a030834 commit 1810cfc

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

packages/protocols/graphql/src/index.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,33 @@ export class GraphQLPlugin implements RuntimePlugin {
979979
private generateResolvers(): any {
980980
const objectTypes = this.getMetaTypes();
981981

982+
// Helper function for parsing GraphQL literals to JSON
983+
const parseLiteral = (ast: any): any => {
984+
if (ast.kind === 'StringValue') {
985+
return JSON.parse(ast.value);
986+
}
987+
if (ast.kind === 'IntValue' || ast.kind === 'FloatValue') {
988+
return parseFloat(ast.value);
989+
}
990+
if (ast.kind === 'BooleanValue') {
991+
return ast.value;
992+
}
993+
if (ast.kind === 'NullValue') {
994+
return null;
995+
}
996+
if (ast.kind === 'ObjectValue') {
997+
const obj: any = {};
998+
ast.fields.forEach((field: any) => {
999+
obj[field.name.value] = parseLiteral(field.value);
1000+
});
1001+
return obj;
1002+
}
1003+
if (ast.kind === 'ListValue') {
1004+
return ast.values.map((value: any) => parseLiteral(value));
1005+
}
1006+
return null;
1007+
};
1008+
9821009
const resolvers: any = {
9831010
// JSON scalar resolver - passes through any value
9841011
JSON: {
@@ -989,27 +1016,7 @@ export class GraphQLPlugin implements RuntimePlugin {
9891016
return value; // value sent to the client
9901017
},
9911018
__parseLiteral(ast: any) {
992-
// Parse GraphQL query literals (not used for variables)
993-
if (ast.kind === 'StringValue') {
994-
return JSON.parse(ast.value);
995-
}
996-
if (ast.kind === 'IntValue' || ast.kind === 'FloatValue') {
997-
return parseFloat(ast.value);
998-
}
999-
if (ast.kind === 'BooleanValue' || ast.kind === 'NullValue') {
1000-
return ast.value;
1001-
}
1002-
if (ast.kind === 'ObjectValue') {
1003-
const obj: any = {};
1004-
ast.fields.forEach((field: any) => {
1005-
obj[field.name.value] = this.__parseLiteral(field.value);
1006-
});
1007-
return obj;
1008-
}
1009-
if (ast.kind === 'ListValue') {
1010-
return ast.values.map((value: any) => this.__parseLiteral(value));
1011-
}
1012-
return null;
1019+
return parseLiteral(ast);
10131020
}
10141021
},
10151022
Query: {

packages/protocols/graphql/src/tck.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ class GraphQLEndpoint implements ProtocolEndpoint {
323323
// Convert to PascalCase (handle underscores and hyphens)
324324
return str
325325
.split(/[_-]/)
326+
.filter(word => word.length > 0) // Remove empty segments
326327
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
327328
.join('');
328329
}

0 commit comments

Comments
 (0)