Skip to content

Commit f9fe0e6

Browse files
authored
feat: allow some meta data to be passed in GraphQL mutations (#4643)
1 parent 729d809 commit f9fe0e6

File tree

9 files changed

+58
-20
lines changed

9 files changed

+58
-20
lines changed

packages/api-aco/src/folder/createFolderTypeDefs.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export const createFolderTypeDefs = (params: CreateFolderTypeDefsParams): string
8484
${inputCreateFields.map(f => f.typeDefs).join("\n")}
8585
8686
input FolderCreateInput {
87+
# Pass an ID if you want to create a folder with a specific ID.
88+
id: ID
89+
8790
${inputCreateFields.map(f => f.fields).join("\n")}
8891
}
8992

packages/api-aco/src/record/record.so.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AcoSearchRecordStorageOperations, SearchRecord } from "./record.types";
66
import { CmsModel, UpdateCmsEntryInput } from "@webiny/api-headless-cms/types";
77
import { attachAcoRecordPrefix } from "~/utils/acoRecordId";
88
import { SEARCH_RECORD_MODEL_ID } from "~/record/record.model";
9-
import { ENTRY_META_FIELDS } from "@webiny/api-headless-cms/constants";
9+
import { ENTRY_META_FIELDS, pickEntryMetaFields } from "@webiny/api-headless-cms/constants";
1010

1111
export const createSearchRecordOperations = (
1212
params: CreateAcoStorageOperationsParams
@@ -75,12 +75,30 @@ export const createSearchRecordOperations = (
7575

7676
return [tags, meta];
7777
},
78-
async createRecord(model, { data: SearchRecordData }) {
79-
const { tags = [], data = {}, ...rest } = SearchRecordData;
78+
async createRecord(model, { data: searchRecordData }) {
79+
const { tags = [], data = {}, ...rest } = searchRecordData;
80+
81+
// We added this so that if the main record has its meta fields set with
82+
// custom values, we can propagate them to the search record as well.
83+
const { createdBy, createdOn, modifiedBy, modifiedOn, savedBy, savedOn } =
84+
pickEntryMetaFields(data);
85+
8086
const entry = await cms.createEntry(model, {
8187
tags,
8288
data,
8389
...rest,
90+
createdBy,
91+
createdOn,
92+
modifiedBy,
93+
modifiedOn,
94+
savedBy,
95+
savedOn,
96+
revisionCreatedBy: createdBy,
97+
revisionCreatedOn: createdOn,
98+
revisionModifiedBy: modifiedBy,
99+
revisionModifiedOn: modifiedOn,
100+
revisionSavedBy: savedBy,
101+
revisionSavedOn: savedOn,
84102
id: attachAcoRecordPrefix(rest.id)
85103
});
86104

packages/api-page-builder/__tests__/graphql/createPage.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ describe("CRUD Test", () => {
1818
});
1919

2020
const page = {
21-
id: "67e15c96026bd2000222d698#0001",
21+
id: "67e15c96026bd2000222d698#0002",
2222
pid: "67e15c96026bd2000222d698",
2323
category: "slug",
24-
version: 1,
24+
version: 2,
2525
title: "Welcome to Webiny",
2626
path: "/welcome-to-webiny",
2727
content: {
@@ -62,7 +62,7 @@ describe("CRUD Test", () => {
6262
};
6363

6464
// The V2 of the createPage mutation should allow us to create pages with
65-
// predefined `createdOn`, `createdBy`, `id`, and also immediately have the
65+
// predefined `createdOn`, `createdBy`, `id`, `version`, and also immediately have the
6666
// page published.
6767
await createPageV2({ data: page });
6868

@@ -73,13 +73,13 @@ describe("CRUD Test", () => {
7373
pageBuilder: {
7474
getPage: {
7575
data: {
76-
id: "67e15c96026bd2000222d698#0001",
76+
id: "67e15c96026bd2000222d698#0002",
7777
pid: "67e15c96026bd2000222d698",
7878
editor: "page-builder",
7979
category: {
8080
slug: "slug"
8181
},
82-
version: 1,
82+
version: 2,
8383
title: "Welcome to Webiny",
8484
path: "/welcome-to-webiny",
8585
url: "https://www.test.com/welcome-to-webiny",
@@ -97,10 +97,10 @@ describe("CRUD Test", () => {
9797
publishedOn: "2025-03-24T13:22:30.918Z",
9898
revisions: [
9999
{
100-
id: "67e15c96026bd2000222d698#0001",
100+
id: "67e15c96026bd2000222d698#0002",
101101
status: "published",
102102
locked: true,
103-
version: 1
103+
version: 2
104104
}
105105
],
106106
settings: {
@@ -143,13 +143,13 @@ describe("CRUD Test", () => {
143143
pageBuilder: {
144144
getPublishedPage: {
145145
data: {
146-
id: "67e15c96026bd2000222d698#0001",
146+
id: "67e15c96026bd2000222d698#0002",
147147
pid: "67e15c96026bd2000222d698",
148148
editor: "page-builder",
149149
category: {
150150
slug: "slug"
151151
},
152-
version: 1,
152+
version: 2,
153153
title: "Welcome to Webiny",
154154
path: "/welcome-to-webiny",
155155
url: "https://www.test.com/welcome-to-webiny",
@@ -167,10 +167,10 @@ describe("CRUD Test", () => {
167167
publishedOn: "2025-03-24T13:22:30.918Z",
168168
revisions: [
169169
{
170-
id: "67e15c96026bd2000222d698#0001",
170+
id: "67e15c96026bd2000222d698#0002",
171171
status: "published",
172172
locked: true,
173-
version: 1
173+
version: 2
174174
}
175175
],
176176
settings: {

packages/api-page-builder/src/graphql/crud/pages.crud.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ export const createPageCrud = (params: CreatePageCrudParams): PagesCrud => {
474474

475475
const id = createIdentifier({
476476
id: pageId,
477-
version: 1
477+
version
478478
});
479479

480480
const rawSettings = input.settings || {

packages/api-page-builder/src/graphql/graphql/pages.gql.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ const createBasePageGraphQL = (): GraphQLSchemaPlugin<PbContext> => {
260260
extend type PbMutation {
261261
createPage(from: ID, category: String, meta: JSON): PbPageResponse
262262
263-
createPageV2(data: PbCreatePageV2Input!): PbPageResponse
263+
createPageV2(data: PbCreatePageV2Input!, meta: JSON): PbPageResponse
264264
265265
# Update page by given ID.
266266
updatePage(id: ID!, data: PbUpdatePageInput!): PbPageResponse
@@ -479,8 +479,8 @@ const createBasePageGraphQL = (): GraphQLSchemaPlugin<PbContext> => {
479479
},
480480
createPageV2: async (_, args: any, context) => {
481481
return resolve(() => {
482-
const { data } = args;
483-
return context.pageBuilder.createPageV2(data);
482+
const { data, meta } = args;
483+
return context.pageBuilder.createPageV2(data, meta);
484484
});
485485
},
486486

packages/api-security/src/createSecurity/createApiKeysMethods.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ const apiKeyModelValidation = zod.object({
2222
.default([])
2323
});
2424

25+
const createApiKeyModelValidation = apiKeyModelValidation.extend({
26+
id: zod.string().optional(),
27+
token: zod
28+
.string()
29+
.optional()
30+
.refine(val => !val || val.startsWith("a"), {
31+
message: 'Token must start with letter "a"'
32+
})
33+
});
34+
2535
const generateToken = (tokenLength = 48): string => {
2636
const token = crypto.randomBytes(Math.ceil(tokenLength / 2)).toString("hex");
2737

@@ -126,7 +136,7 @@ export const createApiKeysMethods = ({
126136
throw new NotAuthorizedError();
127137
}
128138

129-
const validation = apiKeyModelValidation.safeParse(data);
139+
const validation = createApiKeyModelValidation.safeParse(data);
130140
if (!validation.success) {
131141
throw createZodError(validation.error);
132142
}

packages/api-security/src/graphql/apiKey.gql.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ export default new GraphQLSchemaPlugin<SecurityContext>({
1515
}
1616
1717
input SecurityApiKeyInput {
18+
id: ID
1819
name: String!
1920
description: String
21+
token: String
2022
permissions: [JSON]!
2123
}
2224

packages/api-serverless-cms/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export {
6161
createPrivateModelPlugin,
6262
createPrivateSingleEntryModelPlugin,
6363

64+
// Model fields (this is not a plugin factory, hence the missing `Plugin` suffix in the name).
65+
createModelField,
66+
6467
// Other.
68+
createCmsGraphQLSchemaPlugin,
6569
createStorageTransformPlugin
6670
} from "@webiny/api-headless-cms";

packages/api-tenant-manager/src/graphql/tenants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default new GraphQLSchemaPlugin<Context>({
3434
}
3535
3636
input CreateTenantInput {
37+
id: ID
3738
name: String!
3839
description: String!
3940
tags: [String!]!
@@ -98,8 +99,8 @@ export default new GraphQLSchemaPlugin<Context>({
9899
await checkPermissions(context);
99100
const tenant = context.tenancy.getCurrentTenant();
100101
const newTenant = await context.tenancy.createTenant({
101-
...args.data,
102102
id: mdbid(),
103+
...args.data,
103104
parent: tenant.id
104105
});
105106

0 commit comments

Comments
 (0)