@graphql-tools/merge
Advanced tools
Comparing version 8.3.18 to 8.4.0-alpha-20230301043849-3d59d581
@@ -6,3 +6,3 @@ "use strict"; | ||
function mergeArguments(args1, args2, config) { | ||
const result = deduplicateArguments([...args2, ...args1].filter(utils_1.isSome)); | ||
const result = deduplicateArguments([...args2, ...args1].filter(utils_1.isSome), config); | ||
if (config && config.sort) { | ||
@@ -14,10 +14,13 @@ result.sort(utils_1.compareNodes); | ||
exports.mergeArguments = mergeArguments; | ||
function deduplicateArguments(args) { | ||
function deduplicateArguments(args, config) { | ||
return args.reduce((acc, current) => { | ||
const dup = acc.find(arg => arg.name.value === current.name.value); | ||
if (!dup) { | ||
const dupIndex = acc.findIndex(arg => arg.name.value === current.name.value); | ||
if (dupIndex === -1) { | ||
return acc.concat([current]); | ||
} | ||
else if (!(config === null || config === void 0 ? void 0 : config.reverseArguments)) { | ||
acc[dupIndex] = current; | ||
} | ||
return acc; | ||
}, []); | ||
} |
@@ -8,12 +8,5 @@ "use strict"; | ||
const arguments_js_1 = require("./arguments.js"); | ||
function fieldAlreadyExists(fieldsArr, otherField, config) { | ||
const result = fieldsArr.find(field => field.name.value === otherField.name.value); | ||
if (result && !(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) { | ||
const t1 = (0, utils_js_1.extractType)(result.type); | ||
const t2 = (0, utils_js_1.extractType)(otherField.type); | ||
if (t1.name.value !== t2.name.value) { | ||
throw new Error(`Field "${otherField.name.value}" already defined with a different type. Declared as "${t1.name.value}", but you tried to override with "${t2.name.value}"`); | ||
} | ||
} | ||
return !!result; | ||
function fieldAlreadyExists(fieldsArr, otherField) { | ||
const resultIndex = fieldsArr.findIndex(field => field.name.value === otherField.name.value); | ||
return [resultIndex > -1 ? fieldsArr[resultIndex] : null, resultIndex]; | ||
} | ||
@@ -27,18 +20,10 @@ function mergeFields(type, f1, f2, config) { | ||
for (const field of f1) { | ||
if (fieldAlreadyExists(result, field, config)) { | ||
const existing = result.find((f) => f.name.value === field.name.value); | ||
if (!(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) { | ||
if (config === null || config === void 0 ? void 0 : config.throwOnConflict) { | ||
preventConflicts(type, existing, field, false); | ||
} | ||
else { | ||
preventConflicts(type, existing, field, true); | ||
} | ||
if ((0, utils_js_1.isNonNullTypeNode)(field.type) && !(0, utils_js_1.isNonNullTypeNode)(existing.type)) { | ||
existing.type = field.type; | ||
} | ||
} | ||
existing.arguments = (0, arguments_js_1.mergeArguments)(field['arguments'] || [], existing.arguments || [], config); | ||
existing.directives = (0, directives_js_1.mergeDirectives)(field.directives, existing.directives, config); | ||
existing.description = field.description || existing.description; | ||
const [existing, existingIndex] = fieldAlreadyExists(result, field); | ||
if (existing && !(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) { | ||
const newField = ((config === null || config === void 0 ? void 0 : config.onFieldTypeConflict) && config.onFieldTypeConflict(existing, field, type, config === null || config === void 0 ? void 0 : config.throwOnConflict)) || | ||
preventConflicts(type, existing, field, config === null || config === void 0 ? void 0 : config.throwOnConflict); | ||
newField.arguments = (0, arguments_js_1.mergeArguments)(field['arguments'] || [], existing['arguments'] || [], config); | ||
newField.directives = (0, directives_js_1.mergeDirectives)(field.directives, existing.directives, config); | ||
newField.description = field.description || existing.description; | ||
result[existingIndex] = newField; | ||
} | ||
@@ -63,5 +48,16 @@ else { | ||
const bType = (0, utils_js_1.printTypeNode)(b.type); | ||
if (aType !== bType && !safeChangeForFieldType(a.type, b.type, ignoreNullability)) { | ||
throw new Error(`Field '${type.name.value}.${a.name.value}' changed type from '${aType}' to '${bType}'`); | ||
if (aType !== bType) { | ||
const t1 = (0, utils_js_1.extractType)(a.type); | ||
const t2 = (0, utils_js_1.extractType)(b.type); | ||
if (t1.name.value !== t2.name.value) { | ||
throw new Error(`Field "${b.name.value}" already defined with a different type. Declared as "${t1.name.value}", but you tried to override with "${t2.name.value}"`); | ||
} | ||
if (!safeChangeForFieldType(a.type, b.type, !ignoreNullability)) { | ||
throw new Error(`Field '${type.name.value}.${a.name.value}' changed type from '${aType}' to '${bType}'`); | ||
} | ||
} | ||
if ((0, utils_js_1.isNonNullTypeNode)(b.type) && !(0, utils_js_1.isNonNullTypeNode)(a.type)) { | ||
a.type = b.type; | ||
} | ||
return a; | ||
} | ||
@@ -68,0 +64,0 @@ function safeChangeForFieldType(oldType, newType, ignoreNullability = false) { |
import { compareNodes, isSome } from '@graphql-tools/utils'; | ||
export function mergeArguments(args1, args2, config) { | ||
const result = deduplicateArguments([...args2, ...args1].filter(isSome)); | ||
const result = deduplicateArguments([...args2, ...args1].filter(isSome), config); | ||
if (config && config.sort) { | ||
@@ -9,10 +9,13 @@ result.sort(compareNodes); | ||
} | ||
function deduplicateArguments(args) { | ||
function deduplicateArguments(args, config) { | ||
return args.reduce((acc, current) => { | ||
const dup = acc.find(arg => arg.name.value === current.name.value); | ||
if (!dup) { | ||
const dupIndex = acc.findIndex(arg => arg.name.value === current.name.value); | ||
if (dupIndex === -1) { | ||
return acc.concat([current]); | ||
} | ||
else if (!(config === null || config === void 0 ? void 0 : config.reverseArguments)) { | ||
acc[dupIndex] = current; | ||
} | ||
return acc; | ||
}, []); | ||
} |
@@ -5,12 +5,5 @@ import { extractType, isWrappingTypeNode, isListTypeNode, isNonNullTypeNode, printTypeNode } from './utils.js'; | ||
import { mergeArguments } from './arguments.js'; | ||
function fieldAlreadyExists(fieldsArr, otherField, config) { | ||
const result = fieldsArr.find(field => field.name.value === otherField.name.value); | ||
if (result && !(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) { | ||
const t1 = extractType(result.type); | ||
const t2 = extractType(otherField.type); | ||
if (t1.name.value !== t2.name.value) { | ||
throw new Error(`Field "${otherField.name.value}" already defined with a different type. Declared as "${t1.name.value}", but you tried to override with "${t2.name.value}"`); | ||
} | ||
} | ||
return !!result; | ||
function fieldAlreadyExists(fieldsArr, otherField) { | ||
const resultIndex = fieldsArr.findIndex(field => field.name.value === otherField.name.value); | ||
return [resultIndex > -1 ? fieldsArr[resultIndex] : null, resultIndex]; | ||
} | ||
@@ -24,18 +17,10 @@ export function mergeFields(type, f1, f2, config) { | ||
for (const field of f1) { | ||
if (fieldAlreadyExists(result, field, config)) { | ||
const existing = result.find((f) => f.name.value === field.name.value); | ||
if (!(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) { | ||
if (config === null || config === void 0 ? void 0 : config.throwOnConflict) { | ||
preventConflicts(type, existing, field, false); | ||
} | ||
else { | ||
preventConflicts(type, existing, field, true); | ||
} | ||
if (isNonNullTypeNode(field.type) && !isNonNullTypeNode(existing.type)) { | ||
existing.type = field.type; | ||
} | ||
} | ||
existing.arguments = mergeArguments(field['arguments'] || [], existing.arguments || [], config); | ||
existing.directives = mergeDirectives(field.directives, existing.directives, config); | ||
existing.description = field.description || existing.description; | ||
const [existing, existingIndex] = fieldAlreadyExists(result, field); | ||
if (existing && !(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) { | ||
const newField = ((config === null || config === void 0 ? void 0 : config.onFieldTypeConflict) && config.onFieldTypeConflict(existing, field, type, config === null || config === void 0 ? void 0 : config.throwOnConflict)) || | ||
preventConflicts(type, existing, field, config === null || config === void 0 ? void 0 : config.throwOnConflict); | ||
newField.arguments = mergeArguments(field['arguments'] || [], existing['arguments'] || [], config); | ||
newField.directives = mergeDirectives(field.directives, existing.directives, config); | ||
newField.description = field.description || existing.description; | ||
result[existingIndex] = newField; | ||
} | ||
@@ -59,5 +44,16 @@ else { | ||
const bType = printTypeNode(b.type); | ||
if (aType !== bType && !safeChangeForFieldType(a.type, b.type, ignoreNullability)) { | ||
throw new Error(`Field '${type.name.value}.${a.name.value}' changed type from '${aType}' to '${bType}'`); | ||
if (aType !== bType) { | ||
const t1 = extractType(a.type); | ||
const t2 = extractType(b.type); | ||
if (t1.name.value !== t2.name.value) { | ||
throw new Error(`Field "${b.name.value}" already defined with a different type. Declared as "${t1.name.value}", but you tried to override with "${t2.name.value}"`); | ||
} | ||
if (!safeChangeForFieldType(a.type, b.type, !ignoreNullability)) { | ||
throw new Error(`Field '${type.name.value}.${a.name.value}' changed type from '${aType}' to '${bType}'`); | ||
} | ||
} | ||
if (isNonNullTypeNode(b.type) && !isNonNullTypeNode(a.type)) { | ||
a.type = b.type; | ||
} | ||
return a; | ||
} | ||
@@ -64,0 +60,0 @@ function safeChangeForFieldType(oldType, newType, ignoreNullability = false) { |
{ | ||
"name": "@graphql-tools/merge", | ||
"version": "8.3.18", | ||
"version": "8.4.0-alpha-20230301043849-3d59d581", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
import { Config } from './merge-typedefs.js'; | ||
import { FieldDefinitionNode, InputValueDefinitionNode, NameNode } from 'graphql'; | ||
export declare function mergeFields<T extends FieldDefinitionNode | InputValueDefinitionNode>(type: { | ||
type FieldDefNode = FieldDefinitionNode | InputValueDefinitionNode; | ||
type NamedDefNode = { | ||
name: NameNode; | ||
}; | ||
export type OnFieldTypeConflict = (existingField: FieldDefNode, otherField: FieldDefNode, type: NamedDefNode, ignoreNullability: boolean | undefined) => FieldDefNode; | ||
export declare function mergeFields<T extends FieldDefNode>(type: { | ||
name: NameNode; | ||
}, f1: ReadonlyArray<T> | undefined, f2: ReadonlyArray<T> | undefined, config?: Config): T[]; | ||
export {}; |
import { DefinitionNode, DocumentNode, ParseOptions } from 'graphql'; | ||
import { CompareFn } from './utils.js'; | ||
import { GetDocumentNodeFromSchemaOptions, TypeSource } from '@graphql-tools/utils'; | ||
import { OnFieldTypeConflict } from './fields.js'; | ||
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; | ||
@@ -57,2 +58,19 @@ export interface Config extends ParseOptions, GetDocumentNodeFromSchemaOptions { | ||
ignoreFieldConflicts?: boolean; | ||
/** | ||
* Called if types of the same fields are different | ||
* | ||
* Default: false | ||
* | ||
* @example: | ||
* Given: | ||
* ```graphql | ||
* type User { a: String } | ||
* type User { a: Int } | ||
* ``` | ||
* | ||
* Instead of throwing `already defined with a different type` error, | ||
* `onFieldTypeConflict` function is called. | ||
*/ | ||
onFieldTypeConflict?: OnFieldTypeConflict; | ||
reverseArguments?: boolean; | ||
} | ||
@@ -59,0 +77,0 @@ /** |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
96264
1907
2