Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide parent data when resolving with cloud function #8795

Open
wants to merge 2 commits into
base: release-5.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/GraphQL/loaders/schemaDirectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const load = parseGraphQLSchema => {
auth,
info,
body: args,
parent: {
className: String(gqlInfo.parentType),
..._source,
},
})
).response.result;
} catch (e) {
Expand Down
10 changes: 7 additions & 3 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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) {
Expand Down