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

[WIP] Connections support #171

Closed
wants to merge 1 commit into from
Closed

[WIP] Connections support #171

wants to merge 1 commit into from

Conversation

orta
Copy link
Contributor

@orta orta commented Oct 17, 2018

Hi there! I'm not yet at implementation stage for this, but I do want to try and spec out what the client code would look like (as that's the easiest to discuss and the biggest variable for me)

To do connections right we'll need to be able to:

  • Define a ConnectionType (and allow subclassing with new root fields)
  • Define a connection query (to ensure the 4 inputs that have to be there are in, but also to allow expanding with your own args)
  • Define a custom edge in your connection (to allow adding metadata to the connection)
  • Define the node for your connection (That's kinda the entire point of a connection)

So me + @l2succes tried speccing out two versions of how you could set it up. One with all the defaults, the simplest case. The other with every possible change. We have a mix of both in our existing codebases, so most usage falls somewhere in-between.

What do you think?

…ions with type-graphql

Signed-off-by: Luc Sucèss <luc;@artsymail.com>
@MichalLytek
Copy link
Owner

MichalLytek commented Oct 19, 2018

Hi! 👋
This looks good, I'm glad that you like the Recipe example 😄

The problem is with generic types that uses reflection and decorators. You can't just provide T type to the decorator, and the syntax like:

class Edge<NodeType = {}> {
  @Field()
  node: NodeType;
  @Field()
  cursor: string;
 }

just won't work - reflection says that type of node property is Object. So I think that the @ConnectionType decorator is only to provide the node and edgeType info, right?

To achieve generic types with decorators, you have to create higher order class - a function that will take the runtime class as parameters and return new class, just like with resolvers classes:
https://19majkel94.github.io/type-graphql/docs/interfaces-and-inheritance.html#resolvers-inheritance

It's not a big deal but we have to add the isAbstract option to prevent emitting it in schema (or maybe it just works? I don't know honestly 😄). And then you just call it in a slightly different way: - extends Edge(User) instead of extends Edge<User>.

I think that if user need some additional metadata in connection or edge, he should create the classes by itself by extending the base built-in classes, just like in your advanced example:

export function <TNodeType>Edge(NodeType: TNodeType) {
  @ObjectType({ isAbstract: true })
  abstract class EdgeType {
    @Field(type => NodeType)
    node: TNodeType;
    @Field()
    cursor: string;
  }
  return EdgeType;
}

@ObjectType()
export class RecipeEdge extends Edge(Recipe) {
  @Field()
  personalNotes: string;
}

And then create his own RecipeConnection and RecipeConnectionArgs classes to handle the pagination by itself.

But for the simple example, I would make it as less verbose as it can be - just providing the information about using the pagination like @ConnectionArgs and the rest (node type) can be detected from existing decorators, without the need of manual subclassing. There should be also some built-in helpers and types for creating the connection response.

I'm afraid that it's a bit too complex feature to let newcomers make a PR as I'm not 100% sure about the API and can't provide precise tips about implementation without proper research and experimenting by my own 😕

@tonyxiao
Copy link

Related issue : #142

@orta
Copy link
Contributor Author

orta commented Oct 24, 2018

Thanks for the feedback @19majkel94 - that's super reasonable, I think I agree that maybe this one might be a bit tricky for a first timer, gonna close it and keep an eye on #142 👍

@orta orta closed this Oct 24, 2018
@MichalLytek
Copy link
Owner

Thanks again for this PR, now I know that there's a demand for ability to extend Edge and Connection generic types 😉

@MichalLytek MichalLytek added Enhancement 🆕 New feature or request Wontfix ❌ This will not be worked on Community 👨‍👧 Something initiated by a community labels Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request Wontfix ❌ This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants