123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- import objectValues from '../polyfills/objectValues';
- import keyMap from '../jsutils/keyMap';
- import inspect from '../jsutils/inspect';
- import invariant from '../jsutils/invariant';
- import { print } from '../language/printer';
- import { visit } from '../language/visitor';
- import { type GraphQLSchema } from '../type/schema';
- import {
- type GraphQLField,
- type GraphQLType,
- type GraphQLInputType,
- type GraphQLNamedType,
- type GraphQLEnumType,
- type GraphQLUnionType,
- type GraphQLObjectType,
- type GraphQLInterfaceType,
- type GraphQLInputObjectType,
- isScalarType,
- isObjectType,
- isInterfaceType,
- isUnionType,
- isEnumType,
- isInputObjectType,
- isNonNullType,
- isListType,
- isNamedType,
- isRequiredArgument,
- isRequiredInputField,
- } from '../type/definition';
- import { astFromValue } from './astFromValue';
- export const BreakingChangeType = Object.freeze({
- TYPE_REMOVED: 'TYPE_REMOVED',
- TYPE_CHANGED_KIND: 'TYPE_CHANGED_KIND',
- TYPE_REMOVED_FROM_UNION: 'TYPE_REMOVED_FROM_UNION',
- VALUE_REMOVED_FROM_ENUM: 'VALUE_REMOVED_FROM_ENUM',
- REQUIRED_INPUT_FIELD_ADDED: 'REQUIRED_INPUT_FIELD_ADDED',
- INTERFACE_REMOVED_FROM_OBJECT: 'INTERFACE_REMOVED_FROM_OBJECT',
- FIELD_REMOVED: 'FIELD_REMOVED',
- FIELD_CHANGED_KIND: 'FIELD_CHANGED_KIND',
- REQUIRED_ARG_ADDED: 'REQUIRED_ARG_ADDED',
- ARG_REMOVED: 'ARG_REMOVED',
- ARG_CHANGED_KIND: 'ARG_CHANGED_KIND',
- DIRECTIVE_REMOVED: 'DIRECTIVE_REMOVED',
- DIRECTIVE_ARG_REMOVED: 'DIRECTIVE_ARG_REMOVED',
- REQUIRED_DIRECTIVE_ARG_ADDED: 'REQUIRED_DIRECTIVE_ARG_ADDED',
- DIRECTIVE_LOCATION_REMOVED: 'DIRECTIVE_LOCATION_REMOVED',
- });
- export const DangerousChangeType = Object.freeze({
- VALUE_ADDED_TO_ENUM: 'VALUE_ADDED_TO_ENUM',
- TYPE_ADDED_TO_UNION: 'TYPE_ADDED_TO_UNION',
- OPTIONAL_INPUT_FIELD_ADDED: 'OPTIONAL_INPUT_FIELD_ADDED',
- OPTIONAL_ARG_ADDED: 'OPTIONAL_ARG_ADDED',
- INTERFACE_ADDED_TO_OBJECT: 'INTERFACE_ADDED_TO_OBJECT',
- ARG_DEFAULT_VALUE_CHANGE: 'ARG_DEFAULT_VALUE_CHANGE',
- });
- export type BreakingChange = {
- type: $Keys<typeof BreakingChangeType>,
- description: string,
- ...
- };
- export type DangerousChange = {
- type: $Keys<typeof DangerousChangeType>,
- description: string,
- ...
- };
- export function findBreakingChanges(
- oldSchema: GraphQLSchema,
- newSchema: GraphQLSchema,
- ): Array<BreakingChange> {
- const breakingChanges = findSchemaChanges(oldSchema, newSchema).filter(
- change => change.type in BreakingChangeType,
- );
- return ((breakingChanges: any): Array<BreakingChange>);
- }
- export function findDangerousChanges(
- oldSchema: GraphQLSchema,
- newSchema: GraphQLSchema,
- ): Array<DangerousChange> {
- const dangerousChanges = findSchemaChanges(oldSchema, newSchema).filter(
- change => change.type in DangerousChangeType,
- );
- return ((dangerousChanges: any): Array<DangerousChange>);
- }
- function findSchemaChanges(
- oldSchema: GraphQLSchema,
- newSchema: GraphQLSchema,
- ): Array<BreakingChange | DangerousChange> {
- return [
- ...findTypeChanges(oldSchema, newSchema),
- ...findDirectiveChanges(oldSchema, newSchema),
- ];
- }
- function findDirectiveChanges(
- oldSchema: GraphQLSchema,
- newSchema: GraphQLSchema,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const directivesDiff = diff(
- oldSchema.getDirectives(),
- newSchema.getDirectives(),
- );
- for (const oldDirective of directivesDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.DIRECTIVE_REMOVED,
- description: `${oldDirective.name} was removed.`,
- });
- }
- for (const [oldDirective, newDirective] of directivesDiff.persisted) {
- const argsDiff = diff(oldDirective.args, newDirective.args);
- for (const newArg of argsDiff.added) {
- if (isRequiredArgument(newArg)) {
- schemaChanges.push({
- type: BreakingChangeType.REQUIRED_DIRECTIVE_ARG_ADDED,
- description: `A required arg ${newArg.name} on directive ${oldDirective.name} was added.`,
- });
- }
- }
- for (const oldArg of argsDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.DIRECTIVE_ARG_REMOVED,
- description: `${oldArg.name} was removed from ${oldDirective.name}.`,
- });
- }
- for (const location of oldDirective.locations) {
- if (newDirective.locations.indexOf(location) === -1) {
- schemaChanges.push({
- type: BreakingChangeType.DIRECTIVE_LOCATION_REMOVED,
- description: `${location} was removed from ${oldDirective.name}.`,
- });
- }
- }
- }
- return schemaChanges;
- }
- function findTypeChanges(
- oldSchema: GraphQLSchema,
- newSchema: GraphQLSchema,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const typesDiff = diff(
- objectValues(oldSchema.getTypeMap()),
- objectValues(newSchema.getTypeMap()),
- );
- for (const oldType of typesDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.TYPE_REMOVED,
- description: `${oldType.name} was removed.`,
- });
- }
- for (const [oldType, newType] of typesDiff.persisted) {
- if (isEnumType(oldType) && isEnumType(newType)) {
- schemaChanges.push(...findEnumTypeChanges(oldType, newType));
- } else if (isUnionType(oldType) && isUnionType(newType)) {
- schemaChanges.push(...findUnionTypeChanges(oldType, newType));
- } else if (isInputObjectType(oldType) && isInputObjectType(newType)) {
- schemaChanges.push(...findInputObjectTypeChanges(oldType, newType));
- } else if (isObjectType(oldType) && isObjectType(newType)) {
- schemaChanges.push(...findObjectTypeChanges(oldType, newType));
- } else if (isInterfaceType(oldType) && isInterfaceType(newType)) {
- schemaChanges.push(...findFieldChanges(oldType, newType));
- } else if (oldType.constructor !== newType.constructor) {
- schemaChanges.push({
- type: BreakingChangeType.TYPE_CHANGED_KIND,
- description:
- `${oldType.name} changed from ` +
- `${typeKindName(oldType)} to ${typeKindName(newType)}.`,
- });
- }
- }
- return schemaChanges;
- }
- function findInputObjectTypeChanges(
- oldType: GraphQLInputObjectType,
- newType: GraphQLInputObjectType,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const fieldsDiff = diff(
- objectValues(oldType.getFields()),
- objectValues(newType.getFields()),
- );
- for (const newField of fieldsDiff.added) {
- if (isRequiredInputField(newField)) {
- schemaChanges.push({
- type: BreakingChangeType.REQUIRED_INPUT_FIELD_ADDED,
- description: `A required field ${newField.name} on input type ${oldType.name} was added.`,
- });
- } else {
- schemaChanges.push({
- type: DangerousChangeType.OPTIONAL_INPUT_FIELD_ADDED,
- description: `An optional field ${newField.name} on input type ${oldType.name} was added.`,
- });
- }
- }
- for (const oldField of fieldsDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.FIELD_REMOVED,
- description: `${oldType.name}.${oldField.name} was removed.`,
- });
- }
- for (const [oldField, newField] of fieldsDiff.persisted) {
- const isSafe = isChangeSafeForInputObjectFieldOrFieldArg(
- oldField.type,
- newField.type,
- );
- if (!isSafe) {
- schemaChanges.push({
- type: BreakingChangeType.FIELD_CHANGED_KIND,
- description:
- `${oldType.name}.${oldField.name} changed type from ` +
- `${String(oldField.type)} to ${String(newField.type)}.`,
- });
- }
- }
- return schemaChanges;
- }
- function findUnionTypeChanges(
- oldType: GraphQLUnionType,
- newType: GraphQLUnionType,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const possibleTypesDiff = diff(oldType.getTypes(), newType.getTypes());
- for (const newPossibleType of possibleTypesDiff.added) {
- schemaChanges.push({
- type: DangerousChangeType.TYPE_ADDED_TO_UNION,
- description: `${newPossibleType.name} was added to union type ${oldType.name}.`,
- });
- }
- for (const oldPossibleType of possibleTypesDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.TYPE_REMOVED_FROM_UNION,
- description: `${oldPossibleType.name} was removed from union type ${oldType.name}.`,
- });
- }
- return schemaChanges;
- }
- function findEnumTypeChanges(
- oldType: GraphQLEnumType,
- newType: GraphQLEnumType,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const valuesDiff = diff(oldType.getValues(), newType.getValues());
- for (const newValue of valuesDiff.added) {
- schemaChanges.push({
- type: DangerousChangeType.VALUE_ADDED_TO_ENUM,
- description: `${newValue.name} was added to enum type ${oldType.name}.`,
- });
- }
- for (const oldValue of valuesDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.VALUE_REMOVED_FROM_ENUM,
- description: `${oldValue.name} was removed from enum type ${oldType.name}.`,
- });
- }
- return schemaChanges;
- }
- function findObjectTypeChanges(
- oldType: GraphQLObjectType,
- newType: GraphQLObjectType,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = findFieldChanges(oldType, newType);
- const interfacesDiff = diff(oldType.getInterfaces(), newType.getInterfaces());
- for (const newInterface of interfacesDiff.added) {
- schemaChanges.push({
- type: DangerousChangeType.INTERFACE_ADDED_TO_OBJECT,
- description: `${newInterface.name} added to interfaces implemented by ${oldType.name}.`,
- });
- }
- for (const oldInterface of interfacesDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.INTERFACE_REMOVED_FROM_OBJECT,
- description: `${oldType.name} no longer implements interface ${oldInterface.name}.`,
- });
- }
- return schemaChanges;
- }
- function findFieldChanges(
- oldType: GraphQLObjectType | GraphQLInterfaceType,
- newType: GraphQLObjectType | GraphQLInterfaceType,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const fieldsDiff = diff(
- objectValues(oldType.getFields()),
- objectValues(newType.getFields()),
- );
- for (const oldField of fieldsDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.FIELD_REMOVED,
- description: `${oldType.name}.${oldField.name} was removed.`,
- });
- }
- for (const [oldField, newField] of fieldsDiff.persisted) {
- schemaChanges.push(...findArgChanges(oldType, oldField, newField));
- const isSafe = isChangeSafeForObjectOrInterfaceField(
- oldField.type,
- newField.type,
- );
- if (!isSafe) {
- schemaChanges.push({
- type: BreakingChangeType.FIELD_CHANGED_KIND,
- description:
- `${oldType.name}.${oldField.name} changed type from ` +
- `${String(oldField.type)} to ${String(newField.type)}.`,
- });
- }
- }
- return schemaChanges;
- }
- function findArgChanges(
- oldType: GraphQLObjectType | GraphQLInterfaceType,
- oldField: GraphQLField<mixed, mixed>,
- newField: GraphQLField<mixed, mixed>,
- ): Array<BreakingChange | DangerousChange> {
- const schemaChanges = [];
- const argsDiff = diff(oldField.args, newField.args);
- for (const oldArg of argsDiff.removed) {
- schemaChanges.push({
- type: BreakingChangeType.ARG_REMOVED,
- description: `${oldType.name}.${oldField.name} arg ${oldArg.name} was removed.`,
- });
- }
- for (const [oldArg, newArg] of argsDiff.persisted) {
- const isSafe = isChangeSafeForInputObjectFieldOrFieldArg(
- oldArg.type,
- newArg.type,
- );
- if (!isSafe) {
- schemaChanges.push({
- type: BreakingChangeType.ARG_CHANGED_KIND,
- description:
- `${oldType.name}.${oldField.name} arg ${oldArg.name} has changed type from ` +
- `${String(oldArg.type)} to ${String(newArg.type)}.`,
- });
- } else if (oldArg.defaultValue !== undefined) {
- if (newArg.defaultValue === undefined) {
- schemaChanges.push({
- type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE,
- description: `${oldType.name}.${oldField.name} arg ${oldArg.name} defaultValue was removed.`,
- });
- } else {
-
-
-
- const oldValueStr = stringifyValue(oldArg.defaultValue, oldArg.type);
- const newValueStr = stringifyValue(newArg.defaultValue, newArg.type);
- if (oldValueStr !== newValueStr) {
- schemaChanges.push({
- type: DangerousChangeType.ARG_DEFAULT_VALUE_CHANGE,
- description: `${oldType.name}.${oldField.name} arg ${oldArg.name} has changed defaultValue from ${oldValueStr} to ${newValueStr}.`,
- });
- }
- }
- }
- }
- for (const newArg of argsDiff.added) {
- if (isRequiredArgument(newArg)) {
- schemaChanges.push({
- type: BreakingChangeType.REQUIRED_ARG_ADDED,
- description: `A required arg ${newArg.name} on ${oldType.name}.${oldField.name} was added.`,
- });
- } else {
- schemaChanges.push({
- type: DangerousChangeType.OPTIONAL_ARG_ADDED,
- description: `An optional arg ${newArg.name} on ${oldType.name}.${oldField.name} was added.`,
- });
- }
- }
- return schemaChanges;
- }
- function isChangeSafeForObjectOrInterfaceField(
- oldType: GraphQLType,
- newType: GraphQLType,
- ): boolean {
- if (isListType(oldType)) {
- return (
-
- (isListType(newType) &&
- isChangeSafeForObjectOrInterfaceField(
- oldType.ofType,
- newType.ofType,
- )) ||
-
- (isNonNullType(newType) &&
- isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType))
- );
- }
- if (isNonNullType(oldType)) {
-
- return (
- isNonNullType(newType) &&
- isChangeSafeForObjectOrInterfaceField(oldType.ofType, newType.ofType)
- );
- }
- return (
-
- (isNamedType(newType) && oldType.name === newType.name) ||
-
- (isNonNullType(newType) &&
- isChangeSafeForObjectOrInterfaceField(oldType, newType.ofType))
- );
- }
- function isChangeSafeForInputObjectFieldOrFieldArg(
- oldType: GraphQLType,
- newType: GraphQLType,
- ): boolean {
- if (isListType(oldType)) {
-
- return (
- isListType(newType) &&
- isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType.ofType)
- );
- }
- if (isNonNullType(oldType)) {
- return (
-
-
- (isNonNullType(newType) &&
- isChangeSafeForInputObjectFieldOrFieldArg(
- oldType.ofType,
- newType.ofType,
- )) ||
-
- (!isNonNullType(newType) &&
- isChangeSafeForInputObjectFieldOrFieldArg(oldType.ofType, newType))
- );
- }
-
- return isNamedType(newType) && oldType.name === newType.name;
- }
- function typeKindName(type: GraphQLNamedType): string {
- if (isScalarType(type)) {
- return 'a Scalar type';
- }
- if (isObjectType(type)) {
- return 'an Object type';
- }
- if (isInterfaceType(type)) {
- return 'an Interface type';
- }
- if (isUnionType(type)) {
- return 'a Union type';
- }
- if (isEnumType(type)) {
- return 'an Enum type';
- }
- if (isInputObjectType(type)) {
- return 'an Input type';
- }
-
- invariant(false, 'Unexpected type: ' + inspect((type: empty)));
- }
- function stringifyValue(value: mixed, type: GraphQLInputType): string {
- const ast = astFromValue(value, type);
- invariant(ast != null);
- const sortedAST = visit(ast, {
- ObjectValue(objectNode) {
- const fields = [...objectNode.fields].sort((fieldA, fieldB) =>
- fieldA.name.value.localeCompare(fieldB.name.value),
- );
- return { ...objectNode, fields };
- },
- });
- return print(sortedAST);
- }
- function diff<T: { name: string, ... }>(
- oldArray: $ReadOnlyArray<T>,
- newArray: $ReadOnlyArray<T>,
- ): {|
- added: Array<T>,
- removed: Array<T>,
- persisted: Array<[T, T]>,
- |} {
- const added = [];
- const removed = [];
- const persisted = [];
- const oldMap = keyMap(oldArray, ({ name }) => name);
- const newMap = keyMap(newArray, ({ name }) => name);
- for (const oldItem of oldArray) {
- const newItem = newMap[oldItem.name];
- if (newItem === undefined) {
- removed.push(oldItem);
- } else {
- persisted.push([oldItem, newItem]);
- }
- }
- for (const newItem of newArray) {
- if (oldMap[newItem.name] === undefined) {
- added.push(newItem);
- }
- }
- return { added, persisted, removed };
- }
|