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

Commit 271bf2d

Browse files
remidejpwizla
andauthored
Refactor readme examples (#206)
* Refactor readme examples * add combined example * Rephrase titles * Fix readme typo Co-authored-by: Pierre Wizla <[email protected]> Co-authored-by: Pierre Wizla <[email protected]>
1 parent 9e109da commit 271bf2d

File tree

1 file changed

+82
-19
lines changed

1 file changed

+82
-19
lines changed

README.md

Lines changed: 82 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,42 @@ Source plugin for pulling documents into Gatsby from a Strapi API.
44

55
> **WARNING**: This is the README for v1.0.0 which is in `alpha` version for now. Make sure to install it with @alpha to try it out. It's designed to be used with Gatsby v3.
66
7-
## Install
7+
## Installing the plugin
88

9-
`npm install --save gatsby-source-strapi@alpha`
9+
```sh
10+
# Using Yarn
11+
yarn add gatsby-source-strapi@alpha
1012

11-
## How to use
13+
# Or using NPM
14+
npm install --save gatsby-source-strapi@alpha
15+
```
16+
17+
## Setting up the plugin
18+
19+
You can enable and configure this plugin in your `gatsby-config.js` file.
20+
21+
### Basic usage
22+
23+
```javascript
24+
// In your gatsby-config.js
25+
plugins: [
26+
{
27+
resolve: `gatsby-source-strapi`,
28+
options: {
29+
apiURL: `http://localhost:1337`,
30+
queryLimit: 1000, // Defaults to 100
31+
collectionTypes: [`article`, `user`],
32+
singleTypes: [`home-page`, `contact`],
33+
},
34+
},
35+
];
36+
```
37+
38+
### Advanced usage
39+
40+
#### Custom endpoint
41+
42+
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.
1243

1344
```javascript
1445
// In your gatsby-config.js
@@ -17,30 +48,63 @@ plugins: [
1748
resolve: `gatsby-source-strapi`,
1849
options: {
1950
apiURL: `http://localhost:1337`,
20-
queryLimit: 1000, // Default to 100
2151
collectionTypes: [
22-
`article`,
23-
`user`,
24-
// if you don't want to leave the definition of an api endpoint to the pluralize module
2552
{
2653
name: `collection-name`,
2754
endpoint: `custom-endpoint`,
2855
},
29-
// if you want to use custom query strings (e.g. to fetch all locales)
30-
// mapping of api.qs object will be used to create final query string (e.g: http://localhost:1337/collection-name?_locale=all)
56+
]
57+
},
58+
},
59+
];
60+
```
61+
62+
#### Internationalization support
63+
64+
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.
65+
66+
```javascript
67+
// In your gatsby-config.js
68+
plugins: [
69+
{
70+
resolve: `gatsby-source-strapi`,
71+
options: {
72+
apiURL: `http://localhost:1337`,
73+
collectionTypes: [
74+
// Fetch all locales for collection-name
3175
{
3276
name: `collection-name`,
33-
api: { qs: { _locale: 'all' } }
77+
api: { qs: { _locale: `all` } }
3478
},
35-
// exemple fetching only english content
79+
// Only fetch english content for other-collection-name
3680
{
37-
name: `collection-name`,
38-
api: { qs: { _locale: 'en' } }
81+
name: `other-collection-name`,
82+
api: { qs: { _locale: `en` } }
3983
},
40-
],
41-
//If using single types place them in this array.
42-
singleTypes: [`home-page`, `contact`],
43-
// Possibility to login with a strapi user, when content types are not publically available (optional).
84+
// Combined with a custom endpoint
85+
{
86+
name: `another-collection-name`,
87+
endpoint: `custom-endpoint`,
88+
api: { qs: { _locale: `en` } }
89+
},
90+
]
91+
},
92+
},
93+
];
94+
```
95+
96+
#### Authenticated requests
97+
98+
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.
99+
100+
```javascript
101+
// In your gatsby-config.js
102+
plugins: [
103+
{
104+
resolve: `gatsby-source-strapi`,
105+
options: {
106+
apiURL: `http://localhost:1337`,
107+
collectionTypes: [`collection-name`],
44108
loginData: {
45109
identifier: '',
46110
password: '',
@@ -50,7 +114,7 @@ plugins: [
50114
];
51115
```
52116

53-
## How to query
117+
## Querying data
54118

55119
You can query Document nodes created from your Strapi API like the following:
56120

@@ -86,7 +150,6 @@ Make sure to add `api.qs._locale` to your strapi configuration in `gatsby-config
86150
}
87151
```
88152

89-
90153
To query images you can do the following:
91154

92155
```graphql

0 commit comments

Comments
 (0)