Skip to content

Commit

Permalink
Add serviceName info to new validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-scheer committed Jul 23, 2019
1 parent 8c1d07b commit e01e79a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe('value types integration tests', () => {
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_NO_ENTITY",
"message": "Value types cannot be entities (using the @key directive). Please ensure that one type extends the other and doesn't redefine the type, or remove the @key directive if this is not an entity.",
"message": "[serviceB] Product -> Value types cannot be entities (using the \`@key\` directive). Please ensure that the \`Product\` type is extended properly or remove the \`@key\` directive if this is not an entity.",
}
`);
});
Expand Down Expand Up @@ -366,11 +366,11 @@ describe('value types integration tests', () => {
const { errors } = composeAndValidate([serviceA, serviceB]);
expect(errors).toHaveLength(1);
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_FIELD_TYPE_MISMATCH",
"message": "Found field type mismatch on expected value type. 'Product.color' is defined as both a String and a String!. In order to define 'Product' in multiple places, the fields and their types must be identical.",
}
`);
Object {
"code": "VALUE_TYPE_FIELD_TYPE_MISMATCH",
"message": "[serviceA] Product.color -> Found field type mismatch on expected value type belonging to services \`serviceA\` and \`serviceB\`. \`Product.color\` is defined as both a String and a String!. In order to define \`Product\` in multiple places, the fields and their types must be identical.",
}
`);
});

it('on kind mismatch', () => {
Expand Down Expand Up @@ -405,11 +405,11 @@ describe('value types integration tests', () => {
const { errors } = composeAndValidate([serviceA, serviceB]);
expect(errors).toHaveLength(1);
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_KIND_MISMATCH",
"message": "Found kind mismatch on expected value type. 'Product' is defined as both a ObjectTypeDefinition and a InterfaceTypeDefinition. In order to define Product in multiple places, the kinds must be identical.",
}
`);
Object {
"code": "VALUE_TYPE_KIND_MISMATCH",
"message": "[serviceA] Product -> Found kind mismatch on expected value type belonging to services \`serviceA\` and \`serviceB\`. \`Product\` is defined as both a \`ObjectTypeDefinition\` and a \`InterfaceTypeDefinition\`. In order to define \`Product\` in multiple places, the kinds must be identical.",
}
`);
});

it('on union types mismatch', () => {
Expand Down Expand Up @@ -454,11 +454,11 @@ describe('value types integration tests', () => {
const { errors } = composeAndValidate([serviceA, serviceB]);
expect(errors).toHaveLength(1);
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_UNION_TYPES_MISMATCH",
"message": "The union 'Product' is defined in multiple places, however the unioned types do not match. Union types with the same name must also consist of identical types. The types Cabinet, Mattress are mismatched.",
}
`);
Object {
"code": "VALUE_TYPE_UNION_TYPES_MISMATCH",
"message": "[serviceA] Product -> The union \`Product\` is defined in services \`serviceA\` and \`serviceB\`, however their types do not match. Union types with the same name must also consist of identical types. The types Cabinet, Mattress are mismatched.",
}
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('MatchingUnions', () => {
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_UNION_TYPES_MISMATCH",
"message": "The union 'UPC' is defined in multiple places, however the unioned types do not match. Union types with the same name must also consist of identical types. The type Boolean is mismatched.",
"message": "[serviceA] UPC -> The union \`UPC\` is defined in services \`serviceA\` and \`serviceB\`, however their types do not match. Union types with the same name must also consist of identical types. The type Boolean is mismatched.",
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,17 @@ describe('UniqueTypeNamesWithFields', () => {
]);
expect(errors).toHaveLength(2);
expect(errors).toMatchInlineSnapshot(`
Array [
Object {
"code": "VALUE_TYPE_FIELD_TYPE_MISMATCH",
"message": "Found field type mismatch on expected value type. 'Product.sku' is defined as both a String! and a ID!. In order to define 'Product' in multiple places, the fields and their types must be identical.",
},
Object {
"code": "VALUE_TYPE_FIELD_TYPE_MISMATCH",
"message": "Found field type mismatch on expected value type. 'Product.quantity' is defined as both a Int! and a Int. In order to define 'Product' in multiple places, the fields and their types must be identical.",
},
]
`);
Array [
Object {
"code": "VALUE_TYPE_FIELD_TYPE_MISMATCH",
"message": "[serviceA] Product.sku -> Found field type mismatch on expected value type belonging to services \`serviceA\` and \`serviceB\`. \`Product.sku\` is defined as both a String! and a ID!. In order to define \`Product\` in multiple places, the fields and their types must be identical.",
},
Object {
"code": "VALUE_TYPE_FIELD_TYPE_MISMATCH",
"message": "[serviceA] Product.quantity -> Found field type mismatch on expected value type belonging to services \`serviceA\` and \`serviceB\`. \`Product.quantity\` is defined as both a Int! and a Int. In order to define \`Product\` in multiple places, the fields and their types must be identical.",
},
]
`);
});

it('object type definitions (overlapping fields, but non-value types)', () => {
Expand Down Expand Up @@ -337,11 +337,11 @@ describe('UniqueTypeNamesWithFields', () => {
UniqueTypeNamesWithFields,
]);
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_KIND_MISMATCH",
"message": "Found kind mismatch on expected value type. 'Product' is defined as both a ObjectTypeDefinition and a InputObjectTypeDefinition. In order to define Product in multiple places, the kinds must be identical.",
}
`);
Object {
"code": "VALUE_TYPE_KIND_MISMATCH",
"message": "[serviceA] Product -> Found kind mismatch on expected value type belonging to services \`serviceA\` and \`serviceB\`. \`Product\` is defined as both a \`ObjectTypeDefinition\` and a \`InputObjectTypeDefinition\`. In order to define \`Product\` in multiple places, the kinds must be identical.",
}
`);
});

it('value types cannot be entities (part 1)', () => {
Expand Down Expand Up @@ -371,7 +371,7 @@ describe('UniqueTypeNamesWithFields', () => {
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_NO_ENTITY",
"message": "Value types cannot be entities (using the @key directive). Please ensure that one type extends the other and doesn't redefine the type, or remove the @key directive if this is not an entity.",
"message": "[serviceA] Product -> Value types cannot be entities (using the \`@key\` directive). Please ensure that the \`Product\` type is extended properly or remove the \`@key\` directive if this is not an entity.",
}
`);
});
Expand Down Expand Up @@ -403,7 +403,7 @@ describe('UniqueTypeNamesWithFields', () => {
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_NO_ENTITY",
"message": "Value types cannot be entities (using the @key directive). Please ensure that one type extends the other and doesn't redefine the type, or remove the @key directive if this is not an entity.",
"message": "[serviceB] Product -> Value types cannot be entities (using the \`@key\` directive). Please ensure that the \`Product\` type is extended properly or remove the \`@key\` directive if this is not an entity.",
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GraphQLError, ASTVisitor, UnionTypeDefinitionNode } from 'graphql';
import { SDLValidationContext } from 'graphql/validation/ValidationContext';
import Maybe from 'graphql/tsutils/Maybe';
import xorBy from 'lodash.xorby';
import { errorWithCode } from '../../utils';
import { errorWithCode, logServiceAndType } from '../../utils';
import {
existedTypeNameMessage,
duplicateTypeNameMessage,
Expand Down Expand Up @@ -46,7 +46,14 @@ export function UniqueUnionTypes(context: SDLValidationContext): ASTVisitor {
context.reportError(
errorWithCode(
'VALUE_TYPE_UNION_TYPES_MISMATCH',
`The union '${typeName}' is defined in multiple places, however the unioned types do not match. Union types with the same name must also consist of identical types. The type${
`${logServiceAndType(
duplicateTypeNode.serviceName!,
typeName,
)}The union \`${typeName}\` is defined in services \`${
duplicateTypeNode.serviceName
}\` and \`${
node.serviceName
}\`, however their types do not match. Union types with the same name must also consist of identical types. The type${
diffLength > 1 ? 's' : ''
} ${unionDiff.map(diffEntry => diffEntry.name.value).join(', ')} ${
diffLength > 1 ? 'are' : 'is'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {

import { SDLValidationContext } from 'graphql/validation/ValidationContext';
import Maybe from 'graphql/tsutils/Maybe';
import { isTypeNodeAnEntity, diffTypeNodes, errorWithCode } from '../../utils';
import {
isTypeNodeAnEntity,
diffTypeNodes,
errorWithCode,
logServiceAndType,
} from '../../utils';

// Types of nodes this validator is responsible for
type TypesWithRequiredUniqueNames =
Expand Down Expand Up @@ -81,7 +86,19 @@ export function UniqueTypeNamesWithFields(
possibleErrors.push(
errorWithCode(
'VALUE_TYPE_FIELD_TYPE_MISMATCH',
`Found field type mismatch on expected value type. '${typeName}.${fieldName}' is defined as both a ${types[0]} and a ${types[1]}. In order to define '${typeName}' in multiple places, the fields and their types must be identical.`,
`${logServiceAndType(
duplicateTypeNode.serviceName!,
typeName,
fieldName,
)}Found field type mismatch on expected value type belonging to services \`${
duplicateTypeNode.serviceName
}\` and \`${
node.serviceName
}\`. \`${typeName}.${fieldName}\` is defined as both a ${
types[0]
} and a ${
types[1]
}. In order to define \`${typeName}\` in multiple places, the fields and their types must be identical.`,
[node, duplicateTypeNode],
),
);
Expand All @@ -101,18 +118,36 @@ export function UniqueTypeNamesWithFields(
context.reportError(
errorWithCode(
'VALUE_TYPE_KIND_MISMATCH',
`Found kind mismatch on expected value type. '${typeName}' is defined as both a ${kind[0]} and a ${kind[1]}. In order to define ${typeName} in multiple places, the kinds must be identical.`,
`${logServiceAndType(
duplicateTypeNode.serviceName!,
typeName,
)}Found kind mismatch on expected value type belonging to services \`${
duplicateTypeNode.serviceName
}\` and \`${
node.serviceName
}\`. \`${typeName}\` is defined as both a \`${
kind[0]
}\` and a \`${
kind[1]
}\`. In order to define \`${typeName}\` in multiple places, the kinds must be identical.`,
[node, duplicateTypeNode],
),
);
}

// Error if either is an entity
if (isTypeNodeAnEntity(node) || isTypeNodeAnEntity(duplicateTypeNode)) {
const entityNode = isTypeNodeAnEntity(duplicateTypeNode)
? duplicateTypeNode
: node;

context.reportError(
errorWithCode(
'VALUE_TYPE_NO_ENTITY',
`Value types cannot be entities (using the @key directive). Please ensure that one type extends the other and doesn't redefine the type, or remove the @key directive if this is not an entity.`,
`${logServiceAndType(
entityNode.serviceName!,
typeName,
)}Value types cannot be entities (using the \`@key\` directive). Please ensure that the \`${typeName}\` type is extended properly or remove the \`@key\` directive if this is not an entity.`,
[node, duplicateTypeNode],
),
);
Expand Down

0 comments on commit e01e79a

Please sign in to comment.