Skip to content

Commit c752260

Browse files
authored
feat(graphql): disable default mutations (#182)
Add an option to disable default mutations. Closes #170 All credit goes to @grncdr. This introduces a new CLI flag & option: --disable-default-mutations When default mutations are disabled, the only mutation operations available will be those exposed by user defined functions. * Use options objects all the way down This seems more consistent with the rest of the codebase * pass disableDefaultMutations option through BuildToken * typo * Update docs on library usage * Add integration test for disableDefaultMutations * Commit updated snapshots * Revert "Commit updated snapshots" This reverts commit 6db9086. * add new schema snapshot
1 parent 37a5619 commit c752260

9 files changed

Lines changed: 739 additions & 7 deletions

File tree

docs/library.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Arguments include:
4444
- **`options`**: An object containing other miscellaneous options. Options could be:
4545
- `classicIds`: Enables classic ids for Relay 1 support. Instead of using the field name `__id` for globally unique ids, PostGraphQL will instead use the field name `id` for its globally unique ids. This means that table `id` columns will also get renamed to `rowId`.
4646
- `dynamicJson`: Setting this to `true` enables dynamic JSON which will allow you to use any JSON as input and get any arbitrary JSON as output. By default JSON types are just a JSON string.
47+
- `disableDefaultMutations`: Setting this to `true` will prevent the creation of the default mutation types & fields. Database mutation will only be possible through Postgres functions.
4748
- `graphiql`: Set this to `true` to enable the GraphiQL interface.
4849
- `graphqlRoute`: The endpoint the GraphQL executer will listen on. Defaults to `/graphql`.
4950
- `graphiqlRoute`: The endpoint the GraphiQL query interface will listen on (**NOTE:** GraphiQL will not be enabled unless the `graphiql` option is set to `true`). Defaults to `/graphiql`.

src/graphql/schema/BuildToken.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ interface BuildToken {
2727
// By default, JSON is output as a string in our GraphQL queries. If true
2828
// then JSON will be output as a dynamic object.
2929
readonly dynamicJson: boolean,
30+
// If true then the default mutations for tables (e.g. createMyTable) will
31+
// not be created
32+
readonly disableDefaultMutations: boolean,
3033
},
3134
// Hooks for adding custom fields/types into our schema.
3235
readonly _hooks: _BuildTokenHooks,

src/graphql/schema/createGqlSchema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ export type SchemaOptions = {
1212
// If true then any literal will be accepted to this type and its output will
1313
// be plain JSON.
1414
dynamicJson?: boolean,
15+
// If true then the default mutations for tables (e.g. createMyTable) will
16+
// not be created
17+
disableDefaultMutations?: boolean,
1518
// Some hooks to allow extension of the schema. Currently this API is
1619
// private. Use at your own risk.
1720
_hooks?: _BuildTokenHooks,
@@ -33,6 +36,7 @@ export default function createGqlSchema (inventory: Inventory, options: SchemaOp
3336
options: {
3437
nodeIdFieldName: options.nodeIdFieldName || '__id',
3538
dynamicJson: options.dynamicJson || false,
39+
disableDefaultMutations: options.disableDefaultMutations || false,
3640
},
3741
_hooks: options._hooks || {},
3842
_typeOverrides: options._typeOverrides || new Map(),

src/graphql/schema/getMutationGqlType.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default getMutationGqlType
1818
* @private
1919
*/
2020
function createMutationGqlType (buildToken: BuildToken): GraphQLObjectType<mixed> | undefined {
21-
const { inventory } = buildToken
21+
const { inventory, options } = buildToken
2222

2323
// A list of all the mutations we are able to run.
2424
const mutationFieldEntries: Array<[string, GraphQLFieldConfig<mixed, mixed>]> = [
@@ -29,12 +29,14 @@ function createMutationGqlType (buildToken: BuildToken): GraphQLObjectType<mixed
2929
: []
3030
),
3131
...(
32-
// Get the mutations for all of our collections and creates mutations
33-
// for them.
34-
inventory
35-
.getCollections()
36-
.map(collection => createCollectionMutationFieldEntries(buildToken, collection))
37-
.reduce((a, b) => a.concat(b), [])
32+
options.disableDefaultMutations
33+
? []
34+
: // Get the mutations for all of our collections and creates mutations
35+
// for them.
36+
inventory
37+
.getCollections()
38+
.map(collection => createCollectionMutationFieldEntries(buildToken, collection))
39+
.reduce((a, b) => a.concat(b), [])
3840
),
3941
]
4042

0 commit comments

Comments
 (0)