andreas/ocaml-graphql-server

Add a wrapper for all mutations

Closed this issue · 3 comments

Do you have recommendation for how to write a wrapper for all mutations?

We want to be able to pass a flag in the resolver that will disable all mutations. It comes in very handy in GraphiQL to prevent people from accidentally running mutations before they really understand what mutation are. We'd make them manually toggle a selector that turned mutations on.

Writing it for one mutation would look something like:

  field "mutationField" ~typ ~args
    ~resolve:(fun ctx  ->
                fun ()  ->
                  match ctx.readOnly with
                  | true  ->
                      throwError
                        "Refusing to run mutations in read-only mode"
                  | false  -> doMutation ())

Having a hard time writing a wrapper around field because the resolve function can take a variable number of arguments.

Would something like this work?

let mutation_field name ~args ~typ ~resolve =
  Schema.field name ~args ~typ ~resolve:(fun ctx ->
    if ctx.readOnly then
      failwith "readOnly"
    else
      resolve ctx
  )

Closing this one for now. Feel free to reopen if you need more input.

Would something like this work?

let mutation_field name ~args ~typ ~resolve =
  Schema.field name ~args ~typ ~resolve:(fun ctx ->
    if ctx.readOnly then
      failwith "readOnly"
    else
      resolve ctx
  )

@andreas, this would be helpful. Can we reopen the issue?