Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow constraining allowed relations in @whereConditions and @whereHasConditions #1896

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Added

- Allow constraining allowed relations in `@whereConditions` and `@whereHasConditions` https://github.com/nuwave/lighthouse/pull/1896

## v5.33.0

### Added
Expand Down Expand Up @@ -1185,7 +1189,7 @@ You can find and compare releases at the [GitHub release page](https://github.co
a [whereJsonContains filter
- Allow using callable classes with `__invoke` when referencing methods in directives
and when looking for default resolvers or type resolvers https://github.com/nuwave/lighthouse/issues/882
- Allow to restrict column names to a well-defined list in `@whereContraints`
- Allow to restrict column names to a well-defined enum in `@whereContraints`
and generate definitions for an `Enum` type and an `Input` type
that are restricted to the defined columns https://github.com/nuwave/lighthouse/pull/916
- Add test helpers for introspection queries to `MakesGraphQLRequests` https://github.com/nuwave/lighthouse/pull/916
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Benchmarks\\": "benchmarks"
"Benchmarks\\": "benchmarks",
"Tests\\": "tests/"
}
},
"config": {
Expand Down
4 changes: 2 additions & 2 deletions docs/3/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ type Query {
```

This is how you can use it to construct a complex query
that gets actors over age 37 who either have red hair or are at least 150cm.
that gets actors over age 37 who either have red hair or are at least 150 cm.

```graphql
{
Expand All @@ -2227,7 +2227,7 @@ that gets actors over age 37 who either have red hair or are at least 150cm.
{ column: "type", value: "Actor" }
{
OR: [
{ column: "haircolour", value: "red" }
{ column: "hair_color", value: "red" }
{ column: "height", operator: GTE, value: 150 }
]
}
Expand Down
8 changes: 4 additions & 4 deletions docs/4/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -1900,16 +1900,16 @@ Sort a result list by one or more given columns.
"""
directive @orderBy(
"""
Restrict the allowed column names to a well-defined list.
Restrict the allowed column names to a well-defined enum.
This improves introspection capabilities and security.
If not given, the column names can be passed as a String by clients.
Mutually exclusive with the `columnsEnum` argument.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allowes you to re-use the same enum for multiple fields.
Use an existing enum type to restrict the allowed columns to a well-defined enum.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with the `columns` argument.
"""
columnsEnum: String
Expand Down Expand Up @@ -1956,7 +1956,7 @@ enum SortOrder {
}
```

If you want to re-use a list of allowed columns, you can define your own enumeration type and use the `columnsEnum` argument instead of `columns`.
If you want to re-use a list of allowed columns, you can define your own enum type and use the `columnsEnum` argument instead of `columns`.
Here's an example of how you could define it in your schema:

```graphql
Expand Down
28 changes: 14 additions & 14 deletions docs/4/eloquent/complex-where-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ Add a dynamically client-controlled WHERE condition to a fields query.
"""
directive @whereConditions(
"""
Restrict the allowed column names to a well-defined list.
Restrict the allowed column names to a well-defined enum.
This improves introspection capabilities and security.
Mutually exclusive with the `columnsEnum` argument.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allowes you to re-use the same enum for multiple fields.
Use an existing enum type to restrict the allowed columns to a well-defined enum.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with the `columns` argument.
"""
columnsEnum: String
Expand All @@ -54,7 +54,7 @@ You can apply this directive on any field that performs an Eloquent query:
```graphql
type Query {
people(
where: _ @whereConditions(columns: ["age", "type", "haircolour", "height"])
where: _ @whereConditions(columns: ["age", "type", "hair_color", "height"])
): [Person!]! @all
}

Expand All @@ -63,7 +63,7 @@ type Person {
age: Int!
height: Int!
type: String!
hair_colour: String!
hair_color: String!
}
```

Expand Down Expand Up @@ -95,7 +95,7 @@ input PeopleWhereWhereConditions {
enum PeopleWhereColumn {
AGE @enum(value: "age")
TYPE @enum(value: "type")
HAIRCOLOUR @enum(value: "haircolour")
hair_color @enum(value: "hair_color")
HEIGHT @enum(value: "height")
}
```
Expand All @@ -117,7 +117,7 @@ type Query {
enum PersonColumn {
AGE @enum(value: "age")
TYPE @enum(value: "type")
HAIRCOLOUR @enum(value: "haircolour")
hair_color @enum(value: "hair_color")
HEIGHT @enum(value: "height")
}
```
Expand All @@ -127,7 +127,7 @@ Instead of creating enums for the allowed columns, it will simply use the existi

It is recommended to either use the `columns` or the `columnsEnum` argument.
When you don't define any allowed columns, clients can specify arbitrary column names as a `String`.
This approach should by taken with care, as it carries
This approach should be taken with care, as it carries
potential performance and security risks and offers little type safety.

A simple query for a person who is exactly 42 years old would look like this:
Expand All @@ -143,7 +143,7 @@ A simple query for a person who is exactly 42 years old would look like this:
Note that the operator defaults to `EQ` (`=`) if not given, so you could
also omit it from the previous example and get the same result.

The following query gets actors over age 37 who either have red hair or are at least 150cm:
The following query gets actors over age 37 who either have red hair or are at least 150 cm:

```graphql
{
Expand All @@ -154,7 +154,7 @@ The following query gets actors over age 37 who either have red hair or are at l
{ column: TYPE, value: "Actor" }
{
OR: [
{ column: HAIRCOLOUR, value: "red" }
{ column: hair_color, value: "red" }
{ column: HEIGHT, operator: GTE, value: 150 }
]
}
Expand All @@ -174,7 +174,7 @@ query gets people that have no hair and blue-ish eyes:
people(
where: {
AND: [
{ column: HAIRCOLOUR, operator: IS_NULL }
{ column: hair_color, operator: IS_NULL }
{ column: EYES, operator: IN, value: ["blue", "aqua", "turquoise"] }
]
}
Expand Down Expand Up @@ -213,15 +213,15 @@ directive @whereHasConditions(
relation: String

"""
Restrict the allowed column names to a well-defined list.
Restrict the allowed column names to a well-defined enum.
This improves introspection capabilities and security.
Mutually exclusive with the `columnsEnum` argument.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allowes you to re-use the same enum for multiple fields.
Use an existing enum type to restrict the allowed columns to a well-defined enum.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with the `columns` argument.
"""
columnsEnum: String
Expand Down
14 changes: 7 additions & 7 deletions docs/5/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -1998,16 +1998,16 @@ Sort a result list by one or more given columns.
"""
directive @orderBy(
"""
Restrict the allowed column names to a well-defined list.
Restrict the allowed column names to a well-defined enum.
This improves introspection capabilities and security.
Mutually exclusive with the `columnsEnum` argument.
Only used when the directive is added on an argument.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allowes you to re-use the same enum for multiple fields.
Use an existing enum type to restrict the allowed columns to a well-defined enum.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with the `columns` argument.
Only used when the directive is added on an argument.
"""
Expand Down Expand Up @@ -2052,20 +2052,20 @@ Options for the `relations` argument on `@orderBy`.
"""
input OrderByRelation {
"""
TODO: description
Name of the relation.
"""
relation: String!

"""
Restrict the allowed column names to a well-defined list.
Restrict the allowed column names to a well-defined enum.
This improves introspection capabilities and security.
Mutually exclusive with the `columnsEnum` argument.
"""
columns: [String!]

"""
Use an existing enumeration type to restrict the allowed columns to a predefined list.
This allowes you to re-use the same enum for multiple fields.
Use an existing enum type to restrict the allowed columns to a well-defined enum.
This allows you to re-use the same enum for multiple fields.
Mutually exclusive with the `columns` argument.
"""
columnsEnum: String
Expand Down
2 changes: 1 addition & 1 deletion docs/5/digging-deeper/ordering.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ You may pass more than one sorting option to add a secondary ordering.

### Reuse Columns Enum

To re-use a list of allowed columns, define your own enumeration type and use the `columnsEnum` argument instead of `columns`:
To re-use a list of allowed columns, define your own enum type and use the `columnsEnum` argument instead of `columns`:

```graphql
type Query {
Expand Down
Loading