graphql-norm
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -10,2 +10,4 @@ # Change Log | ||
- If denormalize() could not be fulfill a query, data will be `undefined` and `fields` will contain the first field that could not be resolved. | ||
## [1.2.0](https://github.com/dividab/graphql-norm/compare/v1.1.0...v1.2.0) - 2019-10-20 | ||
@@ -12,0 +14,0 @@ |
@@ -253,7 +253,13 @@ (function (global, factory) { | ||
const response = {}; | ||
let partial = false; | ||
const usedFieldsMap = {}; | ||
stack.push([rootFieldNode, "ROOT_QUERY", response, undefined]); | ||
stack.push([ | ||
rootFieldNode, | ||
"ROOT_QUERY", | ||
response, | ||
undefined, | ||
"ROOT_QUERY", | ||
"ROOT_QUERY" | ||
]); | ||
while (stack.length > 0) { | ||
const [fieldNode, idOrIdArray, parentObjectOrArray, parentResponseKey] = stack.pop(); | ||
const [fieldNode, idOrIdArray, parentObjectOrArray, parentResponseKey, parentNormKey, fieldNameInParent] = stack.pop(); | ||
// The stack has work items, depending on the work item we have four different cases to handle: | ||
@@ -273,4 +279,6 @@ // field + id + parentObject = denormalize(ID) => [responseObject, workitems] and parentObject[field] = responseObject | ||
if (normObj === undefined) { | ||
partial = true; | ||
break; | ||
return { | ||
data: undefined, | ||
fields: { [parentNormKey]: new Set([fieldNameInParent]) } | ||
}; | ||
} | ||
@@ -300,8 +308,8 @@ let usedFields = usedFieldsMap[key]; | ||
// Build key according to any arguments | ||
const key = field.arguments && field.arguments.length > 0 | ||
const fieldName = field.arguments && field.arguments.length > 0 | ||
? fieldNameWithArguments(field, variables) | ||
: field.name.value; | ||
// Add this to used fields | ||
usedFields.add(key); | ||
const normObjValue = normObj[key]; | ||
usedFields.add(fieldName); | ||
const normObjValue = normObj[fieldName]; | ||
if (normObjValue !== null && field.selectionSet) { | ||
@@ -313,3 +321,5 @@ // Put a work-item on the stack to build this field and set it on the response object | ||
responseObjectOrNewParentArray, | ||
(field.alias && field.alias.value) || field.name.value | ||
(field.alias && field.alias.value) || field.name.value, | ||
key, | ||
fieldName | ||
]); | ||
@@ -323,3 +333,6 @@ } | ||
else { | ||
partial = true; | ||
return { | ||
data: undefined, | ||
fields: { [key]: new Set([fieldName]) } | ||
}; | ||
} | ||
@@ -340,3 +353,5 @@ } | ||
responseObjectOrNewParentArray, | ||
i | ||
i, | ||
parentNormKey, | ||
fieldNameInParent | ||
]); | ||
@@ -359,3 +374,3 @@ } | ||
return { | ||
data: !partial ? data : undefined, | ||
data: data, | ||
fields: usedFieldsMap | ||
@@ -362,0 +377,0 @@ }; |
@@ -13,7 +13,13 @@ import { expandFragments, getDocumentDefinitions, fieldNameWithArguments, shouldIncludeField, defaultResolveType } from "./functions"; | ||
const response = {}; | ||
let partial = false; | ||
const usedFieldsMap = {}; | ||
stack.push([rootFieldNode, "ROOT_QUERY", response, undefined]); | ||
stack.push([ | ||
rootFieldNode, | ||
"ROOT_QUERY", | ||
response, | ||
undefined, | ||
"ROOT_QUERY", | ||
"ROOT_QUERY" | ||
]); | ||
while (stack.length > 0) { | ||
const [fieldNode, idOrIdArray, parentObjectOrArray, parentResponseKey] = stack.pop(); | ||
const [fieldNode, idOrIdArray, parentObjectOrArray, parentResponseKey, parentNormKey, fieldNameInParent] = stack.pop(); | ||
// The stack has work items, depending on the work item we have four different cases to handle: | ||
@@ -33,4 +39,6 @@ // field + id + parentObject = denormalize(ID) => [responseObject, workitems] and parentObject[field] = responseObject | ||
if (normObj === undefined) { | ||
partial = true; | ||
break; | ||
return { | ||
data: undefined, | ||
fields: { [parentNormKey]: new Set([fieldNameInParent]) } | ||
}; | ||
} | ||
@@ -60,8 +68,8 @@ let usedFields = usedFieldsMap[key]; | ||
// Build key according to any arguments | ||
const key = field.arguments && field.arguments.length > 0 | ||
const fieldName = field.arguments && field.arguments.length > 0 | ||
? fieldNameWithArguments(field, variables) | ||
: field.name.value; | ||
// Add this to used fields | ||
usedFields.add(key); | ||
const normObjValue = normObj[key]; | ||
usedFields.add(fieldName); | ||
const normObjValue = normObj[fieldName]; | ||
if (normObjValue !== null && field.selectionSet) { | ||
@@ -73,3 +81,5 @@ // Put a work-item on the stack to build this field and set it on the response object | ||
responseObjectOrNewParentArray, | ||
(field.alias && field.alias.value) || field.name.value | ||
(field.alias && field.alias.value) || field.name.value, | ||
key, | ||
fieldName | ||
]); | ||
@@ -83,3 +93,6 @@ } | ||
else { | ||
partial = true; | ||
return { | ||
data: undefined, | ||
fields: { [key]: new Set([fieldName]) } | ||
}; | ||
} | ||
@@ -100,3 +113,5 @@ } | ||
responseObjectOrNewParentArray, | ||
i | ||
i, | ||
parentNormKey, | ||
fieldNameInParent | ||
]); | ||
@@ -119,3 +134,3 @@ } | ||
return { | ||
data: !partial ? data : undefined, | ||
data: data, | ||
fields: usedFieldsMap | ||
@@ -122,0 +137,0 @@ }; |
{ | ||
"name": "graphql-norm", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Normalization and denormalization of GraphQL responses", | ||
@@ -5,0 +5,0 @@ "main": "dist/umd.js", |
@@ -169,3 +169,3 @@ # graphql-norm | ||
- **data**: This is the data for the query as it would have been returned from a GraphQL serverl, or `undefined` is the query could not be completely fulfilled from the data in `normMap`. | ||
- **fields**: An object where each property is an normlized object key, and the value is a `Set` of used fields. This can be useful for tracking which key/fields will affect this query data. If an tuple of this object and the data is stored, each time a new normalized result is merged a cache we can check if the new normalized data being merged contains any of the keys/fields of this query then it is affected by the merge, otherwise not. This is similar to the approach used [by relay](https://relay.dev/docs/en/thinking-in-graphql#achieving-view-consistency) for tracking changes. | ||
- **fields**: An object where each property is an normlized object key, and the value is a `Set` of used fields. This can be useful for tracking which key/fields will affect this query data. If an tuple of this object and the data is stored, each time a new normalized result is merged a cache we can check if the new normalized data being merged contains any of the keys/fields of this query then it is affected by the merge, otherwise not. This is similar to the approach used [by relay](https://relay.dev/docs/en/thinking-in-graphql#achieving-view-consistency) for tracking changes. If the query could not be fulfilled from cache, data will be `undefined` and `fields` will contain the first field that could not be resolved. | ||
@@ -172,0 +172,0 @@ ### merge() |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
78428
912