From e35b19bfbca8742f5b65378b39d9bec1722c424a Mon Sep 17 00:00:00 2001 From: Benjamin Schulz Date: Fri, 27 Oct 2023 18:06:45 +0200 Subject: [PATCH 1/2] Provide parent data when resolving with cloud function --- src/GraphQL/loaders/schemaDirectives.js | 4 ++++ src/Routers/FunctionsRouter.js | 10 +++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/GraphQL/loaders/schemaDirectives.js b/src/GraphQL/loaders/schemaDirectives.js index f354167317..bd4e0d0913 100644 --- a/src/GraphQL/loaders/schemaDirectives.js +++ b/src/GraphQL/loaders/schemaDirectives.js @@ -28,6 +28,10 @@ const load = parseGraphQLSchema => { auth, info, body: args, + parent: { + className: gqlInfo?.parentType, + ..._source, + }, }) ).response.result; } catch (e) { diff --git a/src/Routers/FunctionsRouter.js b/src/Routers/FunctionsRouter.js index d239908103..05fe3f60c7 100644 --- a/src/Routers/FunctionsRouter.js +++ b/src/Routers/FunctionsRouter.js @@ -112,8 +112,11 @@ export class FunctionsRouter extends PromiseRouter { }; } static handleCloudFunction(req) { - const functionName = req.params.functionName; - const applicationId = req.config.applicationId; + const { + params: { functionName }, + config: { applicationId }, + parent, + } = req; const theFunction = triggers.getFunction(functionName, applicationId); if (!theFunction) { @@ -122,7 +125,7 @@ export class FunctionsRouter extends PromiseRouter { let params = Object.assign({}, req.body, req.query); params = parseParams(params); const request = { - params: params, + params, master: req.auth && req.auth.isMaster, user: req.auth && req.auth.user, installationId: req.info.installationId, @@ -131,6 +134,7 @@ export class FunctionsRouter extends PromiseRouter { ip: req.config.ip, functionName, context: req.info.context, + parent, }; return new Promise(function (resolve, reject) { From c23fcb40f757639616d5da6d51d9915d7769613d Mon Sep 17 00:00:00 2001 From: Benjamin Schulz Date: Mon, 30 Oct 2023 11:22:18 +0100 Subject: [PATCH 2/2] Added test and always return className as string --- spec/ParseGraphQLServer.spec.js | 22 ++++++++++++++++++++++ src/GraphQL/loaders/schemaDirectives.js | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/spec/ParseGraphQLServer.spec.js b/spec/ParseGraphQLServer.spec.js index 493bd70f1c..4469e99944 100644 --- a/spec/ParseGraphQLServer.spec.js +++ b/spec/ParseGraphQLServer.spec.js @@ -10603,6 +10603,9 @@ describe('ParseGraphQLServer', () => { hello3: String! @mock(with: "Hello world!") hello4: User! @mock(with: { username: "somefolk" }) } + extend type User { + hasParentData: Boolean! @resolve + } `, }); parseGraphQLServer.applyGraphQL(expressApp); @@ -10704,6 +10707,25 @@ describe('ParseGraphQLServer', () => { }) ).toBeResolved(); }); + + it('provides parent data when resolving a field', async () => { + let parent; + Parse.Cloud.define('hasParentData', async req => { + parent = req.parent; + return true; + }); + await apolloClient.query({ + query: gql` + query { + hello4 { + hasParentData + } + } + `, + }); + expect(parent.className).toEqual('User'); + expect(parent.username).toEqual('somefolk'); + }); }); describe('GraphQL Schema Based', () => { diff --git a/src/GraphQL/loaders/schemaDirectives.js b/src/GraphQL/loaders/schemaDirectives.js index bd4e0d0913..00d15ac336 100644 --- a/src/GraphQL/loaders/schemaDirectives.js +++ b/src/GraphQL/loaders/schemaDirectives.js @@ -29,7 +29,7 @@ const load = parseGraphQLSchema => { info, body: args, parent: { - className: gqlInfo?.parentType, + className: String(gqlInfo.parentType), ..._source, }, })