-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This version features new rule type `inputRule`. You can use `inputRule` to easily validated arguments provided to the function. Fixes #113 Fixes #262
- Loading branch information
Showing
9 changed files
with
256 additions
and
25 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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { IRules, IRule } from './types' | ||
export { shield } from './shield' | ||
export { rule, allow, deny, and, or, not } from './constructors' | ||
export { rule, inputRule, allow, deny, and, or, not } from './constructors' |
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,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`input rule schema validation works as expected 1`] = ` | ||
Array [ | ||
[GraphQLError: It has to be an email!], | ||
] | ||
`; |
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,65 @@ | ||
import { graphql } from 'graphql' | ||
import { applyMiddleware } from 'graphql-middleware' | ||
import { makeExecutableSchema } from 'graphql-tools' | ||
import { shield, inputRule } from '../src' | ||
|
||
describe('input rule', () => { | ||
test('schema validation works as expected', async () => { | ||
const typeDefs = ` | ||
type Query { | ||
hello: String! | ||
} | ||
type Mutation { | ||
login(email: String!): String | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => 'world', | ||
}, | ||
Mutation: { | ||
login: () => 'pass', | ||
}, | ||
} | ||
|
||
const schema = makeExecutableSchema({ typeDefs, resolvers }) | ||
|
||
// Permissions | ||
const permissions = shield({ | ||
Mutation: { | ||
login: inputRule(yup => | ||
yup.object({ | ||
email: yup | ||
.string() | ||
.email('It has to be an email!') | ||
.required(), | ||
}), | ||
), | ||
}, | ||
}) | ||
|
||
const schemaWithPermissions = applyMiddleware(schema, permissions) | ||
|
||
/* Execution */ | ||
|
||
const query = ` | ||
mutation { | ||
success: login(email: "[email protected]") | ||
failure: login(email: "notemail") | ||
} | ||
` | ||
const res = await graphql(schemaWithPermissions, query) | ||
|
||
console.log(res) | ||
|
||
/* Tests */ | ||
|
||
expect(res.data).toEqual({ | ||
success: 'pass', | ||
failure: null, | ||
}) | ||
expect(res.errors).toMatchSnapshot() | ||
}) | ||
}) |
Oops, something went wrong.