Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create script to fetch company data from stackshare api #1572

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions script/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#use "topfind";;
#thread;;
#require "lwt";;
#require "cohttp-lwt-unix";;
#require "ezjsonm";;

open Lwt
open Cohttp
open Cohttp_lwt_unix
open Printf

let uri = "https://api.stackshare.io/graphql"
let key = "hmTBPOT6qizPthv9BVFjFg"

let query = {| query {
leads(usingToolSlugs: ["ocaml"], toolMatch: "any",
after:""){
edges{
node{
companyId
companyName
domain
}
}
}
}|}

let send_graphql_query query =
let open Lwt.Infix in
let headers =
Cohttp.Header.of_list ["Content-type", "application/json"; "Accept", "application/json"; "x-api-key" , key]
in
let uri = Uri.of_string uri in
let body =
`O [("query", `String query)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this piece of code doing?
and Why are we using Ezjsonm.to_string body if the above expression is already converting the query to string form.??

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this the json object is being constructed, so body is still an ezyjsonm type which is why you still need to convert body to a string type for sending the post request in line 38.

Also, if you do an opam install ocamlformat and run dune build @fmt --auto-promote . it will format the code so it's more legible. :)

in
let serialized_body = Ezjsonm.to_string body in
Cohttp_lwt_unix.Client.post ~headers ~body:(`String serialized_body) uri >>= fun (_resp, body) ->
Cohttp_lwt.Body.to_string body >|= fun s ->
print_endline s

let () =
Lwt_main.run (send_graphql_query query)