Skip to content

Commit

Permalink
Some tweaks to latest changes for cost spec
Browse files Browse the repository at this point in the history
  • Loading branch information
sachindshinde committed Aug 30, 2024
1 parent 351f80c commit 82376e6
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,19 @@ describe('composition of directive with non-trivial argument strategies', () =>
t: 2, k: 1, b: 4,
},
}, {
name: 'sum',
type: (schema: Schema) => new NonNullType(schema.intType()),
compositionStrategy: ARGUMENT_COMPOSITION_STRATEGIES.SUM,
argValues: {
s1: { t: 3, k: 1 },
s2: { t: 2, k: 5, b: 4 },
},
resultValues: {
t: 5, k: 6, b: 4,
},
}, {
// NOTE: See the note for the SUM strategy in argumentCompositionStrategies.ts
// for more information on why this is commented out.
// name: 'sum',
// type: (schema: Schema) => new NonNullType(schema.intType()),
// compositionStrategy: ARGUMENT_COMPOSITION_STRATEGIES.SUM,
// argValues: {
// s1: { t: 3, k: 1 },
// s2: { t: 2, k: 5, b: 4 },
// },
// resultValues: {
// t: 5, k: 6, b: 4,
// },
// }, {
name: 'intersection',
type: (schema: Schema) => new NonNullType(new ListType(new NonNullType(schema.stringType()))),
compositionStrategy: ARGUMENT_COMPOSITION_STRATEGIES.INTERSECTION,
Expand Down
1 change: 1 addition & 0 deletions internals-js/src/__tests__/values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,6 @@ describe('objectEquals tests', () => {
expect(valueEquals({ foo: 'foo', bar: undefined }, { foo: 'foo' })).toBe(
false,
);
expect(valueEquals({}, null)).toBe(false);
});
});
118 changes: 68 additions & 50 deletions internals-js/src/argumentCompositionStrategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,87 +13,105 @@ export type ArgumentCompositionStrategy = {
function supportFixedTypes(types: (schema: Schema) => InputType[]): TypeSupportValidator {
return (schema, type) => {
const supported = types(schema);
if (!supported.some((t) => sameType(t, type))) {
return { valid: false, supportedMsg: `type(s) ${supported.join(', ')}` };
}
return { valid: true };
return supported.some((t) => sameType(t, type))
? { valid: true }
: { valid: false, supportedMsg: `type(s) ${supported.join(', ')}` };

Check warning on line 18 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L17-L18

Added lines #L17 - L18 were not covered by tests
};
}

function supportAnyNonNullArray(): TypeSupportValidator {
return (_, type) => {
if (!isNonNullType(type) || !isListType(type.ofType)) {
return { valid: false, supportedMsg: 'non nullable list types of any type'};
}
return { valid: true };
return (_, type) => isNonNullType(type) && isListType(type.ofType)
? { valid: true }
: { valid: false, supportedMsg: 'non nullable list types of any type' }

Check warning on line 25 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L24-L25

Added lines #L24 - L25 were not covered by tests
}

function supportAnyArray(): TypeSupportValidator {
return (_, type) => isListType(type) || (isNonNullType(type) && isListType(type.ofType))
? { valid: true }
: { valid: false, supportedMsg: 'list types of any type' };

Check warning on line 31 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L30-L31

Added lines #L30 - L31 were not covered by tests
}

// NOTE: This function makes the assumption that for the directive argument
// being merged, it is not "nullable with non-null default" in the supergraph
// schema (this kind of type/default combo is confusing and should be avoided,
// if possible). This assumption allows this function to replace null with
// undefined, which makes for a cleaner supergraph schema.
function mergeNullableValues<T>(
mergeValues: (values: T[]) => T
): (values: (T | null | undefined)[]) => T | undefined {
return (values: (T | null | undefined)[]) => {
const nonNullValues = values.filter((v) => v !== null && v !== undefined) as T[];
return nonNullValues.length > 0
? mergeValues(nonNullValues)
: undefined;

Check warning on line 46 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
};
}

function unionValues(values: any[]): any {
return values.reduce((acc, next) => {
const newValues = next.filter((v1: any) => !acc.some((v2: any) => valueEquals(v1, v2)));
return acc.concat(newValues);

Check warning on line 53 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L50-L53

Added lines #L50 - L53 were not covered by tests
}, []);
}

export const ARGUMENT_COMPOSITION_STRATEGIES = {
MAX: {
name: 'MAX',
isTypeSupported: supportFixedTypes((schema: Schema) => [new NonNullType(schema.intType())]),
mergeValues: (values: any[]) => Math.max(...values),
mergeValues: (values: number[]) => Math.max(...values),

Check warning on line 61 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L61

Added line #L61 was not covered by tests
},
MIN: {
name: 'MIN',
isTypeSupported: supportFixedTypes((schema: Schema) => [new NonNullType(schema.intType())]),
mergeValues: (values: any[]) => Math.min(...values),
},
SUM: {
name: 'SUM',
isTypeSupported: supportFixedTypes((schema: Schema) => [new NonNullType(schema.intType())]),
mergeValues: (values: any[]) => values.reduce((acc, val) => acc + val, 0),
mergeValues: (values: number[]) => Math.min(...values),

Check warning on line 66 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L66

Added line #L66 was not covered by tests
},
// NOTE: This doesn't work today because directive applications are de-duped
// before being merged, we'd need to modify merge logic if we need this kind
// of behavior.
// SUM: {
// name: 'SUM',
// isTypeSupported: supportFixedTypes((schema: Schema) => [new NonNullType(schema.intType())]),
// mergeValues: (values: any[]) => values.reduce((acc, val) => acc + val, 0),
// },
INTERSECTION: {
name: 'INTERSECTION',
isTypeSupported: supportAnyNonNullArray(),
mergeValues: (values: any[]) => values.reduce((acc, val) => acc.filter((v1: any) => val.some((v2: any) => valueEquals(v1, v2))), values[0]),
mergeValues: (values: any[]) => values.reduce((acc, next) => {
if (acc === undefined) {
return next;
} else {
return acc.filter((v1: any) => next.some((v2: any) => valueEquals(v1, v2)));

Check warning on line 83 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L81-L83

Added lines #L81 - L83 were not covered by tests
}
}, undefined) ?? [],

Check warning on line 85 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L85

Added line #L85 was not covered by tests
},
UNION: {
name: 'UNION',
isTypeSupported: supportAnyNonNullArray(),
mergeValues: (values: any[]) =>
values.reduce((acc, val) => {
const newValues = val.filter((v1: any) => !acc.some((v2: any) => valueEquals(v1, v2)));
return acc.concat(newValues);
}, []),
mergeValues: unionValues,
},
NULLABLE_AND: {
name: 'NULLABLE_AND',
isTypeSupported: supportFixedTypes((schema: Schema) => [schema.booleanType()]),
mergeValues: (values: (boolean | null | undefined)[]) => values.reduce((acc, next) => {
if (acc === null || acc === undefined) {
return next;
} else if (next === null || next === undefined) {
return acc;
} else {
return acc && next;
}
}, undefined),
isTypeSupported: supportFixedTypes((schema: Schema) => [

Check warning on line 94 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L94

Added line #L94 was not covered by tests
schema.booleanType(),
new NonNullType(schema.booleanType())
]),
mergeValues: mergeNullableValues(
(values: boolean[]) => values.every((v) => v)

Check warning on line 99 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L99

Added line #L99 was not covered by tests
),
},
NULLABLE_MAX: {
name: 'NULLABLE_MAX',
isTypeSupported: supportFixedTypes((schema: Schema) => [schema.intType(), new NonNullType(schema.intType())]),
mergeValues: (values: any[]) => values.reduce((a: any, b: any) => a !== undefined && b !== undefined ? Math.max(a, b) : a ?? b, undefined),
isTypeSupported: supportFixedTypes((schema: Schema) => [

Check warning on line 104 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L104

Added line #L104 was not covered by tests
schema.intType(),
new NonNullType(schema.intType())
]),
mergeValues: mergeNullableValues(
(values: number[]) => Math.max(...values)

Check warning on line 109 in internals-js/src/argumentCompositionStrategies.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/argumentCompositionStrategies.ts#L109

Added line #L109 was not covered by tests
)
},
NULLABLE_UNION: {
name: 'NULLABLE_UNION',
isTypeSupported: (_: Schema, type: InputType) => ({ valid: isListType(type) }),
mergeValues: (values: any[]) => {
if (values.every((v) => v === undefined)) {
return undefined;
}

const combined = new Set();
for (const subgraphValues of values) {
if (Array.isArray(subgraphValues)) {
for (const value of subgraphValues) {
combined.add(value);
}
}
}
return Array.from(combined);
}
isTypeSupported: supportAnyArray(),
mergeValues: mergeNullableValues(unionValues),
}
}
8 changes: 4 additions & 4 deletions internals-js/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ export class CoreFeatures {
const feature = this.byAlias.get(splitted[0]);
return feature ? {
feature,
nameInFeature: splitted[1],
nameInFeature: splitted.slice(1).join('__'),
isImported: false,
} : undefined;
} else {
Expand All @@ -1076,7 +1076,7 @@ export class CoreFeatures {
if ((as ?? name) === importName) {
return {
feature,
nameInFeature: name.slice(1),
nameInFeature: isDirective ? name.slice(1) : name,
isImported: true,
};
}
Expand All @@ -1088,8 +1088,8 @@ export class CoreFeatures {
if (directFeature && isDirective) {
return {
feature: directFeature,
nameInFeature: directFeature.imports.find(imp => imp.as === `@${element.name}`)?.name.slice(1) ?? element.name,
isImported: true,
nameInFeature: element.name,
isImported: false,
};
}

Expand Down
Loading

0 comments on commit 82376e6

Please sign in to comment.