Skip to content

Commit

Permalink
Add Dream.no_route, a zero of Dream
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Apr 28, 2021
1 parent 4ec1ef6 commit 37a27c4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
3 changes: 3 additions & 0 deletions example/i-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ If you'd like to do I/O or other asynchronous operations in your resolvers, use
[`io_field`](https://github.com/andreas/ocaml-graphql-server#lwt-support)
rather than `field`.

Use [`Dream.no_route`](https://aantron.github.io/dream/#val-no_route) to serve
GraphiQL conditionally, only during development.

See example [**`w-graphql-subscription`**](../w-graphql-subscription#files) for
an example with a GraphQL subscription.

Expand Down
60 changes: 38 additions & 22 deletions src/dream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,29 @@ val router : route list -> middleware
because, in the future, it may be possible to query routes for site
structure metadata. *)

val get : string -> handler -> route
(** Forwards [`GET] requests for the given path to the handler.
{[
Dream.get "/home" home_template
]} *)

val post : string -> handler -> route
val put : string -> handler -> route
val delete : string -> handler -> route
val head : string -> handler -> route
val connect : string -> handler -> route
val options : string -> handler -> route
val trace : string -> handler -> route
val patch : string -> handler -> route
(** Like {!Dream.get}, but for each of the other {{!type-method_} methods}. *)

val any : string -> handler -> route
(** Like {!Dream.get}, but does not check the method. *)

val not_found : handler
(** Always responds with [404 Not Found]. *)

(* :((( *)
val param : string -> request -> string
(** Retrieves the path parameter. If it is missing, {!Dream.param} raises an
Expand Down Expand Up @@ -1187,29 +1210,19 @@ val scope : string -> middleware list -> route list -> route
Scopes can be nested. *)

val get : string -> handler -> route
(** Forwards [`GET] requests for the given path to the handler.
val no_route : route
(** A dummy value of type {!type-route} that is completely ignored by the
router. Useful for disabling routes conditionally during application start:
{[
Dream.get "/home" home_template
Dream.router [
if development then
Dream.get "/graphiql" (Dream.graphiql "/graphql")
else
Dream.no_route;
]
]} *)

val post : string -> handler -> route
val put : string -> handler -> route
val delete : string -> handler -> route
val head : string -> handler -> route
val connect : string -> handler -> route
val options : string -> handler -> route
val trace : string -> handler -> route
val patch : string -> handler -> route
(** Like {!Dream.get}, but for each of the other {{!type-method_} methods}. *)

val any : string -> handler -> route
(** Like {!Dream.get}, but does not check the method. *)

val not_found : handler
(** Always responds with [404 Not Found]. *)



(** {1 Static files} *)
Expand Down Expand Up @@ -1426,8 +1439,8 @@ val graphql : (request -> 'a promise) -> 'a Graphql_lwt.Schema.schema -> handler
let () =
Dream.run
@@ Dream.router [
Dream.post "/graphql" (Dream.graphql Lwt.return schema);
Dream.get "/graphiql" (Dream.graphiql "/graphql");
Dream.any "/graphql" (Dream.graphql Lwt.return schema);
Dream.get "/graphiql" (Dream.graphiql "/graphql");
]
@@ Dream.not_found
]}
Expand Down Expand Up @@ -1461,7 +1474,10 @@ val graphiql : string -> handler
Dream's build of GraphiQL is found in the
{{:https://github.com/aantron/dream/tree/master/src/graphiql} src/graphiql}
directory. If you have the need, you can use it as the starting point for
your own customized GraphiQL. *)
your own customized GraphiQL.
Use {!Dream.no_route} to disable GraphiQL conditionally outside of
development. *)



Expand Down
3 changes: 3 additions & 0 deletions src/middleware/router.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ let patch pattern handler =
let any pattern handler =
[parse pattern, Handler (`Any, handler)]

let no_route =
[]

let rec apply middlewares routes =
let rec compose handler = function
| [] -> handler
Expand Down
1 change: 1 addition & 0 deletions src/middleware/router.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ val options : string -> Dream.handler -> route
val trace : string -> Dream.handler -> route
val patch : string -> Dream.handler -> route
val any : string -> Dream.handler -> route
val no_route : route

(* Route groups. *)
val scope : string -> Dream.middleware list -> route list -> route
Expand Down

0 comments on commit 37a27c4

Please sign in to comment.