Skip to content

Commit 0cb74a0

Browse files
authored
Merge pull request #60 from andreas/lwt-server
Add Server.start to Graphql_lwt
2 parents aa12f4c + b581455 commit 0cb74a0

File tree

7 files changed

+72
-48
lines changed

7 files changed

+72
-48
lines changed

examples/jbuild

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(jbuild_version 1)
22

3-
(executables
3+
(executable
44
((libraries (cohttp.lwt graphql-lwt yojson))
5-
(names (server))))
5+
(name server)))
66

77
(alias
88
((name DEFAULT)

examples/server.ml

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,6 @@ let schema = Schema.(schema [
6969
]
7070
)
7171

72-
let json_err = function
73-
| Ok _ as ok -> ok
74-
| Error err -> Error (`String err)
75-
76-
let execute variables query =
77-
let open Lwt_result in
78-
Lwt.return @@ json_err @@ Graphql_parser.parse query >>= fun doc ->
79-
Schema.execute schema () ~variables doc
80-
81-
let callback conn (req : Cohttp.Request.t) body =
82-
Lwt_io.printf "Req: %s\n" req.resource;
83-
match (req.meth, req.resource) with
84-
| `GET, _ -> C.Server.respond_file "./index.html" ()
85-
| `POST, _ ->
86-
begin
87-
Cohttp_lwt_body.to_string body >>= fun query_json ->
88-
Lwt_io.printf "Body: %s\n" query_json;
89-
let query = Yojson.Basic.(from_string query_json |> Util.member "query" |> Util.to_string)
90-
in
91-
let variables =
92-
try
93-
Yojson.Basic.(from_string query_json |> Util.member "variables" |> Util.to_assoc)
94-
with _ -> []
95-
in
96-
Lwt_io.printf "Query: %s\n" query;
97-
execute (variables :> (string * Graphql_parser.const_value) list) query >>= function
98-
| Ok data ->
99-
let body = Yojson.Basic.to_string data in
100-
C.Server.respond_string ~status:`OK ~body ()
101-
| Error err ->
102-
let body = Yojson.Basic.to_string err in
103-
C.Server.respond_error ~body ()
104-
end
105-
| _ -> C.Server.respond_string ~status:`Not_found ~body:"" ()
106-
10772
let () =
108-
C.Server.create ~mode:(`TCP (`Port 8080)) (C.Server.make ~callback ())
73+
Server.start ~ctx:(fun () -> ()) schema
10974
|> Lwt_main.run

graphql-lwt.opam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ depends: [
1212
"graphql"
1313
"alcotest" {test}
1414
"lwt"
15+
"cohttp-lwt-unix" {>= "0.99"}
16+
"crunch"
1517
]
1618
available: [
1719
ocaml-version >= "4.03.0"

examples/index.html renamed to graphql-lwt/src/assets/index.html

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@
5757
otherParams[k] = parameters[k];
5858
}
5959
}
60-
var fetchURL = "http://localhost:8080";
61-
6260
// Defines a GraphQL fetcher using the fetch API.
6361
function graphQLFetcher(graphQLParams) {
64-
return fetch(fetchURL, {
62+
return fetch(window.location, {
6563
method: 'post',
6664
headers: {
6765
'Accept': 'application/json',
@@ -107,10 +105,10 @@
107105
onEditQuery: onEditQuery,
108106
onEditVariables: onEditVariables,
109107
onEditOperationName: onEditOperationName,
110-
query: null,
111-
response: null,
112-
variables: null,
113-
operationName: null,
108+
query: parameters.query,
109+
response: parameters.response,
110+
variables: parameters.variables,
111+
operationName: parameters.operationName,
114112
}),
115113
document.body
116114
);

graphql-lwt/src/graphql_lwt.ml

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,52 @@
1-
module Schema = Graphql_schema.Make(Lwt)
1+
open Graphql
2+
3+
module Schema = Graphql_schema.Make(Lwt)
4+
5+
module Server = struct
6+
module C = Cohttp_lwt_unix
7+
open Lwt
8+
9+
let static_file_response ?(encoding=`None) path =
10+
match Assets.read path with
11+
| Some body -> C.Server.respond_string ~status:`OK ~body ()
12+
| None -> C.Server.respond_string ~status:`Not_found ~body:"" ()
13+
14+
let json_err = function
15+
| Ok _ as ok -> ok
16+
| Error err -> Error (`String err)
17+
18+
let execute_query ctx schema variables query =
19+
let open Lwt_result in
20+
Lwt.return @@ json_err @@ Graphql_parser.parse query >>= fun doc ->
21+
Schema.execute schema ctx ~variables doc
22+
23+
let execute_request ctx schema req body =
24+
Cohttp_lwt_body.to_string body >>= fun body' ->
25+
Lwt_io.printf "Body: %s\n" body';
26+
let json = Yojson.Basic.from_string body' in
27+
let query = Yojson.Basic.(json |> Util.member "query" |> Util.to_string) in
28+
let variables = try Yojson.Basic.Util.(json |> member "variables" |> to_assoc) with _ -> [] in
29+
Lwt_io.printf "Query: %s\n" query;
30+
let result = execute_query ctx schema (variables :> (string * Graphql_parser.const_value) list) query in
31+
result >>= function
32+
| Ok data ->
33+
let body = Yojson.Basic.to_string data in
34+
C.Server.respond_string ~status:`OK ~body ()
35+
| Error err ->
36+
let body = Yojson.Basic.to_string err in
37+
C.Server.respond_error ~body ()
38+
39+
let mk_callback mk_context schema conn (req : Cohttp.Request.t) body =
40+
Lwt_io.printf "Req: %s\n" req.resource;
41+
let req_path = Cohttp.Request.uri req |> Uri.path in
42+
let path_parts = Str.(split (regexp "/") req_path) in
43+
match req.meth, path_parts with
44+
| `GET, ["graphql"] -> static_file_response "index.html"
45+
| `GET, ["graphql"; path] -> static_file_response path
46+
| `POST, ["graphql"] -> execute_request (mk_context ()) schema req body
47+
| _ -> C.Server.respond_string ~status:`Not_found ~body:"" ()
48+
49+
let start ?(port=8080) ~ctx schema =
50+
let callback = mk_callback ctx schema in
51+
C.Server.create ~mode:(`TCP (`Port port)) (C.Server.make ~callback ())
52+
end

graphql-lwt/src/graphql_lwt.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
module Schema : sig
33
include Graphql_intf.Schema with type 'a io = 'a Lwt.t
44
end
5+
6+
module Server : sig
7+
val start : ?port:int -> ctx:(unit -> 'ctx) -> 'ctx Schema.schema -> unit Lwt.t
8+
end

graphql-lwt/src/jbuild

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
(jbuild_version 1)
22

3+
(rule
4+
((targets (assets.ml))
5+
(deps ((files_recursively_in assets)))
6+
(action (run ${bin:ocaml-crunch} -m plain assets -o assets.ml))))
7+
38
(library
49
((name graphql_lwt)
510
(public_name graphql-lwt)
611
(wrapped false)
7-
(preprocess (pps (ppx_sexp_conv)))
8-
(libraries (graphql lwt))))
12+
(libraries (str graphql lwt cohttp.lwt))))

0 commit comments

Comments
 (0)