Skip to content

Commit

Permalink
Fix bug found after rebase and update jsdoc on 'retrieveAttributes'
Browse files Browse the repository at this point in the history
  • Loading branch information
howard-e committed Nov 27, 2023
1 parent 8dd8119 commit 7146600
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
14 changes: 9 additions & 5 deletions server/resolvers/helpers/deriveAttributesFromCustomField.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ const getChildPaths = (parent, fields) => {
});
};

const deriveAttributesFromCustomField = (modelName, customFields) => {
/**
* Returns the required database attributes to be called to support GraphQL field(s) being called
* @param {string} fieldName - The referenced field name defined in graphql-schema
* @param {string[]} customFields - Gathered graphql query field names to check if there is a matching database attribute
* @returns {{fields: *[], derived: *[]}|*[]}
*/
const deriveAttributesFromCustomField = (fieldName, customFields) => {
if (!customFields) return [];
const derived = [];
const fields = [
...customFields.map(({ value }) => value),
...customFields.flatMap(mapParentFn)
];
fields.push(...getChildPaths(modelName, fields));
fields.push(...getChildPaths(fieldName, fields));

switch (modelName) {
switch (fieldName) {
case 'testPlanVersion':
case 'latestTestPlanVersion': {
if (
Expand All @@ -60,8 +66,6 @@ const deriveAttributesFromCustomField = (modelName, customFields) => {
if (fields.includes('tester')) derived.push('testerUserId');
if (fields.includes('testPlanReport'))
derived.push('testPlanReportId');
if (fields.includes('testPlanReport'))
derived.push('testPlanReportId');
}
}

Expand Down
20 changes: 15 additions & 5 deletions server/resolvers/helpers/retrieveAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const deriveAttributesFromCustomField = require('./deriveAttributesFromCustomFie

const getAttributesFromSelections = (
selections,
modelName = null,
fieldName = null,
useSubpath = false
) => {
return (
useSubpath
? selections.filter(item => item.name.value === modelName)
? selections.filter(item => item.name.value === fieldName)
: selections
).map(({ name: { value }, selectionSet }) => {
return {
Expand All @@ -20,15 +20,25 @@ const getAttributesFromSelections = (
});
};

/**
* Used to return the ONLY database attributes a query may require to process a graphql query
* @param {string} fieldName - The referenced field name defined in graphql-schema. eg. testPlanReport, testPlanVersion,
* draftTestPlanRuns
* @param {string[]} modelAttributes - The 'known' database attributes the field could include
* @param {Array<FieldNode>} fieldNodes - Apollo GraphQL's fieldNodes included in a query's info object
* {see https://www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-arguments}
* @param {boolean} useSubpath - Flag to check attributes at increased depths of the query object
* @returns {{raw: *[], attributes: any[]}}
*/
const retrieveAttributes = (
modelName,
fieldName,
modelAttributes,
{ fieldNodes },
useSubpath = false
) => {
const attributes = getAttributesFromSelections(
fieldNodes[0].selectionSet.selections,
modelName,
fieldName,
useSubpath
);

Expand All @@ -44,7 +54,7 @@ const retrieveAttributes = (
});

const { fields, derived } = deriveAttributesFromCustomField(
modelName,
fieldName,
unknown
);

Expand Down
9 changes: 8 additions & 1 deletion server/resolvers/testPlanReportsResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const testPlanReportsResolver = async (
attributes: testPlanReportAttributes
} = retrieveAttributes('testPlanReport', TEST_PLAN_REPORT_ATTRIBUTES, info);

const { attributes: testPlanRunAttributes } = retrieveAttributes(
'draftTestPlanRuns',
TEST_PLAN_RUN_ATTRIBUTES,
info,
false
);

const { attributes: testPlanVersionAttributes } = retrieveAttributes(
'testPlanVersion',
TEST_PLAN_VERSION_ATTRIBUTES,
Expand Down Expand Up @@ -57,7 +64,7 @@ const testPlanReportsResolver = async (
null,
where,
testPlanReportAttributes,
TEST_PLAN_RUN_ATTRIBUTES,
testPlanRunAttributes,
testPlanVersionAttributes,
null,
null,
Expand Down

0 comments on commit 7146600

Please sign in to comment.