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

Allow recursive object types #28

Merged
merged 1 commit into from
Feb 12, 2017
Merged

Allow recursive object types #28

merged 1 commit into from
Feb 12, 2017

Conversation

andreas
Copy link
Owner

@andreas andreas commented Feb 12, 2017

This PR adds the ability to define a recursive object type: an object with a field that refers to the type itself. An example would be a tweet, which has many replies (also tweets).

To achieve this, the fields of an object is now a function that receives the type itself and must return a list of fields. The new type of obj is thus:

val obj : name:string ->
          fields:(('ctx, 'src option) typ -> ('ctx, 'src) field list) -> (* <-- new *)
          ('ctx, 'src option) typ

Example:

type tweet = {
  id : int;
  replies : tweet list;
}

let tweet = Schema.(obj
  ~name:"tweet"
  ~fields:(fun tweet -> [ (* <-- new *)
    field "id"
      ~typ:(non_null int)
      ~args:Arg.[]
      ~resolver:(fun ctx t -> t.id)
    ;
    field "replies"
      ~typ:(non_null (list tweet))
      ~args:Arg.[]
      ~resolver:(fun ctx t -> t.replies)
  ])
)

Behind the scenes, this is achieved using let rec and lazy. Thanks to @Drup for pointing me in the right direction.

closes #27

@andreas andreas merged commit 8d4d8ab into master Feb 12, 2017
@andreas andreas deleted the recursive-objects branch February 12, 2017 11:49
@Drup
Copy link

Drup commented Feb 12, 2017

Nice. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to return recursive schema types
2 participants