Skip to content

Commit

Permalink
refactor: move config to root
Browse files Browse the repository at this point in the history
  • Loading branch information
raae committed Jul 28, 2024
1 parent 54e4dec commit 2d98c60
Show file tree
Hide file tree
Showing 15 changed files with 555 additions and 308 deletions.
25 changes: 8 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default BlogPost;

### Transform Type Requirements

Gatsby Image support may be added to any GraphQL Type describing a Cloudinary asset with the following information:
Gatsby Image support may be added to any GraphQL Type describing a Cloudinary asset with the following data:

```js
{
Expand All @@ -138,7 +138,7 @@ Gatsby Image support may be added to any GraphQL Type describing a Cloudinary as
}
```

If the GraphQL Type does not have the required data shape, you may use the `mapping` option to map the data from the sourced data shape to the required data shape:
If the GraphQL Type does not have the required data shape, you may configure how to resolve the data using the `transformTypes` configuration option:

```js
// File: ./gatsby-config.js
Expand All @@ -152,15 +152,10 @@ module.exports = {
transformTypes: [
{
type: `CustomType`,
mapping: {
// Use a static value
cloudName: () => 'my-cloud',
// Use a differnt key than the default
publicId: 'thePublicId', // default for publicId is `public_id`
// Resolve a value using a function
height: (data) => data.dimensions?.height,
width: (data) => data.dimentions?.width,
},
// Use a static value
cloudName: 'my-cloud',
// Resolve a value using a function
height: (data) => data.metadata?.height,
},
],
// Optional transformation option
Expand Down Expand Up @@ -211,12 +206,10 @@ module.exports = {
return result[1];
},
// Or set it statically if all assets are from the same cloud
// cloudName: () => "my-cloud",
// cloudName: "my-cloud",
},
},
],
// Optional transformation option
defaultTransformations: ['c_fill', 'g_auto', 'q_auto'],
},
},
`gatsby-plugin-image`,
Expand Down Expand Up @@ -261,12 +254,10 @@ module.exports = {
return result[1];
},
// Or set it statically if all assets are from the same cloud
// cloudName: () => "my-cloud",
// cloudName: "my-cloud",
},
},
],
// Optional transformation option
defaultTransformations: ['c_fill', 'g_auto', 'q_auto'],
},
},
`gatsby-plugin-image`,
Expand Down
36 changes: 25 additions & 11 deletions demo/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,39 @@ module.exports = {
'BlogPostHeroImage',
'VariedData',
'EmptyDataCloudinary',
// 'MarkdownRemarkFrontmatterHeroImage',
'MarkdownRemarkFrontmatterHeroImage',
{
type: 'MarkdownRemarkFrontmatterHeroImage',
type: 'MarkdownRemarkFrontmatterHeroImageWithUnconformingShape',
cloudName: `lilly-labs-consulting`,
mapping: {
cloudName: `cloudName`,
publicId: `publicId`,
secureDistribution: `example.com`,
secure: () => true,
publicId: 'a_public_id',
},
},
{
type: 'MarkdownRemarkFrontmatterHeroImageWithUnconformingShape',
type: 'SecureDistribution',
secureDistribution: `example.com`,
// secure: true,
publicId: (data) => data.public,
},
{
type: 'Cname',
cname: `example.com`,
secure: false,
format: (data) => data.metadata.format,
mapping: {
cloudName: `a_cloud_name`,
publicId: (data) => {
return data['a_public_id'];
},
width: (data) => data.metadata.width,
height: (data) => data.metadata.height,
},
},
{
type: 'PrivateCDN',
privateCdn: true,
secure: true,
},
{
type: 'PrivateCDNUnsecure',
// Configured in the source date
},
],
},
},
Expand Down
89 changes: 89 additions & 0 deletions demo/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,95 @@ exports.sourceNodes = (gatsbyUtils) => {
createNode,
createNodeId,
});

// To test root config
const secureDistributionData1 = {
title: 'Secure Distribution Example One',
slug: 'secure-1',
cloudName: 'lilly-labs-consulting',
public: 'sample',
width: 864,
originalHeight: 576,
originalFormat: 'jpg',
};

createNode({
...secureDistributionData1,
id: createNodeId(`SecureDistribution >>> 1`),
internal: {
type: 'SecureDistribution',
contentDigest: createContentDigest(secureDistributionData1),
},
});

reporter.info(`[site] Create SecureDistribution with existing asset # 1`);

const cnameData1 = {
title: 'Cname (unsecure) Example One',
slug: 'cname-1',
cloud_name: 'lilly-labs-consulting',
public_id: 'sample',
metadata: {
width: 864,
height: 576,
format: 'jpg',
},
};

createNode({
...cnameData1,
id: createNodeId(`Cname >>> 1`),
internal: {
type: 'Cname',
contentDigest: createContentDigest(cnameData1),
},
});

reporter.info(`[site] Create Cname with existing asset # 1`);

const privateCdn1 = {
title: 'Private CDN Example One',
slug: 'private-cdn-1',
cloud_name: 'lilly-labs-consulting',
public_id: 'sample',
width: 864,
height: 576,
format: 'jpg',
};

createNode({
...privateCdn1,
id: createNodeId(`PrivateCDN >>> 1`),
internal: {
type: 'PrivateCDN',
contentDigest: createContentDigest(privateCdn1),
},
});

reporter.info(`[site] Create PrivateCDN with existing asset # 1`);

const privateCdnUnsecure1 = {
title: 'Private CDN Unsecure Example One',
slug: 'private-cdn-unsecure-1',
cloud_name: 'lilly-labs-consulting',
secure: false,
privateCdn: true,
public_id: 'sample',
width: 864,
height: 576,
format: 'jpg',
};

createNode({
...privateCdnUnsecure1,
id: createNodeId(`PrivateCDNUnsecure >>> 1`),
internal: {
type: 'PrivateCDNUnsecure',
contentDigest: createContentDigest(privateCdnUnsecure1),
},
});

reporter.info(`[site] Create PrivateCDNUnsecure with existing asset # 1`);
};

exports.onCreateNode = async (gatsbyUtils) => {
Expand Down
Loading

0 comments on commit 2d98c60

Please sign in to comment.