Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Allow child image sharp on arrays of images #90

Merged
merged 6 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ You can query Document nodes created from your Strapi API like the following:
}
}
```

To query images you can do the following:

```graphql
{
allStrapiArticle {
edges {
node {
id
singleImage {
publicURL
}
multipleImages {
localFile {
publicURL
}
}
}
}
}
}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-strapi",
"version": "0.0.10",
"version": "0.0.11",
"description": "Gatsby source plugin for building websites using Strapi as a data source",
"author": {
"email": "hi@strapi.io",
Expand Down
137 changes: 85 additions & 52 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,97 @@ const extractFields = async (
store,
cache,
createNode,
createNodeId,
touchNode,
auth,
item
item,
key = 'localFile'
) => {
for (const key of Object.keys(item)) {
const field = item[key]
if (Array.isArray(field)) {
// add recursion to fetch nested strapi references
await Promise.all(
field.map(async f =>
extractFields(apiURL, store, cache, createNode, touchNode, auth, f)
)
)
} else {
// image fields have a mime property among other
// maybe should find a better test
if (field !== null && field.hasOwnProperty('mime')) {
let fileNodeID
// using field on the cache key for multiple image field
const mediaDataCacheKey = `strapi-media-${item.id}-${key}`
const cacheMediaData = await cache.get(mediaDataCacheKey)
// image fields have a mime property among other
// maybe should find a better test
if (item && item.hasOwnProperty('mime')) {
let fileNodeID
// using field on the cache key for multiple image field
const mediaDataCacheKey = `strapi-media-${item.id}-${key}`
const cacheMediaData = await cache.get(mediaDataCacheKey)

// If we have cached media data and it wasn't modified, reuse
// previously created file node to not try to redownload
if (cacheMediaData && field.updatedAt === cacheMediaData.updatedAt) {
fileNodeID = cacheMediaData.fileNodeID
touchNode({ nodeId: cacheMediaData.fileNodeID })
}
// If we have cached media data and it wasn't modified, reuse
// previously created file node to not try to redownload
if (cacheMediaData && item.updatedAt === cacheMediaData.updatedAt) {
fileNodeID = cacheMediaData.fileNodeID
touchNode({ nodeId: cacheMediaData.fileNodeID })
}

// If we don't have cached data, download the file
if (!fileNodeID) {
try {
// full media url
const source_url = `${field.url.startsWith('http') ? '' : apiURL}${
field.url
}`
const fileNode = await createRemoteFileNode({
url: source_url,
store,
cache,
createNode,
auth,
})
// If we don't have cached data, download the file
if (!fileNodeID) {
try {
// full media url
const source_url = `${item.url.startsWith('http') ? '' : apiURL}${
item.url
}`
const fileNode = await createRemoteFileNode({
url: source_url,
store,
cache,
createNode,
createNodeId,
auth,
})

// If we don't have cached data, download the file
if (fileNode) {
fileNodeID = fileNode.id
// If we don't have cached data, download the file
if (fileNode) {
fileNodeID = fileNode.id

await cache.set(mediaDataCacheKey, {
fileNodeID,
modified: field.updatedAt,
})
}
} catch (e) {
// Ignore
}
}
if (fileNodeID) {
item[`${key}___NODE`] = fileNodeID
await cache.set(mediaDataCacheKey, {
fileNodeID,
updatedAt: item.updatedAt,
})
}
} catch (e) {
// Ignore
}
}

if (fileNodeID) {
if (key !== 'localFile') {
return fileNodeID
}

item.localFile___NODE = fileNodeID
}
} else if (Array.isArray(item)) {
await Promise.all(
item.map(async f =>
extractFields(
apiURL,
store,
cache,
createNode,
createNodeId,
touchNode,
auth,
f
)
)
)
} else if (item && typeof item === 'object') {
for (const key of Object.keys(item)) {
const field = item[key]

const fileNodeID = await extractFields(
apiURL,
store,
cache,
createNode,
createNodeId,
touchNode,
auth,
field,
key
)

if (fileNodeID) {
item[`${key}___NODE`] = fileNodeID
}
}
}
Expand All @@ -77,6 +108,7 @@ exports.downloadMediaFiles = async ({
store,
cache,
createNode,
createNodeId,
touchNode,
jwtToken: auth,
}) =>
Expand All @@ -89,6 +121,7 @@ exports.downloadMediaFiles = async ({
store,
cache,
createNode,
createNodeId,
touchNode,
auth,
item
Expand Down