|
| 1 | +# Customisation |
| 2 | + |
| 3 | +Although we automtically generate a GraphQL schema based on your Parse Server database, we have provided a number of ways in which to configure and extend this schema. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +Whilst it's great to simply plug GraphQL into your Parse setup and immediately query any of your existing classes, you may find that this level of exposure is not suitable to your project. We have therefore provided a flexible way to limit which types, queries mutations are exposed within your GraphQL schema. |
| 8 | + |
| 9 | +### Configuration Options |
| 10 | + |
| 11 | +By default, no configuration is needed to get GraphQL working with your Parse Server. All of the following settings are completely optional, and can be provided or omitted as desired. To configure your schema, you simply need to provide a valid JSON object with the expected properties as described below: |
| 12 | + |
| 13 | +```typescript |
| 14 | +// The properties with ? are optional |
| 15 | + |
| 16 | +interface ParseGraphQLConfiguration { |
| 17 | + // All classes enabled by default |
| 18 | + // Provide an empty array to disable all classes |
| 19 | + enabledForClasses?: Array<string>; |
| 20 | + |
| 21 | + // Selectively disable specific classes |
| 22 | + disabledForClasses?: Array<string>; |
| 23 | + |
| 24 | + // Provide an array of per-class settings |
| 25 | + classConfigs?: Array<{ |
| 26 | + |
| 27 | + // You must provide a className |
| 28 | + // Only provide one config object per class |
| 29 | + className: string; |
| 30 | + |
| 31 | + type?: { |
| 32 | + |
| 33 | + // By default, all fields can be sent for |
| 34 | + // a create or update mutation. Use this |
| 35 | + // setting to limit to specific fields. |
| 36 | + inputFields?: { |
| 37 | + create?: Array<string>; |
| 38 | + update?: Array<string>; |
| 39 | + }; |
| 40 | + |
| 41 | + // By default, all fields can be resolved |
| 42 | + // on a get or find query. Use this to limit |
| 43 | + // which fields can be selected. |
| 44 | + outputFields?: Array<string>; |
| 45 | + |
| 46 | + // By default, all valid fields can be used |
| 47 | + // to filter a query. Use this to limit |
| 48 | + // which fields can be used to constrain a query. |
| 49 | + constraintFields?: Array<string>; |
| 50 | + |
| 51 | + // By default, all valid fields can be used |
| 52 | + // to sort the results of a query. Use this to |
| 53 | + // limit which fields can be used to sort a query |
| 54 | + // and which direction that sort can be set to. |
| 55 | + sortFields?: { |
| 56 | + field: string; |
| 57 | + asc: boolean; |
| 58 | + desc: boolean; |
| 59 | + }[]; |
| 60 | + }; |
| 61 | + |
| 62 | + // By default, a get and find query type is created |
| 63 | + // for all included classes. Use this to disable |
| 64 | + // the available query types for this class. |
| 65 | + query?: { |
| 66 | + get?: boolean; |
| 67 | + find?: boolean; |
| 68 | + }; |
| 69 | + |
| 70 | + // By default, all write mutation types are |
| 71 | + // exposed for all included classes. Use this to disable |
| 72 | + // the available mutation types for this class. |
| 73 | + mutation?: { |
| 74 | + create?: boolean; |
| 75 | + update?: boolean; |
| 76 | + destroy?: boolean; |
| 77 | + }; |
| 78 | + }> |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Set or Update Configuration |
| 83 | + |
| 84 | +We have provided a public API in `ParseGraphQLServer` which accepts the above JSON object for setting (and updating) your Parse GraphQL Configuration, `setGraphQLConfig`: |
| 85 | + |
| 86 | +```javascript |
| 87 | + const parseGraphQLServer = new ParseGraphQLServer(parseServer, { |
| 88 | + graphQLPath: parseServerConfig.graphQLPath, |
| 89 | + playgroundPath: parseServerConfig.playgroundPath |
| 90 | + }); |
| 91 | + |
| 92 | + const config = { |
| 93 | + // ... ParseGraphQLConfiguration |
| 94 | + }; |
| 95 | + |
| 96 | + await parseGraphQLServer.setGraphQLConfig(config); |
| 97 | +``` |
| 98 | + |
| 99 | +### Include or Exclude Classes |
| 100 | + |
| 101 | +By default, all of your Parse classes, including the defaults such as `Parse.User`, `Parse.Session`, `Parse.Role` are added to the schema. You can restrict this using the `enabledForClassess` or `disabledForClassess` options, which accepts an array of class names. |
| 102 | + |
| 103 | + |
| 104 | +In the following example, we limit our GraphQL schema to only expose the default `_User` class, along with a few custom classes: |
| 105 | + |
| 106 | +```javascript |
| 107 | +{ |
| 108 | + "enabledForClasses": ["_User", "Book", "Review", "Comment"], |
| 109 | + "disabledForClasses": null |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +In the following example, we limit our GraphQL schema by hiding some sensitive classes: |
| 114 | + |
| 115 | +```javascript |
| 116 | +{ |
| 117 | + // undefined or null results in the default behaviour, i.e. include all classes |
| 118 | + "enabledForClasses": undefined, |
| 119 | + // override the included classes by filtering out the following: |
| 120 | + "disabledForClasses": [ "UserSensitiveData", "ProductOrder", "Invoice" ] |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Input Types |
| 125 | + |
| 126 | +By default, we enrich the schema by generating a number of [Input Types](https://graphql.org/learn/schema/#input-types) for each class. This, as a healthy side-effect, improves development experience by providing type-completion and docs, though the true purpose is to define exactly what fields are exposed and useable per operation type. You can provide a `type` setting for any or each of your classes to limit which fields are exposed: |
| 127 | + |
| 128 | +In the following example, we have a custom class called `Review` where the fields `rating` and `body` are allowed on the `create` mutation, and the field `numberOfLikes` on the `update` mutation: |
| 129 | + |
| 130 | +```javascript |
| 131 | +{ |
| 132 | + "classConfigs": [ |
| 133 | + { |
| 134 | + "className": "Review", |
| 135 | + "type": { |
| 136 | + "inputFields": { |
| 137 | + "create": ["rating", "body"], |
| 138 | + "update": ["numberOfLikes"] |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + ] |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +You may decide to restrict which fields can be resolved when getting or finding records from a given class, for example, if you have a class called `Video` which includes a sensitive field `dmcaFlags`, you can hide this field by explicitly stating the fields that can be resolved: |
| 147 | + |
| 148 | +```javascript |
| 149 | +{ |
| 150 | + "classConfigs": [ |
| 151 | + { |
| 152 | + "className": "Video", |
| 153 | + "type": { |
| 154 | + "outputFields": ["name", "author", "numberOfViews", "comments", "cdnUrl"] |
| 155 | + } |
| 156 | + } |
| 157 | + ] |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +In production-grade environments where performance optimisation is critical, complete control over query filters and sortability is required to ensure that unindexed queries are not executed. For this reason, we provide a way to limit which fields can be used to constrain a query, and which fields (including the direction) can be used to sort that query. |
| 162 | + |
| 163 | + |
| 164 | +In the following example, we set the fields `name` and `age` as the only two that can be used to filter the `_User` class, and defining the `createdAt` and `age` fields the only sortable field whilst disabling the ascending direction on the `createdAt` field: |
| 165 | + |
| 166 | +```javascript |
| 167 | +{ |
| 168 | + "classConfigs": [ |
| 169 | + { |
| 170 | + "className": "_User", |
| 171 | + "type": { |
| 172 | + "constraintFields": ["name", "age"], |
| 173 | + "sortFields": [ |
| 174 | + { |
| 175 | + "field": "createdAt", |
| 176 | + "desc": true, |
| 177 | + "asc": false |
| 178 | + }, |
| 179 | + { |
| 180 | + "field": "age", |
| 181 | + "desc": true, |
| 182 | + "asc": true |
| 183 | + } |
| 184 | + ] |
| 185 | + } |
| 186 | + } |
| 187 | + ] |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +### Queries |
| 192 | + |
| 193 | +By default, the schema exposes a `get` and `find` operation for each class, for example, `get_User` and `find_User`. You can disable either of these for any class in your schema, like so: |
| 194 | + |
| 195 | + |
| 196 | +```javascript |
| 197 | +{ |
| 198 | + "classConfigs": [ |
| 199 | + { |
| 200 | + "className": "_User", |
| 201 | + "query": { |
| 202 | + "get": true, |
| 203 | + "find": false |
| 204 | + } |
| 205 | + }, |
| 206 | + { |
| 207 | + "className": "Review", |
| 208 | + "query": { |
| 209 | + "get": false, |
| 210 | + "find": true |
| 211 | + } |
| 212 | + } |
| 213 | + ] |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +### Mutations |
| 218 | + |
| 219 | +By default, the schema exposes a `create`, `update` and `delete` operation for each class, for example, `create_User`, `update_User` and `delete_User`. You can disable any of these mutations for any class in your schema, like so: |
| 220 | + |
| 221 | + |
| 222 | +```javascript |
| 223 | +{ |
| 224 | + "classConfigs": [ |
| 225 | + { |
| 226 | + "className": "_User", |
| 227 | + "mutation": { |
| 228 | + "create": true, |
| 229 | + "update": true, |
| 230 | + "destroy": true |
| 231 | + } |
| 232 | + }, |
| 233 | + { |
| 234 | + "className": "Review", |
| 235 | + "mutation": { |
| 236 | + "create": true, |
| 237 | + "update": false, |
| 238 | + "destroy": true |
| 239 | + } |
| 240 | + } |
| 241 | + ] |
| 242 | +} |
| 243 | +``` |
| 244 | + |
| 245 | +**Note**: the `delete` mutation setting key is named `destroy` to avoid issues due to `delete` being a javascript reserved word. |
0 commit comments