diff --git a/docs/v3.x/plugins/graphql.md b/docs/v3.x/plugins/graphql.md index 29bd8f11c4..fb983b83d3 100644 --- a/docs/v3.x/plugins/graphql.md +++ b/docs/v3.x/plugins/graphql.md @@ -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. - - `:asc` or `: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 + +- `:asc` or `:desc` - `where` (object): Define the filters to apply in the query. - ``: Equals. - `_ne`: Not equals. diff --git a/packages/strapi-plugin-graphql/services/schema-generator.js b/packages/strapi-plugin-graphql/services/schema-generator.js index 81576decb2..ac353b51a5 100644 --- a/packages/strapi-plugin-graphql/services/schema-generator.js +++ b/packages/strapi-plugin-graphql/services/schema-generator.js @@ -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'); @@ -65,6 +69,8 @@ const generateSchema = () => { ${polymorphicSchema.definition} ${Types.addInput()} + + ${PublicationState.definition} type AdminUser { id: ID! diff --git a/packages/strapi-plugin-graphql/services/type-definitions.js b/packages/strapi-plugin-graphql/services/type-definitions.js index 63b5e95a9f..c898bddcef 100644 --- a/packages/strapi-plugin-graphql/services/type-definitions.js +++ b/packages/strapi-plugin-graphql/services/type-definitions.js @@ -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: { diff --git a/packages/strapi-plugin-graphql/types/publication-state.js b/packages/strapi-plugin-graphql/types/publication-state.js new file mode 100644 index 0000000000..31c0b72d70 --- /dev/null +++ b/packages/strapi-plugin-graphql/types/publication-state.js @@ -0,0 +1,16 @@ +'use strict'; + +module.exports = { + definition: ` + enum PublicationState { + LIVE + PREVIEW + } + `, + resolver: { + PublicationState: { + LIVE: 'live', + PREVIEW: 'preview', + }, + }, +};