Add publicationState argument for graphql queries

Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
Convly 2020-10-05 15:54:28 +02:00
parent ded8bc37bb
commit 0951e45d12
4 changed files with 42 additions and 2 deletions

View File

@ -226,7 +226,14 @@ You can also apply different parameters to the query to make more complex querie
- `limit` (integer): Define the number of returned entries.
- `start` (integer): Define the amount of entries to skip.
- `sort` (string): Define how the data should be sorted.
- `<field>:asc` or `<field>:desc`
- `publicationState` (PublicationState): Only select entries matching the publication state provided.
Handled states are:
- `LIVE`: Return only published values (default)
- `PREVIEW`: Return both draft & published entries
- `<field>:asc` or `<field>:desc`
- `where` (object): Define the filters to apply in the query.
- `<field>`: Equals.
- `<field>_ne`: Not equals.

View File

@ -14,6 +14,7 @@ const { buildModels } = require('./type-definitions');
const { mergeSchemas, createDefaultSchema, diffResolvers } = require('./utils');
const { toSDL } = require('./schema-definitions');
const { buildQuery, buildMutation } = require('./resolvers-builder');
const PublicationState = require('../types/publication-state');
/**
* Generate GraphQL schema.
@ -51,9 +52,12 @@ const generateSchema = () => {
const mutationFields =
shadowCRUD.mutation && toSDL(shadowCRUD.mutation, resolver.Mutation, null, 'mutation');
Object.assign(resolvers, PublicationState.resolver);
const scalars = Types.getScalars();
Object.assign(resolvers, scalars);
const scalarDef = Object.keys(scalars)
.map(key => `scalar ${key}`)
.join('\n');
@ -66,6 +70,8 @@ const generateSchema = () => {
${Types.addInput()}
${PublicationState.definition}
type AdminUser {
id: ID!
username: String

View File

@ -7,7 +7,14 @@
*/
const _ = require('lodash');
<<<<<<< HEAD
const { contentTypes } = require('strapi-utils');
=======
const {
getPrivateAttributes,
contentTypes: { hasDraftAndPublish },
} = require('strapi-utils');
>>>>>>> Add publicationState argument for graphql queries
const DynamicZoneScalar = require('../types/dynamiczoneScalar');
@ -397,9 +404,13 @@ const buildCollectionType = model => {
..._.get(_schema, `resolver.Query.${pluralName}`, {}),
};
if (actionExists(resolverOpts)) {
const draftAndPublishArgument = hasDraftAndPublish(model)
? 'publicationState: PublicationState'
: '';
const params = `(sort: String, limit: Int, start: Int, where: JSON ${draftAndPublishArgument})`;
_.merge(localSchema, {
query: {
[`${pluralName}(sort: String, limit: Int, start: Int, where: JSON)`]: `[${model.globalId}]`,
[`${pluralName}${params}`]: `[${model.globalId}]`,
},
resolvers: {
Query: {

View File

@ -0,0 +1,16 @@
'use strict';
module.exports = {
definition: `
enum PublicationState {
LIVE
PREVIEW
}
`,
resolver: {
PublicationState: {
LIVE: 'live',
PREVIEW: 'preview',
},
},
};