mirror of
https://github.com/strapi/strapi.git
synced 2025-08-30 11:45:48 +00:00
Add publicationState argument for graphql queries
Signed-off-by: Convly <jean-sebastien.herbaux@epitech.eu>
This commit is contained in:
parent
ded8bc37bb
commit
0951e45d12
@ -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.
|
- `limit` (integer): Define the number of returned entries.
|
||||||
- `start` (integer): Define the amount of entries to skip.
|
- `start` (integer): Define the amount of entries to skip.
|
||||||
- `sort` (string): Define how the data should be sorted.
|
- `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.
|
- `where` (object): Define the filters to apply in the query.
|
||||||
- `<field>`: Equals.
|
- `<field>`: Equals.
|
||||||
- `<field>_ne`: Not equals.
|
- `<field>_ne`: Not equals.
|
||||||
|
@ -14,6 +14,7 @@ const { buildModels } = require('./type-definitions');
|
|||||||
const { mergeSchemas, createDefaultSchema, diffResolvers } = require('./utils');
|
const { mergeSchemas, createDefaultSchema, diffResolvers } = require('./utils');
|
||||||
const { toSDL } = require('./schema-definitions');
|
const { toSDL } = require('./schema-definitions');
|
||||||
const { buildQuery, buildMutation } = require('./resolvers-builder');
|
const { buildQuery, buildMutation } = require('./resolvers-builder');
|
||||||
|
const PublicationState = require('../types/publication-state');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate GraphQL schema.
|
* Generate GraphQL schema.
|
||||||
@ -51,9 +52,12 @@ const generateSchema = () => {
|
|||||||
const mutationFields =
|
const mutationFields =
|
||||||
shadowCRUD.mutation && toSDL(shadowCRUD.mutation, resolver.Mutation, null, 'mutation');
|
shadowCRUD.mutation && toSDL(shadowCRUD.mutation, resolver.Mutation, null, 'mutation');
|
||||||
|
|
||||||
|
Object.assign(resolvers, PublicationState.resolver);
|
||||||
|
|
||||||
const scalars = Types.getScalars();
|
const scalars = Types.getScalars();
|
||||||
|
|
||||||
Object.assign(resolvers, scalars);
|
Object.assign(resolvers, scalars);
|
||||||
|
|
||||||
const scalarDef = Object.keys(scalars)
|
const scalarDef = Object.keys(scalars)
|
||||||
.map(key => `scalar ${key}`)
|
.map(key => `scalar ${key}`)
|
||||||
.join('\n');
|
.join('\n');
|
||||||
@ -65,6 +69,8 @@ const generateSchema = () => {
|
|||||||
${polymorphicSchema.definition}
|
${polymorphicSchema.definition}
|
||||||
|
|
||||||
${Types.addInput()}
|
${Types.addInput()}
|
||||||
|
|
||||||
|
${PublicationState.definition}
|
||||||
|
|
||||||
type AdminUser {
|
type AdminUser {
|
||||||
id: ID!
|
id: ID!
|
||||||
|
@ -7,7 +7,14 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
<<<<<<< HEAD
|
||||||
const { contentTypes } = require('strapi-utils');
|
const { contentTypes } = require('strapi-utils');
|
||||||
|
=======
|
||||||
|
const {
|
||||||
|
getPrivateAttributes,
|
||||||
|
contentTypes: { hasDraftAndPublish },
|
||||||
|
} = require('strapi-utils');
|
||||||
|
>>>>>>> Add publicationState argument for graphql queries
|
||||||
|
|
||||||
const DynamicZoneScalar = require('../types/dynamiczoneScalar');
|
const DynamicZoneScalar = require('../types/dynamiczoneScalar');
|
||||||
|
|
||||||
@ -397,9 +404,13 @@ const buildCollectionType = model => {
|
|||||||
..._.get(_schema, `resolver.Query.${pluralName}`, {}),
|
..._.get(_schema, `resolver.Query.${pluralName}`, {}),
|
||||||
};
|
};
|
||||||
if (actionExists(resolverOpts)) {
|
if (actionExists(resolverOpts)) {
|
||||||
|
const draftAndPublishArgument = hasDraftAndPublish(model)
|
||||||
|
? 'publicationState: PublicationState'
|
||||||
|
: '';
|
||||||
|
const params = `(sort: String, limit: Int, start: Int, where: JSON ${draftAndPublishArgument})`;
|
||||||
_.merge(localSchema, {
|
_.merge(localSchema, {
|
||||||
query: {
|
query: {
|
||||||
[`${pluralName}(sort: String, limit: Int, start: Int, where: JSON)`]: `[${model.globalId}]`,
|
[`${pluralName}${params}`]: `[${model.globalId}]`,
|
||||||
},
|
},
|
||||||
resolvers: {
|
resolvers: {
|
||||||
Query: {
|
Query: {
|
||||||
|
16
packages/strapi-plugin-graphql/types/publication-state.js
Normal file
16
packages/strapi-plugin-graphql/types/publication-state.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
definition: `
|
||||||
|
enum PublicationState {
|
||||||
|
LIVE
|
||||||
|
PREVIEW
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
resolver: {
|
||||||
|
PublicationState: {
|
||||||
|
LIVE: 'live',
|
||||||
|
PREVIEW: 'preview',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user