Skip to content

Commit

Permalink
Merge pull request #135 from reindexio/validate-presense-of-User-type
Browse files Browse the repository at this point in the history
Make migrate ensure that User type is present
  • Loading branch information
freiksenet committed Oct 12, 2015
2 parents cf3be0a + 7c1bc89 commit fb720c9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
10 changes: 10 additions & 0 deletions db/migrations/__tests__/validateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ describe('validateSchema', () => {
]);
});

it('reports missing required types', () => {
const types = [
type('Foo'),
];
const errors = validateSchema({ types }, interfaces, ['User']);
assert.deepEqual(errors, [
'Expected User type to be present.',
]);
});

it('reports duplicated plural names', () => {
const types = [
type('Typo'),
Expand Down
19 changes: 18 additions & 1 deletion db/migrations/validateSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { getName, byName } from './utilities';
const InterfaceDefaultFields = getInterfaceDefaultFields();
const TypeDefaultFields = getTypeDefaultFields();

export default function validateSchema({ types }, interfaces) {
export default function validateSchema(
{ types },
interfaces,
requiredTypes = []
) {
const errors = [];
function invariant(...args) {
try {
Expand Down Expand Up @@ -54,6 +58,19 @@ export default function validateSchema({ types }, interfaces) {
return errors;
}

const typeNames = types.map(getName);
for (const requiredType of requiredTypes) {
invariant(
typeNames.includes(requiredType),
'Expected %s type to be present.',
requiredType
);
}

if (errors.length > 0) {
return errors;
}

const typesByName = byName(types);
types.forEach((type) =>
validateType(type, typesByName, interfaces, invariant)
Expand Down
4 changes: 3 additions & 1 deletion graphQL/mutations/createMigrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export default function createMigrate(typeSets, interfaces) {
context,
);

const errors = validateSchema({ types: input.types }, interfaces);
const errors = validateSchema({ types: input.types }, interfaces, [
'User',
]);

setImmediate(() => {
trackEvent(context.rootValue.credentials, 'pushed-schema', {
Expand Down

0 comments on commit fb720c9

Please sign in to comment.