Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async _updateRequest(
request: string,
options: RequestOptions,
context: RequestContext,
): Promise {
const updatedRequest = options.fragments ? RequestParser._concatFragments(request, options.fragments) : request;
const ast = parse(updatedRequest);
const operationDefinitions = getOperationDefinitions(ast);
if (operationDefinitions.length > 1) {
return Promise.reject(new TypeError("@graphql-box/request-parser expected one operation, but got multiple."));
}
RequestParser._addOperationToContext(operationDefinitions, context);
const _this = this;
const typeInfo = new TypeInfo(this._schema);
let fragmentDefinitions: FragmentDefinitionNodeMap | undefined;
const variableTypes: VariableTypesMap = {};
try {
const updatedAST = visit(ast, {
enter(
node: ASTNode,
private _addFieldToNode(node: FieldNode | InlineFragmentNode, key: string): void {
if (!hasChildFields(node, key)) {
const mockAST = parse(`{${key}}`);
const queryNode = getOperationDefinitions(mockAST, QUERY)[0];
const fieldsAndTypeNames = getChildFields(queryNode, key);
if (!fieldsAndTypeNames) return;
const { fieldNode } = fieldsAndTypeNames[0];
addChildField(node, fieldNode, this._schema, key);
}
}
private async _setDataEntityAndRequestFieldPathCacheEntries(
requestData: RequestData,
responseData: ResponseDataForCaching,
options: RequestOptions,
context: RequestContext,
): Promise {
const operationNode = getOperationDefinitions(requestData.ast, context.operation)[0];
const fieldsAndTypeNames = getChildFields(operationNode);
if (!fieldsAndTypeNames) return;
await Promise.all(
fieldsAndTypeNames.map(({ fieldNode }) => {
return this._parseFieldDataEntityAndRequestFieldPathCacheEntryData(
fieldNode,
{ requestFieldPath: context.operation },
responseData,
options,
context,
);
}),
);
}
private _buildCacheMetadata(
{ ast }: RequestData,
{ data, ...otherProps }: RawResponseDataWithMaybeCacheMetadata,
options: RequestOptions,
context: RequestContext,
): CacheMetadata {
const cacheMetadata = this._createCacheMetadata({ data, ...otherProps }, context);
const queryNode = getOperationDefinitions(ast, context.operation)[0];
const fieldsAndTypeNames = getChildFields(queryNode);
if (!fieldsAndTypeNames) return cacheMetadata;
fieldsAndTypeNames.forEach(({ fieldNode }) =>
this._setFieldCacheability(
fieldNode,
{ requestFieldPath: context.operation },
{ cacheMetadata, data },
options,
context,
),
);
return cacheMetadata;
}
private _filterQuery(
{ ast }: RequestData,
{ fieldPathChecklist }: CachedResponseData,
context: RequestContext,
): void {
const queryNode = getOperationDefinitions(ast, context.operation)[0];
const fieldsAndTypeNames = getChildFields(queryNode);
if (!fieldsAndTypeNames) return;
for (let i = fieldsAndTypeNames.length - 1; i >= 0; i -= 1) {
const { fieldNode } = fieldsAndTypeNames[i];
const { requestFieldPath } = CacheManager._getFieldKeysAndPaths(fieldNode, {
requestFieldPath: context.operation,
});
if (this._filterField(fieldNode, fieldPathChecklist, requestFieldPath, context)) {
deleteChildFields(queryNode, fieldNode);
}
}
context.queryFiltered = true;