mirror of
https://github.com/strapi/strapi.git
synced 2025-12-24 21:54:24 +00:00
Create group type
This commit is contained in:
parent
a62f73fe5f
commit
fbfe41e1d1
@ -7,7 +7,7 @@
|
||||
},
|
||||
"options": {
|
||||
"increments": true,
|
||||
"timestamps": ["created_at", "updated_at"],
|
||||
"timestamps": true,
|
||||
"comment": ""
|
||||
},
|
||||
"attributes": {
|
||||
|
||||
@ -41,22 +41,18 @@ const buildShadowCRUD = (models, plugin) => {
|
||||
? strapi.plugins[plugin].models[name]
|
||||
: strapi.models[name];
|
||||
|
||||
const { globalId, primaryKey } = model;
|
||||
// Setup initial state with default attribute that should be displayed
|
||||
// but these attributes are not properly defined in the models.
|
||||
const initialState = {
|
||||
[model.primaryKey]: 'ID!',
|
||||
[primaryKey]: 'ID!',
|
||||
};
|
||||
|
||||
// always add an id field os to make the api database agnostic
|
||||
if (model.primaryKey !== 'id') {
|
||||
// always add an id field to make the api database agnostic
|
||||
if (primaryKey !== 'id') {
|
||||
initialState['id'] = 'ID!';
|
||||
}
|
||||
|
||||
const globalId = model.globalId;
|
||||
const _schema = _.cloneDeep(
|
||||
_.get(strapi.plugins, 'graphql.config._schema.graphql', {})
|
||||
);
|
||||
|
||||
if (!acc.resolver[globalId]) {
|
||||
acc.resolver[globalId] = {
|
||||
// define the default id resolver
|
||||
@ -68,13 +64,15 @@ const buildShadowCRUD = (models, plugin) => {
|
||||
|
||||
// Add timestamps attributes.
|
||||
if (_.isArray(_.get(model, 'options.timestamps'))) {
|
||||
Object.assign(initialState, {
|
||||
[_.get(model, ['options', 'timestamps', 0])]: 'DateTime!',
|
||||
[_.get(model, ['options', 'timestamps', 1])]: 'DateTime!',
|
||||
});
|
||||
const [createdAtKey, updatedAtKey] = model.options.timestamps;
|
||||
initialState[createdAtKey] = 'DateTime!';
|
||||
initialState[updatedAtKey] = 'DateTime!';
|
||||
}
|
||||
|
||||
// Retrieve user customisation.
|
||||
const _schema = _.cloneDeep(
|
||||
_.get(strapi.plugins, 'graphql.config._schema.graphql', {})
|
||||
);
|
||||
|
||||
const { type = {}, resolver = {} } = _schema;
|
||||
|
||||
// Convert our layer Model to the GraphQL DL.
|
||||
|
||||
@ -161,6 +161,44 @@ const schemaBuilder = {
|
||||
}, modelCruds);
|
||||
}
|
||||
|
||||
const groupDefs = Object.values(strapi.groups)
|
||||
.map(group => {
|
||||
const { globalId, attributes, primaryKey } = group;
|
||||
|
||||
const definition = {
|
||||
[primaryKey]: 'ID!',
|
||||
};
|
||||
|
||||
// always add an id field to make the api database agnostic
|
||||
if (primaryKey !== 'id') {
|
||||
definition['id'] = 'ID!';
|
||||
}
|
||||
// Add timestamps attributes.
|
||||
if (_.isArray(_.get(group, 'options.timestamps'))) {
|
||||
const [createdAtKey, updatedAtKey] = group.options.timestamps;
|
||||
definition[createdAtKey] = 'DateTime!';
|
||||
definition[updatedAtKey] = 'DateTime!';
|
||||
}
|
||||
|
||||
const gqlAttributes = Object.keys(attributes)
|
||||
.filter(attribute => attributes[attribute].private !== true)
|
||||
.reduce((acc, attribute) => {
|
||||
// Convert our type to the GraphQL type.
|
||||
acc[attribute] = Types.convertType({
|
||||
definition: attributes[attribute],
|
||||
modelName: globalId,
|
||||
attributeName: attribute,
|
||||
});
|
||||
|
||||
return acc;
|
||||
}, definition);
|
||||
|
||||
return `type ${globalId} {${this.formatGQL(gqlAttributes, {}, group)}}`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
console.log(groupDefs);
|
||||
|
||||
// Extract custom definition, query or resolver.
|
||||
const {
|
||||
definition,
|
||||
@ -238,6 +276,7 @@ const schemaBuilder = {
|
||||
let typeDefs = `
|
||||
${definition}
|
||||
${shadowCRUD.definition}
|
||||
${groupDefs}
|
||||
type Query {${shadowCRUD.query &&
|
||||
this.formatGQL(
|
||||
shadowCRUD.query,
|
||||
@ -268,7 +307,7 @@ const schemaBuilder = {
|
||||
this.writeGenerateSchema(graphql.printSchema(schema));
|
||||
}
|
||||
|
||||
// Remove custom scaler (like Upload);
|
||||
// Remove custom scalar (like Upload);
|
||||
typeDefs = Types.removeCustomScalar(typeDefs, resolvers);
|
||||
|
||||
return {
|
||||
|
||||
@ -33,7 +33,7 @@ module.exports = {
|
||||
action = '',
|
||||
}) {
|
||||
// Type
|
||||
if (definition.type) {
|
||||
if (definition.type && definition.type !== 'group') {
|
||||
let type = 'String';
|
||||
|
||||
switch (definition.type) {
|
||||
@ -74,6 +74,16 @@ module.exports = {
|
||||
return type;
|
||||
}
|
||||
|
||||
if (definition.type === 'group') {
|
||||
const globalId = strapi.groups[definition.group].globalId;
|
||||
const typeId = definition.required === true ? `${globalId}!` : globalId;
|
||||
if (definition.repeatable === true) {
|
||||
return `[${typeId}]!`;
|
||||
}
|
||||
|
||||
return `${typeId}`;
|
||||
}
|
||||
|
||||
const ref = definition.model || definition.collection;
|
||||
|
||||
// Association
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user