github-pages-deploy-action/node_modules/graphql/utilities/findDeprecatedUsages.js.flow

59 lines
1.6 KiB
Plaintext
Raw Normal View History

2020-03-07 11:45:40 +08:00
// @flow strict
import { GraphQLError } from '../error/GraphQLError';
import { visit } from '../language/visitor';
import { type DocumentNode } from '../language/ast';
import { getNamedType } from '../type/definition';
import { type GraphQLSchema } from '../type/schema';
import { TypeInfo, visitWithTypeInfo } from './TypeInfo';
/**
* A validation rule which reports deprecated usages.
*
* Returns a list of GraphQLError instances describing each deprecated use.
*/
export function findDeprecatedUsages(
schema: GraphQLSchema,
ast: DocumentNode,
): Array<GraphQLError> {
const errors = [];
const typeInfo = new TypeInfo(schema);
visit(
ast,
visitWithTypeInfo(typeInfo, {
Field(node) {
const parentType = typeInfo.getParentType();
const fieldDef = typeInfo.getFieldDef();
if (parentType && fieldDef?.deprecationReason != null) {
errors.push(
new GraphQLError(
`The field "${parentType.name}.${fieldDef.name}" is deprecated. ` +
fieldDef.deprecationReason,
node,
),
);
}
},
EnumValue(node) {
const type = getNamedType(typeInfo.getInputType());
const enumVal = typeInfo.getEnumValue();
if (type && enumVal?.deprecationReason != null) {
errors.push(
new GraphQLError(
`The enum value "${type.name}.${enumVal.name}" is deprecated. ` +
enumVal.deprecationReason,
node,
),
);
}
},
}),
);
return errors;
}