-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
215 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
'graphql-no-alias': major | ||
--- | ||
|
||
Implement imperative configuration | ||
|
||
With imperative configuration, there is no need for type definition and schema modification. | ||
|
||
```ts | ||
const permissions = { | ||
Query: { | ||
'*': 2, // default value for all queries | ||
getAnotherUser: 5 // custom value for specific query | ||
}, | ||
Mutation: { | ||
'*': 1 //default value for all mutations | ||
} | ||
} | ||
const { validation } = createValidation({ permissions }) | ||
|
||
const schema = buildSchema(/* GraphQL */ ` | ||
type Query { | ||
getUser: User | ||
getAnotherUser: User | ||
} | ||
type User { | ||
name: String | ||
} | ||
`) | ||
``` | ||
|
||
When the `permissions` key is passed to configuration, schema directives will be ignored. |
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,80 @@ | ||
import { buildSchema, GraphQLError, parse, validate } from 'graphql' | ||
import createValidation from '../' | ||
|
||
describe('Permissions via config', () => { | ||
test('Set default value for all queries', () => { | ||
const permissions = { | ||
Query: { | ||
'*': 1 | ||
} | ||
} | ||
const { validation } = createValidation({ permissions }) | ||
|
||
const schema = buildSchema(/* GraphQL */ ` | ||
type Query { | ||
getUser: User | ||
getAnotherUser: User | ||
} | ||
type User { | ||
name: String | ||
} | ||
`) | ||
|
||
const query = /* GraphQL */ ` | ||
{ | ||
getUser { | ||
name | ||
} | ||
alias_1: getUser { | ||
name | ||
} | ||
getAnotherUser { | ||
name | ||
} | ||
alias_1: getAnotherUser { | ||
name | ||
} | ||
} | ||
` | ||
const doc = parse(query) | ||
const errors = validate(schema, doc, [validation]) | ||
expect(errors).toHaveLength(2) | ||
}) | ||
|
||
test('Override default value for specific query call', () => { | ||
const permissions = { | ||
Query: { | ||
'*': 1, | ||
getAnotherUser: 2 | ||
} | ||
} | ||
const { validation } = createValidation({ permissions }) | ||
|
||
const schema = buildSchema(/* GraphQL */ ` | ||
type Query { | ||
getUser: User | ||
getAnotherUser: User | ||
} | ||
type User { | ||
name: String | ||
} | ||
`) | ||
|
||
const query = /* GraphQL */ ` | ||
{ | ||
getUser { | ||
name | ||
} | ||
getAnotherUser { | ||
name | ||
} | ||
alias_1: getAnotherUser { | ||
name | ||
} | ||
} | ||
` | ||
const doc = parse(query) | ||
const errors = validate(schema, doc, [validation]) | ||
expect(errors).toHaveLength(0) | ||
}) | ||
}) |
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