Closed sgrove closed 7 years ago
I'll close this as I figured it out, and I'll submit a PR with a bit more detail on how the structure works in the README
Rather than address your question one by one, I hope the following example helps (I haven't try to compile this code and it blatantly disregards the issue you raised in #27):
(* Assumption *)
module TwitterAPI = sig
type tweet = {
id : int;
title : string;
children_ids : int list;
}
val get_tweet : int -> tweet option Lwt.t
end : struct
(* some implementation *)
end
(* Tweet object definition *)
let rec tweet = Schema.(obj "Tweet"
~fields:[
field "id"
~typ:(non_null int)
~args:Arg.[]
~resolver:(fun ctx tweet -> tweet.id)
;
field "title"
~typ:(non_null string)
~args:Arg.[]
~resolver:(fun ctx tweet -> tweet.title)
;
io_field "replies"
~typ:(non_null (list tweet))
~args:Arg.[]
~resolver:(fun ctx tweet ->
Lwt_list.map_p TwitterAPI.get_tweet tweet.children_ids
)
]
)
(* Schema definition *)
let schema = Schema.(schema
~fields:[
io_field "tweet"
~typ:tweet
~args:Arg.[
arg "tweet_id" ~typ:(non_null int)
]
~resolver:(fun ctx () tweet_id -> TwitterAPI.get_tweet tweet_id
]
)
Does that help? 😄
Indeed, that's great, thanks!
For example, if I'm modeling a Tweet:
id
,title
,children_ids
(simply a list of ids of reply-tweets) fieldsocaml-graphql-server
side, it hasid
,title
, andreplies
(a recursive list of tweets) fields.io_fields
to make the call against the Twitter API (but I'll only want to make one call to retrieve both fields)replies
, I need to see the result in order to make the subsequent calls and recursively fill out the replies. But I'm not sure how to get the list of children_ids from the result of the previous step.So my questions:
This is just an example (I'm not sure what Twitter's API actually looks like) I'm hoping to get some clarification on. It's been quite a bit of fun to poke at so far - thank you!