diff --git a/server/resolvers/helpers/deriveAttributesFromCustomField.js b/server/resolvers/helpers/deriveAttributesFromCustomField.js index 89711a635..d03381e15 100644 --- a/server/resolvers/helpers/deriveAttributesFromCustomField.js +++ b/server/resolvers/helpers/deriveAttributesFromCustomField.js @@ -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 ( @@ -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'); } } diff --git a/server/resolvers/helpers/retrieveAttributes.js b/server/resolvers/helpers/retrieveAttributes.js index 95f5b356d..58706523b 100644 --- a/server/resolvers/helpers/retrieveAttributes.js +++ b/server/resolvers/helpers/retrieveAttributes.js @@ -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 { @@ -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} 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 ); @@ -44,7 +54,7 @@ const retrieveAttributes = ( }); const { fields, derived } = deriveAttributesFromCustomField( - modelName, + fieldName, unknown ); diff --git a/server/resolvers/testPlanReportsResolver.js b/server/resolvers/testPlanReportsResolver.js index 43090c157..4900443c2 100644 --- a/server/resolvers/testPlanReportsResolver.js +++ b/server/resolvers/testPlanReportsResolver.js @@ -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, @@ -57,7 +64,7 @@ const testPlanReportsResolver = async ( null, where, testPlanReportAttributes, - TEST_PLAN_RUN_ATTRIBUTES, + testPlanRunAttributes, testPlanVersionAttributes, null, null,