|
| 1 | +# Visibility |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Visibility patterns allows fine-grained control over fields that are exposed by a schema, including introspection and handling requests. |
| 6 | + |
| 7 | +With directives `@materializer`, `@sequence`, `@supplies` and `@inject` fields can be resolved through resolution of other fields. |
| 8 | + |
| 9 | +For example, here the field `Customer.orders` is resolved by the resolution of `Query.orders`, using `@materializer`. |
| 10 | + |
| 11 | +```graphql |
| 12 | +type Query { |
| 13 | + customer(email: String!): Customer @rest(endpoint: "...") |
| 14 | + |
| 15 | + orders(customer_id: ID!): [Order] @dbquery(type: "postgresql") |
| 16 | +} |
| 17 | + |
| 18 | +type Customer { |
| 19 | + id: ID! |
| 20 | + name: String |
| 21 | + email: String |
| 22 | + orders: [Order] |
| 23 | + @materializer( |
| 24 | + query: "orders" |
| 25 | + arguments: { name: "customer_id", field: "id" } |
| 26 | + ) |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +(details for `@rest`, `@dbquery` omitted for clarity) |
| 31 | + |
| 32 | +In this case, we assume that the schema developer only wants to have clients obtain customer information through `Query.customer`, |
| 33 | +including their orders, that is they want to only have clients use the _graph_ defined by the schema (customers have orders). |
| 34 | + |
| 35 | +This means they do not want clients to make a request such as `{orders(customer_id:1) {date cost}}`, instead only |
| 36 | +obtain orders through `{ customer( email: "[email protected]") { name orders { date cost}}}`. |
| 37 | + |
| 38 | +While this can be achieved by field access policies, visibility provides a scoping mechanism for fields within the schema definition (`*.graphql` files) itself. |
| 39 | + |
| 40 | +And fields hidden by visibility are effectively not part of the external GraphQL schema and thus cannot be selected by a request |
| 41 | +or inspected using GraphQL introspection. |
| 42 | + |
| 43 | +Visibiilty is applied before field access policies, as field access policies apply to the external schema of an endpoint. |
| 44 | + |
| 45 | +> [!WARNING] |
| 46 | +> Visibility patterns are not applied for requests make with an admin key, thus the full schema definition from the `*.graphql` files is exposed. This is to aid debugging of schemas. |
| 47 | + |
| 48 | +## Visibility patterns |
| 49 | + |
| 50 | +Visibility is controlled through the directive argument `@sdl(visibility:)`. |
| 51 | + |
| 52 | +The visibility patterns apply only to the schema elements that are included through `@sdl(files:)`. |
| 53 | + |
| 54 | +For our example above we assume the schema is in `customer.graphl`, thus our `index.graphql` would look like: |
| 55 | + |
| 56 | +```graphql |
| 57 | +schema |
| 58 | + @sdl( |
| 59 | + files: "customer.graphql" |
| 60 | + visibility: { expose: true, types: "Query", fields: "customer" } |
| 61 | + ) { |
| 62 | + query: Query |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +Fields that match the pattern are defined using regular expressions in `types` and `fields`, that match type names and field names. |
| 67 | + |
| 68 | +Defaults match the style of field access policies in that: |
| 69 | + |
| 70 | +- Root operation type fields (`Query`, `Mutation`, `Subscription`) are not exposed by default. |
| 71 | +- All other fields in object and interface types are exposed by default. |
| 72 | + |
| 73 | +Thus in this simple example all fields in `Query` are not exposed with the exception of `Query.customer`. |
| 74 | + |
| 75 | +The external schema will only include `Query.customer` and and schema elements reachable from that field. |
| 76 | + |
| 77 | +> [!NOTE] |
| 78 | +> Any fields defined in this `index.graphql` are **not** subject to the visibility patterns, as patterns only apply to the schema elements that are included through files listed in `@sdl(files:)`. |
| 79 | +
|
| 80 | +## Consistent field naming |
| 81 | + |
| 82 | +Visibility patterns encourage a consistent naming policy for a GraphQL schema. |
| 83 | +For example using the prefix `_` for any "internal" field not to be exposed, can be enforced using a visibility pattern such as: |
| 84 | + |
| 85 | +```graphql |
| 86 | +schema |
| 87 | + @sdl( |
| 88 | + files: "customer.graphql" |
| 89 | + visibility: [ |
| 90 | + { expose: true, types: "Query", fields: "customer" } |
| 91 | + { expose: false, types: ".*", fields: "_.*" } # Any type, any field whose name starts with _ |
| 92 | + ] |
| 93 | + ) { |
| 94 | + query: Query |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +> [!TIP] |
| 99 | +> Double underscore `__` as a prefix is reserved for GraphQL introspection and is not allowed. |
| 100 | +
|
| 101 | +## Try it out |
| 102 | + |
| 103 | +Deploy the schema in this folder and then introspect the schema. |
| 104 | + |
| 105 | +This lists the fields in `Query` |
| 106 | + |
| 107 | +```graphql |
| 108 | +query { |
| 109 | + __schema { |
| 110 | + queryType { |
| 111 | + fields { |
| 112 | + name |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +The response is, showing `Query.orders` is not visible: |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + "data": { |
| 124 | + "__schema": { |
| 125 | + "description": "", |
| 126 | + "queryType": { |
| 127 | + "fields": [ |
| 128 | + { |
| 129 | + "name": "customer" |
| 130 | + } |
| 131 | + ] |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +You can verify the `Query.orders` cannot be selected: |
| 139 | + |
| 140 | +```graphql |
| 141 | +query { |
| 142 | + orders(customer_id: 1) { |
| 143 | + date |
| 144 | + when |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +results in: |
| 150 | + |
| 151 | +```json |
| 152 | +{ |
| 153 | + "errors": [ |
| 154 | + { |
| 155 | + "message": "Cannot query field \"orders\" on type \"Query\".", |
| 156 | + "locations": [ |
| 157 | + { |
| 158 | + "line": 1, |
| 159 | + "column": 9 |
| 160 | + } |
| 161 | + ] |
| 162 | + } |
| 163 | + ] |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +> [!NOTE] |
| 168 | +> If you see `Query.orders` then check if you are using the admin key in your request. |
0 commit comments