Skip to content

Commit

Permalink
feat: generate contentful schema and introspection file
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvolk committed Jun 21, 2024
1 parent 889b9ec commit f2f7cff
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Contentful GraphQL Delivery API
CONTENTFUL_SPACE_ID=
CONTENTFUL_ACCESS_TOKEN=

# Makeswift Site API Key
# In the Makeswift builder, go to Settings > Host and copy the API key for the site.
MAKESWIFT_SITE_API_KEY=
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ next-env.d.ts
client/generated
bigcommerce.graphql
bigcommerce-graphql.d.ts
contentful.graphql
contentful-env.d.ts

# secrets
.catalyst
66 changes: 54 additions & 12 deletions scripts/generate.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const { generateSchema, generateOutput } = require('@gql.tada/cli-utils');
const { join } = require('path');

const graphqlApiDomain = process.env.BIGCOMMERCE_GRAPHQL_API_DOMAIN ?? 'mybigcommerce.com';
const contentfulGraphqlApiDomain =
process.env.CONTENTFUL_GRAPHQL_API_DOMAIN ?? 'graphql.contentful.com';
const contentfulEnvironment = process.env.CONTENTFUL_ENVIRONMENT ?? 'master';

const getStoreHash = () => {
const storeHash = process.env.BIGCOMMERCE_STORE_HASH;
Expand Down Expand Up @@ -43,19 +46,58 @@ const getEndpoint = () => {
return `https://store-${storeHash}-${channelId}.${graphqlApiDomain}/graphql`;
};

const getContentfulSpaceId = () => {
const spaceId = process.env.CONTENTFUL_SPACE_ID;

if (!spaceId) {
throw new Error('Missing Contentful space ID');
}

return spaceId;
};

const getContentfulAccessToken = () => {
const accessToken = process.env.CONTENTFUL_ACCESS_TOKEN;

if (!accessToken) {
throw new Error('Missing Contentful access token');
}

return accessToken;
};

const getContentfulEndpoint = () => {
const spaceId = getContentfulSpaceId();

return `https://${contentfulGraphqlApiDomain}/content/v1/spaces/${spaceId}/environments/${contentfulEnvironment}`;
};

const generate = async () => {
await generateSchema({
input: getEndpoint(),
headers: { Authorization: `Bearer ${getToken()}` },
output: join(__dirname, '../bigcommerce.graphql'),
tsconfig: undefined,
});

await generateOutput({
disablePreprocessing: false,
output: undefined,
tsconfig: undefined,
});
try {
await generateSchema({
input: getEndpoint(),
headers: { Authorization: `Bearer ${getToken()}` },
output: join(__dirname, '../bigcommerce.graphql'),
tsconfig: undefined,
});

await generateSchema({
input: getContentfulEndpoint(),
headers: { Authorization: `Bearer ${getContentfulAccessToken()}` },
output: join(__dirname, '../contentful.graphql'),
tsconfig: undefined,
});

await generateOutput({
disablePreprocessing: false,
output: undefined,
tsconfig: undefined,
});
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
}
};

generate();
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"name": "bigcommerce",
"schema": "./bigcommerce.graphql",
"tadaOutputLocation": "./bigcommerce-graphql.d.ts"
},
{
"name": "contentful",
"schema": "./contentful.graphql",
"tadaOutputLocation": "./contentful-env.d.ts"
}
]
}
Expand Down

0 comments on commit f2f7cff

Please sign in to comment.