Skip to content

Commit

Permalink
feat(cli): create graphql schema using gql.tada
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvolk committed Mar 14, 2024
1 parent 604f60a commit 635040d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
56 changes: 1 addition & 55 deletions apps/core/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,7 @@
import { CodegenConfig } from '@graphql-codegen/cli';

export const graphqlApiDomain: string =
process.env.BIGCOMMERCE_GRAPHQL_API_DOMAIN ?? 'mybigcommerce.com';

const getToken = () => {
const token = process.env.BIGCOMMERCE_CUSTOMER_IMPERSONATION_TOKEN;

if (!token) {
throw new Error('Missing customer impersonation token');
}

return token;
};

const getStoreHash = () => {
const storeHash = process.env.BIGCOMMERCE_STORE_HASH;

if (!storeHash) {
throw new Error('Missing store hash');
}

return storeHash;
};

const getChannelId = () => {
const channelId = process.env.BIGCOMMERCE_CHANNEL_ID;

return channelId;
};

const getEndpoint = () => {
const storeHash = getStoreHash();
const channelId = getChannelId();

// Not all sites have the channel-specific canonical URL backfilled.
// Wait till MSF-2643 is resolved before removing and simplifying the endpoint logic.
if (!channelId || channelId === '1') {
return `https://store-${storeHash}.${graphqlApiDomain}/graphql`;
}

return `https://store-${storeHash}-${channelId}.${graphqlApiDomain}/graphql`;
};

const config: CodegenConfig = {
schema: [
{
[getEndpoint()]: {
headers: {
Authorization: `Bearer ${getToken()}`,
},
},
},
],
schema: './schema.graphql',
documents: ['client/queries/**/*.ts', 'client/mutations/**/*.ts', 'client/fragments/**/*.ts'],
generates: {
'./client/generated/': {
Expand All @@ -71,10 +21,6 @@ const config: CodegenConfig = {
},
},
},
'./schema.graphql': {
plugins: ['schema-ast'],
watchPattern: '',
},
},
};

Expand Down
2 changes: 2 additions & 0 deletions apps/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev-next": "next dev",
"dev-codegen": "dotenv -e .env.local -- npm run codegen -- -w",
"codegen": "dotenv -e .env.local -- gql-gen --errors-only --config codegen.ts",
"generate": "dotenv -e .env.local -- node ./scripts/generate.js",
"build": "npm run codegen && next build",
"start": "next start",
"lint": "next lint",
Expand Down Expand Up @@ -44,6 +45,7 @@
"devDependencies": {
"@0no-co/graphqlsp": "^1.5.0",
"@bigcommerce/eslint-config-catalyst": "workspace:^",
"@gql.tada/cli-utils": "^0.1.2",
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/client-preset": "^4.1.0",
"@graphql-codegen/schema-ast": "^4.0.0",
Expand Down
23 changes: 23 additions & 0 deletions apps/core/scripts/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Debugging
console.log('process.cwd()', process.cwd());

const { generateSchema, generateTadaTypes } = require('@gql.tada/cli-utils');

const storeHash = process.env.BIGCOMMERCE_STORE_HASH;
const channelId = process.env.BIGCOMMERCE_CHANNEL_ID;
const customerImpersonationToken = process.env.BIGCOMMERCE_CUSTOMER_IMPERSONATION_TOKEN;

const graphqlApiDomain = process.env.BIGCOMMERCE_GRAPHQL_API_DOMAIN ?? 'mybigcommerce.com';

const generate = async () => {
await generateSchema(
`https://store-${storeHash}${
channelId === 1 ? '' : `-${channelId}`
}.${graphqlApiDomain}/graphql`,
{ headers: { Authorization: `Bearer ${customerImpersonationToken}` } },
);

await generateTadaTypes();
};

// generate();
6 changes: 6 additions & 0 deletions packages/create-catalyst/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ export const create = async (options: CreateCommandOptions) => {

await installDependencies(projectDir, packageManager);

await spinner(exec(`${packageManager} run generate`, { cwd: projectDir }), {
text: 'Creating GraphQL schema...',
successText: 'Created GraphQL schema',
failText: (err) => chalk.red(`Failed to create GraphQL schema: ${err.message}`),
});

await spinner(exec(`${packageManager} run codegen`, { cwd: projectDir }), {
text: 'Generating GraphQL types...',
successText: 'GraphQL types generated successfully',
Expand Down
22 changes: 15 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 635040d

Please sign in to comment.