Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created February 7, 2019 13:17
Show Gist options
  • Save fakenickels/f2cb1d850832c336304f7af9bcff1505 to your computer and use it in GitHub Desktop.
Save fakenickels/f2cb1d850832c336304f7af9bcff1505 to your computer and use it in GitHub Desktop.

try on curl to see curl
-X POST
-H "Content-Type: application/json"
--data '{ "query": "query { users { id, role } } " }'
http://localhost:3000/graphql

the most awesome thing about plyaing with graphql and ocaml is because they are really idiomatic together

let schema =
  Graphql_lwt.Schema.(
    schema([
      io_field(
        "users", 
        ~typ=non_null(list(non_null(user))), 
        ~args=Arg.[],
        ~resolve=( (info, ()) => Lwt.return( ( Ok( users ) ) ) )
      ),
    ])
  );

here we define a field users on graphql that should be a non nullable array of non nullable users so in the ~resolve function ocaml won't let you return "Null" in compile time so you can't deploy this kind of bug, it's impossible actually there are no "nulls" in ocaml you encode with a special kind of value called "variant" so in this case the variant is called option an option is defined as Some(value) | None yurijeanToday at 11:13 AM it returns this

gabriel.metalanguageToday at 11:13 AM yes, that's correct! you can take a look on SummarizeApp.re it's where the schema is initially defined you define upfront all the possible responses the server can reply with with the schema so it auto docs itself if you go to the playground

you can click there the docs are auto generated based on the schema you provided

let role =
  Schema.(
    enum(
      "role",
      ~doc="The role of a user",
      ~values=[
        enum_value("USER", ~value=User),
        enum_value("ADMIN", ~value=Admin),
      ],
    )
  );

let user =
  Schema.(
    obj("user", ~doc="A user in the system", ~fields=_ =>
      [
        field(
          "id",
          ~doc="Unique user identifier",
          ~typ=non_null(int),
          ~args=Arg.[],
          ~resolve=(info, p) =>
          p.id
        ),
        field(
          "name", 
          ~args=Arg.[],
          ~typ=non_null(string),
          ~resolve=(info, p) => p.name
        ),
        field(
          "role", 
          ~args=Arg.[],
          ~typ=non_null(role),
          ~resolve=(info, p) => p.role
        ),
      ]
    )
  );

in this case it's that https://github.com/Astrocoders/summhub-server/blob/master/executable/SummarizeApp.re

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment