Skip to content

Commit

Permalink
Merge pull request #32 from storyblok/feature/datasource-entries-dime…
Browse files Browse the repository at this point in the history
…nsions

Datasource Entries dimensions missing
  • Loading branch information
onefriendaday authored Jul 31, 2020
2 parents 9061ece + 6cb4dde commit 460a02c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ This lets you for example to query for a specific component:
}
```

### Datasources (without the entries)
### Datasources

```GraphQL
allStoryblokDatasource(filter: {data_source: {eq: null}}) {
allStoryblokDatasource {
edges {
node {
id
Expand All @@ -217,16 +217,35 @@ allStoryblokDatasource(filter: {data_source: {eq: null}}) {
}
```

### Datasource Entries by specific Datasource
### Datasource Entries

This will return all datasources, with or not dimensions values:

```GraphQL
allStoryblokDatasourceEntry(filter: { data_source: { eq: "DATASOURCE_SLUG" } }) {
edges {
node {
id
name
value
data_source
data_source_dimension
}
}
}
```

If you want to **filter by a specific dimension**, you should use:

```GraphQL
allStoryblokDatasource(filter: { data_source: { eq: "DATASOURCE_SLUG" } }) {
allStoryblokDatasourceEntry(filter: { data_source: { eq: "DATASOURCE_SLUG" }, data_source_dimension: { eq: "DATASOURCE_DIMENSION_VALUE" } }) {
edges {
node {
id
name
value
data_source
data_source_dimension
}
}
}
Expand Down
33 changes: 27 additions & 6 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ exports.sourceNodes = async function({ boundActionCreators }, options) {
const languages = space.language_codes.map((lang) => { return lang + '/*' })
languages.push('')

for (var spKey = 0; spKey < languages.length; spKey++) {
for (const language of languages) {
await Sync.getAll('stories', {
node: 'StoryblokEntry',
params: getStoryParams(languages[spKey], options),
params: getStoryParams(language, options),
process: (item) => {
for (var prop in item.content) {
if (!item.content.hasOwnProperty(prop) || ['_editable', '_uid'].indexOf(prop) > -1) {
Expand Down Expand Up @@ -63,13 +63,34 @@ exports.sourceNodes = async function({ boundActionCreators }, options) {
node: 'StoryblokDatasource'
})

for (var dsKey = 0; dsKey < datasources.length; dsKey++) {
for (const datasource of datasources) {
const datasourceSlug = datasource.slug

await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasource',
params: {datasource: datasources[dsKey].slug},
node: 'StoryblokDatasourceEntry',
params: {
datasource: datasourceSlug
},
process: (item) => {
item.data_source = datasources[dsKey].slug
item.data_source_dimension = null
item.data_source = datasourceSlug
}
})

const datasourceDimensions = datasource.dimensions || []

for (const dimension of datasourceDimensions) {
await Sync.getAll('datasource_entries', {
node: 'StoryblokDatasourceEntry',
params: {
datasource: datasourceSlug,
dimension: dimension.entry_value
},
process: (item) => {
item.data_source_dimension = dimension.entry_value
item.data_source = datasourceSlug
}
})
}
}
}
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Alexander Feiglstorfer <[email protected]>",
"dependencies": {
"json-stringify-safe": "^5.0.1",
"storyblok-js-client": "^2.5.1"
"storyblok-js-client": "^2.5.2"
},
"scripts": {
"test": "jest"
Expand Down
32 changes: 30 additions & 2 deletions src/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,23 @@ module.exports = {
},

createNode(name, item) {
const nodeObject = this.builderNode(name, item)

this.$createNode(nodeObject)
},

builderNode (name, item) {
if (name ==='StoryblokDatasourceEntry') {
return this.factoryDatasourceEntryNode(name, item)
}

return this.factoryDefaultNode(name, item)
},

factoryDefaultNode (name, item) {
const lang = item.lang || 'default'
const node = Object.assign({}, item, {

return Object.assign({}, item, {
id: `${name.toLowerCase()}-${item.id}-${lang}`,
internalId: item.id,
parent: null,
Expand All @@ -40,8 +55,21 @@ module.exports = {
contentDigest: crypto.createHash(`md5`).update(stringify(item)).digest(`hex`)
}
})
},

this.$createNode(node)
factoryDatasourceEntryNode (name, item) {
const dimension = item.data_source_dimension || 'default'
return Object.assign({}, item, {
id: `${name.toLowerCase()}-${item.id}-${dimension}`,
internalId: item.id,
parent: null,
children: [],
internal: {
mediaType: `application/json`,
type: name,
contentDigest: crypto.createHash(`md5`).update(stringify(item)).digest(`hex`)
}
})
},

async getOne(single, type, options) {
Expand Down

0 comments on commit 460a02c

Please sign in to comment.