Skip to content
This repository was archived by the owner on Sep 16, 2025. It is now read-only.

Commit b0aaa5d

Browse files
DomDewdewitzmachbarComfortablyCoding
authored
feat(graphql): add publicationState filter support (#23)
* feat(slugController): add publicationState as a query parameter (#23) * feat(graphql): add publicationState as an arg to graphql type (#24) * chore(README): update readme (#25) * feat(graphql): refactor publicationState query * chore(README): update readme to include previewState example for GraphQL * feat(graphql): update server/utils/isValidFindSlugParams.js Co-authored-by: daedalus <[email protected]> * chore(readme): update readme with suggested changes Co-authored-by: daedalus <[email protected]> * feat(graphql): apply suggestions from code review in types Co-authored-by: daedalus <[email protected]> * feat(graphql): include lodash check based on requested changes * fix(graphql): contentType not defined in lodash check Co-authored-by: Dominik Dewitz <[email protected]> Co-authored-by: daedalus <[email protected]>
1 parent 32e5d0a commit b0aaa5d

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Any time the respective content types have an entity created or updated the slug
8282

8383
### Find One by Slug
8484

85-
Hitting the `/api/slugify/slugs/:modelName/:slug` endpoint for any configured content types will return the entity type that matches the slug in the url.
85+
Hitting the `/api/slugify/slugs/:modelName/:slug` endpoint for any configured content types will return the entity type that matches the slug in the url. Additionally the endpoint accepts any of the parameters that can be added to the routes built into Strapi.
8686

8787
**IMPORTANT** The modelName is case sensitive and must match exactly with the name defined in the configuration.
8888

@@ -120,6 +120,25 @@ await fetch(`${API_URL}/api/slugify/slugs/article/lorem-ipsum-dolor`);
120120
}
121121
```
122122

123+
Additionally if `draftAndPublish` is enabled for the content-type a `publicationState` arg can be passed to the GraphQL query that accepts either `preview` or `live` as input.
124+
125+
**IMPORTANT** Please beware that the request for an entry in `preview` will return both draft entries & published entries as per Strapi default.
126+
127+
```graphql
128+
{
129+
findSlug(modelName:"article",slug:"lorem-ipsum-dolor",publicationState:"preview"){
130+
... on ArticleEntityResponse{
131+
data{
132+
id
133+
attributes{
134+
title
135+
}
136+
}
137+
}
138+
}
139+
}
140+
```
141+
123142
### Example Response
124143

125144
If an article with the slug of `lorem-ipsum-dolor` exists the response will look the same as a single entity response

server/graphql/types.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ const getCustomTypes = (strapi, nexus) => {
4747
args: {
4848
modelName: nexus.stringArg('The model name of the content type'),
4949
slug: nexus.stringArg('The slug to query for'),
50+
publicationState: nexus.stringArg('The publication state of the entry')
5051
},
5152
resolve: async (_parent, args, ctx) => {
5253
const { models } = getPluginService(strapi, 'settingsService').get();
53-
const { modelName, slug } = args;
54+
const { modelName, slug, publicationState } = args;
5455
const { auth } = ctx.state;
5556

5657
isValidFindSlugParams({
5758
modelName,
5859
slug,
5960
models,
61+
publicationState
6062
});
6163

6264
const { uid, field, contentType } = models[modelName];
@@ -70,10 +72,10 @@ const getCustomTypes = (strapi, nexus) => {
7072
},
7173
};
7274

73-
// only return published entries
75+
// only return published entries by default if content type has draftAndPublish enabled
7476
if (_.get(contentType, ['options', 'draftAndPublish'], false)) {
75-
query.publicationState = 'live';
76-
}
77+
query.publicationState = publicationState || 'live';
78+
}
7779

7880
const data = await getPluginService(strapi, 'slugService').findOne(uid, query);
7981
const sanitizedEntity = await sanitizeOutput(data, contentType, auth);

server/utils/isValidFindSlugParams.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const { ValidationError } = require('@strapi/utils/lib/errors');
2+
const _ = require('lodash');
23

34
const isValidFindSlugParams = (params) => {
45
if (!params) {
56
throw new ValidationError('A model and slug must be provided.');
67
}
78

8-
const { modelName, slug, models } = params;
9+
const { modelName, slug, models, publicationState } = params;
10+
const model = models[modelName];
911

1012
if (!modelName) {
1113
throw new ValidationError('A model name path variable is required.');
@@ -15,7 +17,9 @@ const isValidFindSlugParams = (params) => {
1517
throw new ValidationError('A slug path variable is required.');
1618
}
1719

18-
const model = models[modelName];
20+
if (_.get(model, ['contentType', 'options', 'draftAndPublish'], false) && publicationState) {
21+
throw new ValidationError('Filtering by publication state is only supported for content types that have Draft and Publish enabled.')
22+
}
1923

2024
// ensure valid model is passed
2125
if (!model) {

0 commit comments

Comments
 (0)