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

How to define unknown levels of nested fields #358

Closed
stevehu opened this issue Apr 15, 2016 · 3 comments
Closed

How to define unknown levels of nested fields #358

stevehu opened this issue Apr 15, 2016 · 3 comments

Comments

@stevehu
Copy link

stevehu commented Apr 15, 2016

For a multi-level comment system, you have reply of reply and the depth is different for each comment. In the spec, it looks like you have to define the exact structure of data you want to be returned; however, when you load a list of comments, you don't know if one of them have 10 levels of replies. Is there a way to workaround the current spec or the spec can be extended to support it?

@helfer
Copy link
Contributor

helfer commented Apr 15, 2016

I don't think there is, at least not in GraphQL. I think the best way to get the same result is to turn the comments tree into a list via a preorder-traversal. The comment should have an additional field called level (or something like that), which will tell the client how to display it.

@robrichard
Copy link
Contributor

I think you could do this by having each comment have a field to get child comments. You could write a query to get as many nested levels as you need for the initial read. I'm imagining a system like reddit where you can view x number of nested comments, then click a link to view more past that.

Implement fields as a function to allow for the circular dependency.

var Comment = new GraphQLObjectSchema({
    name: 'Comment',
    fields: () => ({
        id: GraphQLString,
        comments: new GraphQLList(Comment)
    })
})

Load three layers of comments for the initial view:

{
    post(id: 1) {
        comments {
            id
            comments {
                id
                comments {
                    id
                }
            }
        }
    }
}

load three more layers when user clicks "load more comments"

{
    comment(id: 2) {
        comments {
            id
            comments {
                id 
                comments {
                    id
                }
            }
        }
    }
}

@stevehu
Copy link
Author

stevehu commented Apr 19, 2016

It was discussed here and the case was closed.

@stevehu stevehu closed this as completed Apr 19, 2016
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

No branches or pull requests

3 participants