Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): Added support for type and field validation.
+ I have introduced tightening option of no locals to keep the code as clean as possible. + This also adds nice error messages for developers when they apply rule to a type or field that doesn't exist.
- Loading branch information
Showing
6 changed files
with
116 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import test from 'ava' | ||
import { applyMiddleware } from 'graphql-middleware' | ||
import { makeExecutableSchema } from 'graphql-tools' | ||
import { ValidationError } from '../validation' | ||
import { shield, allow } from '../' | ||
|
||
test('Finds a type missing in schema and warns developer.', async t => { | ||
// Schema | ||
const typeDefs = ` | ||
type Query { | ||
a: String! | ||
} | ||
` | ||
const resolvers = { | ||
Query: { | ||
a: () => 'a', | ||
}, | ||
} | ||
|
||
const schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
}) | ||
|
||
// Permissions | ||
|
||
const permissions = shield({ | ||
Query: allow, | ||
Fail1: allow, | ||
Fail2: allow, | ||
}) | ||
|
||
t.throws( | ||
() => { | ||
applyMiddleware(schema, permissions) | ||
}, | ||
ValidationError, | ||
`It seems like you have applied rules to Fail1, Fail2 types but Shield cannot find them in your schema.`, | ||
) | ||
}) | ||
|
||
test('Finds the fields missing in schema and warns developer.', async t => { | ||
// Schema | ||
const typeDefs = ` | ||
type Query { | ||
a: String! | ||
} | ||
` | ||
const resolvers = { | ||
Query: { | ||
a: () => 'a', | ||
}, | ||
} | ||
|
||
const schema = makeExecutableSchema({ | ||
typeDefs, | ||
resolvers, | ||
}) | ||
|
||
// Permissions | ||
|
||
const permissions = shield({ | ||
Query: { | ||
a: allow, | ||
b: allow, | ||
c: allow, | ||
}, | ||
}) | ||
|
||
t.throws( | ||
() => { | ||
applyMiddleware(schema, permissions) | ||
}, | ||
ValidationError, | ||
`It seems like you have applied rules to Query.b, Query.c fields but Shield cannot find them in your schema.`, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class ValidationError extends Error { | ||
constructor(message: string) { | ||
super(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters