Skip to content

Commit 79e9d4e

Browse files
author
naman-contentstack
committed
added fix for regions in graph ql
1 parent 2c1ea67 commit 79e9d4e

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/commands/tsgen.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
2929
'$ csdx tsgen -a "delivery token alias" -o "contentstack/generated.d.ts" --api-type graphql --namespace "GraphQL" ',
3030
];
3131

32+
// Check if a region is a default Contentstack region
33+
private isDefaultRegion(region: string): boolean {
34+
const defaultRegions = [
35+
"US",
36+
"EU",
37+
"AZURE_NA",
38+
"AZURE_EU",
39+
"GCP_NA",
40+
"GCP_EU",
41+
];
42+
return defaultRegions.includes(region.toUpperCase());
43+
}
44+
3245
static flags: FlagInput = {
3346
"token-alias": flags.string({
3447
char: "a",
@@ -127,7 +140,37 @@ export default class TypeScriptCodeGeneratorCommand extends Command {
127140
if (config.region === "us") {
128141
config.region = "US";
129142
}
130-
const result = await graphqlTS({ ...config, namespace: namespace });
143+
144+
// Check if token has delivery type (required for GraphQL)
145+
if (token.type !== "delivery") {
146+
throw new Error(
147+
"GraphQL API requires a delivery token. Management tokens are not supported for GraphQL operations.",
148+
);
149+
}
150+
151+
// Prepare GraphQL config - only include host for custom regions
152+
const graphqlConfig: any = {
153+
apiKey: config.apiKey,
154+
token: config.token,
155+
environment: config.environment,
156+
namespace: namespace,
157+
};
158+
159+
// Add region or host based on whether it's a custom region
160+
if (config.host && !this.isDefaultRegion(config.region)) {
161+
// Custom region - include both region and host
162+
graphqlConfig.region = config.region;
163+
graphqlConfig.host = config.host;
164+
} else {
165+
// Default region - only include region
166+
graphqlConfig.region = config.region;
167+
}
168+
169+
const result = await graphqlTS(graphqlConfig);
170+
171+
if (!result) {
172+
throw new Error("GraphQL API returned no result");
173+
}
131174

132175
fs.writeFileSync(outputPath, result);
133176
this.log(

tests/integration/tsgen.integration.test.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ describe("Integration Test for tsgen command", () => {
130130
];
131131

132132
const result = spawnSync(cmd, args, { encoding: "utf-8" });
133-
console.error(result);
134133
expect(result.status).toBe(0);
135134
expect(fs.existsSync(outputFilePath)).toBeTruthy();
136135

@@ -155,14 +154,7 @@ describe("Integration Test for tsgen command", () => {
155154
namespace,
156155
];
157156

158-
console.log("Running GraphQL namespace test with args:", args);
159157
const result = spawnSync(cmd, args, { encoding: "utf-8" });
160-
console.error("GraphQL Namespace Test Result:", {
161-
status: result.status,
162-
stdout: result.stdout,
163-
stderr: result.stderr,
164-
error: result.error,
165-
});
166158

167159
expect(result.status).toBe(0);
168160
expect(fs.existsSync(outputFilePath)).toBeTruthy();

0 commit comments

Comments
 (0)