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

Init migration to v4 #266

Merged
merged 41 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
43d484a
Update dependencies
soupette Jan 21, 2022
35e7f6b
Init migration
soupette Jan 21, 2022
0ff61e2
Fix relations and file download
soupette Jan 21, 2022
73f0dd8
Fix multiple images and add support for components
soupette Jan 21, 2022
d760c9e
Rename index.js to gatsby-node, clean data for dynamiczone
soupette Jan 25, 2022
5373051
Init DZ support
soupette Jan 25, 2022
9fb72a1
Delete old nodes
soupette Jan 25, 2022
8682f9e
Fix content digest
soupette Jan 26, 2022
fb02bbc
Add comments
soupette Jan 26, 2022
d522b70
Fix type naming and delete only parent nodes
soupette Jan 26, 2022
2182fac
Only fetch data after build
soupette Jan 26, 2022
fed9db5
Merge pull request #267 from strapi/delta-fetch
soupette Jan 27, 2022
080fdf2
Extract images for markdown field
soupette Jan 27, 2022
874ca31
Extract markdown images
soupette Jan 27, 2022
dcec77f
Fix PR feedback
soupette Jan 28, 2022
0d557a7
Merge pull request #268 from strapi/extract-markdown-files
soupette Jan 28, 2022
f91f7fd
Refacto code
soupette Jan 28, 2022
d246601
Merge pull request #269 from strapi/refactoring
soupette Jan 28, 2022
e54a281
Create manifest
soupette Feb 7, 2022
69fab3a
Change manifestId
soupette Feb 8, 2022
5d180b2
Merge pull request #274 from strapi/content-sync
soupette Feb 8, 2022
82d3072
Update plugin config and README accordingly
soupette Feb 8, 2022
a5b5595
Merge pull request #275 from strapi/improve-config
soupette Feb 9, 2022
a1cd966
Add support for user content type and file content type
soupette Feb 9, 2022
f2fb197
Fix PR feedback
soupette Feb 9, 2022
e1e6116
Merge pull request #276 from strapi/fix-users-content-type
soupette Feb 9, 2022
bba30b1
v2.0.0-alpha.1
soupette Feb 9, 2022
780acb2
Refacto README
soupette Feb 9, 2022
e1a29e0
Set timestamps after fetching models
soupette Feb 10, 2022
806963f
Update readme and fix restricted field
soupette Feb 10, 2022
6298b76
Fix PR feedback
soupette Feb 10, 2022
aa4f32f
Merge pull request #279 from strapi/update-readme
remidej Feb 10, 2022
73cf9bd
Fix typo
soupette Feb 10, 2022
e4db837
v2.0.0-alpha.2
soupette Feb 10, 2022
4f4298e
v2.0.0-alpha.3
soupette Feb 10, 2022
f897fc9
Update readme
soupette Feb 11, 2022
a2ab208
Fix PR feedback
soupette Feb 11, 2022
1fde793
Merge pull request #282 from strapi/fix-readme
soupette Feb 11, 2022
e74dafd
Fix PR feedback
soupette Feb 15, 2022
3195a48
v2.0.0-alpha.4
soupette Feb 15, 2022
25c0ff1
Fix conflicts
soupette Feb 15, 2022
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
240 changes: 86 additions & 154 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,158 +22,99 @@ You can enable and configure this plugin in your `gatsby-config.js` file.

```javascript
// In your gatsby-config.js

const strapiConfig = {
apiURL: process.env.STRAPI_API_URL,
accessToken: process.env.STRAPI_TOKEN,
collectionTypes: [
{
singularName: 'article',
queryParams: {
// Populate all the fields
populate: '*',
},
},
{
singularName: 'company',
queryParams: {
populate: '*',
},
},
{
singularName: 'author',
queryParams: {
populate: '*',
},
},
],
singleTypes: [
// {
// singularName: "about",
// /**
// * Default queryParams value
// * {
// * populate: '*',
// * }
// * */
// },
],
};

plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
queryLimit: 1000, // Defaults to 100
collectionTypes: [`article`, `user`],
singleTypes: [`home-page`, `contact`],
},
options: strapiConfig,
},
];
```

### Advanced usage

#### Custom endpoint

By default, we use the pluralize module to deduct the endpoint that matches a collection type. You can opt out of this behavior. To do so, pass an entity definition object with your custom endpoint.
#### Deep queries populate

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
collectionTypes: [
{
name: `collection-name`,
endpoint: `custom-endpoint`,
const strapiConfig = {
// ...
collectionTypes: [
{
singularName: 'article',
queryParams: {
// Populate media and relations
// Make sure to not specify the fields key so the api always returns the updatedAt
populate: {
image: '*',
images: '*',
author: {
populate: {
avatar: '*',
company: {
populate: {
image: '*',
},
},
},
},
},
]
},
},
},
];
],
};
```

#### Internationalization support

Strapi now supports [internationalization](https://strapi.io/documentation/developer-docs/latest/development/plugins/i18n.html#installation). But by default, this plugin will only fetch data in the default locale of your Strapi app. If your content types are available in different locales, you can also pass an entity definition object to specify the locale you want to fetch for a content type. Use the `all` value to get all available locales on a collection type.

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
collectionTypes: [
// Fetch all locales for collection-name
{
name: `collection-name`,
api: { qs: { _locale: `all` } }
},
// Only fetch english content for other-collection-name
{
name: `other-collection-name`,
api: { qs: { _locale: `en` } }
},
// Combined with a custom endpoint
{
name: `another-collection-name`,
endpoint: `custom-endpoint`,
api: { qs: { _locale: `en` } }
},
]
},
},
];
```

For single types, the `all` value will not work, since single type queries do not return an array. If you want a single type to be available in different locales, add several entity definition objects for that same single type. The source plugin will merge them together, so you can access the right locale in your queries using the `locale` filter.

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
singleTypes: [
{
name: 'single-type-name',
api: {
qs: {
_locale: 'en'
}
},
},
{
name: 'single-type-name',
api: {
qs: {
_locale: 'fr'
}
},
},
],
},
},
];
```
##### TODO

#### Draft content

Strapi now supports [Draft and publish](https://strapi.io/documentation/developer-docs/latest/concepts/draft-and-publish.html#draft-and-publish), which allows you to save your content as a draft and publish it later. By default, this plugin will only fetch the published content.

But you may want to fetch unpublished content in Gatsby as well. To do so, find a content type that has draft & publish enabled, and add an entity definition object to your config. Then, use the query string option to specify the [publication state](https://strapi.io/documentation/developer-docs/latest/developer-resources/content-api/content-api.html#publication-state) API parameter.

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
collectionTypes: [
{
name: 'collection-name',
api: {
qs: {
// 'preview' fetches both draft & published content
_publicationState: 'preview',
}
}
}
],
},
},
],
```

#### Authenticated requests

Strapi's [Roles & Permissions plugin](https://strapi.io/documentation/developer-docs/latest/development/plugins/users-permissions.html#concept) allows you to protect your API actions. If you need to access a route that is only available to a logged in user, you can provide your credentials so that this plugin can access to the protected data.

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-strapi`,
options: {
apiURL: `http://localhost:1337`,
collectionTypes: [`collection-name`],
loginData: {
identifier: '',
password: '',
},
},
},
];
```
##### TODO

## Querying data

Expand All @@ -182,17 +123,32 @@ You can query Document nodes created from your Strapi API like the following:
```graphql
{
allStrapiArticle {
edges {
node {
id
title
content
nodes {
author {
name
avatar {
localFile {
childImageSharp {
gatsbyImageData
}
}
}
}
categories {
name
}
content {
childMarkdownRemark {
html
}
}
}
}
}
```

##### TODO

You can query Document nodes in a chosen language

Make sure to add `api.qs._locale` to your strapi configuration in `gatsby-config.js` (see example above)
Expand All @@ -210,27 +166,3 @@ Make sure to add `api.qs._locale` to your strapi configuration in `gatsby-config
}
}
```

To query images you can do the following:

```graphql
{
allStrapiArticle {
edges {
node {
id
singleImage {
localFile {
publicURL
}
}
multipleImages {
localFile {
publicURL
}
}
}
}
}
}
```
2 changes: 1 addition & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib')
module.exports = require('./lib/gatsby-node');
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@
"strapi"
],
"dependencies": {
"axios": "0.21.1",
"gatsby-node-helpers": "0.3.0",
"gatsby-source-filesystem": "3.7.1",
"lodash": "4.17.21",
"pluralize": "8.0.0"
"axios": "0.24.0",
"gatsby-source-filesystem": "4.5.1",
"lodash": "^4.17.21",
"qs": "^6.10.3"
},
"devDependencies": {
"@babel/cli": "7.14.5",
Expand All @@ -62,6 +61,12 @@
"lint-staged": "10.5.1",
"prettier": "2.1.2"
},
"peerDependencies": {
"gatsby": "^4.0.0",
"gatsby-plugin-image": "^2.5.1",
"gatsby-plugin-sharp": "^4.5.1",
"sharp": "^0.29.0"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
Expand Down
18 changes: 18 additions & 0 deletions src/axiosInstance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import axios from 'axios';

const createInstance = (config) => {
const headers = { ...config?.headers };

if (config.accessToken) {
headers.authorization = `Bearer ${config.accessToken}`;
}

const instance = axios.create({
baseURL: config.apiURL,
headers,
});

return instance;
};

export default createInstance;
Loading