package opium-graphql
Run GraphQL servers with Opium
Install
Dune Dependency
Authors
Maintainers
Sources
opium-0.20.0.tbz
sha256=326b91866de90baf535f8b7d4b2ff23e39d952e573c04b3c13f1054b59ff2fb6
sha512=59b83e7c8fe5f7ae328fb7f2343fe5b8fb735e8f6ee263cfd6c75bb179688ef7cf2b4586b35a2231ed3f3c1ada543021b7a4759326ae095eb77a5f38b9fa3a8a
doc/index.html
Opium GraphQL
opium-graphql
provides Opium handlers to serve GraphQL APIs.
Installation
To install opium-graphql
, you can add it to your dune-project
's package stanza:
(package (name my_app) (depends (ocaml (>= 4.08)) opium opium-graphql))
And let Dune generate the Opam file for you. Or you can add it directly to your .opam
file:
depends: [ "opium" "opium-graphql" ]
Usage
Here's a sample application that serves a schema with a node user
:
open Opium
module Schema = struct
open Graphql_lwt
type context = unit
type user =
{ id : int
; name : string
}
let users = [ { id = 1; name = "Alice" }; { id = 2; name = "Bob" } ]
let user : (context, user option) Graphql_lwt.Schema.typ =
Schema.(
obj "user" ~doc:"A user in the system" ~fields:(fun _ ->
[ field
"id"
~doc:"Unique user identifier"
~typ:(non_null int)
~args:Arg.[]
~resolve:(fun _info p -> p.id)
; field
"name"
~typ:(non_null string)
~args:Arg.[]
~resolve:(fun _info p -> p.name)
]))
;;
let schema =
Graphql_lwt.Schema.(
schema
[ field
"users"
~typ:(non_null (list (non_null user)))
~args:Arg.[]
~resolve:(fun _info () -> users)
])
;;
end
let graphql =
let handler = Opium_graphql.make_handler ~make_context:(fun _req -> ()) Schema.schema in
Opium.App.all "/" handler
;;
let graphiql =
let handler = Opium_graphql.graphiql_handler ~graphql_endpoint:"/" in
Opium.App.get "/graphiql" handler
;;
let _ =
Logs.set_reporter (Logs_fmt.reporter ());
Logs.set_level (Some Logs.Debug);
App.empty |> graphql |> graphiql |> App.run_command
;;
The example provides two endpoints:
/
that serves the actual GraphQL API/graphiql
that serves the GraphiQL tool
To test the API, you can go on the GraphiQL tool and run the following query:
query {
users {
name
}
}
API documentation
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
On This Page