Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Interface and union resolvers #149

Merged
merged 21 commits into from
Jan 20, 2019
Merged

Interface and union resolvers #149

merged 21 commits into from
Jan 20, 2019

Conversation

koenpunt
Copy link
Contributor

@koenpunt koenpunt commented Oct 19, 2018

This is a new version of #66.

I didn't test the generated files yet, but the test snapshot looks promising.

It generates a resolver for the interface and union types in the following shape;

export namespace MediaResolvers {
  export interface Type {
    __resolveType: GraphQLTypeResolver<Image | Video, Context>;
  }
}

And it generates a __isTypeOf resolver on types that implement an interface or are part of a union.

export namespace MediaResolvers {
  export interface Type {
    // ...
    __isTypeOf?: GraphQLIsTypeOfFn<Image | Video, Context>;
  }
}

Also, what's up with all the .tmp tests? Barely anything is covered with tests at the moment..

TODO

  • Ignore interfaces that are not implemented

@schickling
Copy link
Contributor

Thanks a lot for this PR. Would you mind testing it locally, so we can merge this PR with confidence? 💪

Also, what's up with all the .tmp tests? Barely anything is covered with tests at the moment..

Yes, we're in the process of refactoring the tests as well. See #157.

@koenpunt
Copy link
Contributor Author

koenpunt commented Oct 20, 2018

Thanks a lot for this PR. Would you mind testing it locally, so we can merge this PR with confidence? 💪

Just updated some minor things, should be good now. As in, works on my machine.

@koenpunt
Copy link
Contributor Author

koenpunt commented Oct 20, 2018

For even better support I could also generate an interface type, which implements the interface's resolver fields, and can be used as a parent type to prevent writing resolvers for implementing types twice.

@schickling
Copy link
Contributor

schickling commented Oct 20, 2018

Thanks a lot @koenpunt. I've just taken a look. A few things:

  1. Would you mind extending the scaffolding behavior as well?
  2. What's the reason for the resolver to be __resolveType? instead of __resolveType?
  3. Same for IUser? in the Resolvers interface
  4. Same for __isTypeOf? in a particular resolver type

@koenpunt
Copy link
Contributor Author

@schickling __resolveType indeed doesn't need to be optional, since the resolver itself is.
__isTypeOf is optional, because when the interface's resolver is defined (and thus __resolveType), this method is not necessary.

Regarding the scaffolding behavior; I'll be on a plane from Amsterdam to Las Vegas, and I expect that to be done when I land in about 14 hours.

@schickling
Copy link
Contributor

Is my understanding correct that you either have to implement IUser + __resolveType or the __resolveType method for all types that implement an interface? Seems like we have three options to go from here:

  1. Make both cases optionally typed (-> your current implementation)
  2. Just allow one of the ways (I suggest IUser + __resolveType) and force it to be implemented. This allows for stricter type-safety.
  3. Add a configuration option to let the user decide re (2)

What are your thoughts? I'm kind of leaning to (2) for now and eventually (3).

Have a safe flight 🛬

@schickling
Copy link
Contributor

I had a quick chat with @leebyron. He suggested to go for option (1) as the graphql-js implementation will check at runtime that at least one of __resolveType or __isTypeOf is present.

@koenpunt are you still up to moving forward with this?

@koenpunt
Copy link
Contributor Author

@schickling alright, good to know. Yeah I'm using the library already myself, so I kinda need it, but I have to rebase again etc.

@koenpunt
Copy link
Contributor Author

koenpunt commented Nov 5, 2018

I'm not going to work on this before #244 is merged, because I have a feeling I have to start over again once that lands...

@schickling
Copy link
Contributor

#244 is now merged. Do you want to tackle this again? (Sorry for the overhead but I think the refactor was well worth it) 🙏

@koenpunt
Copy link
Contributor Author

koenpunt commented Nov 8, 2018

Do you want to tackle this again?

I will have a go at it tonight or tomorrow night

@koenpunt
Copy link
Contributor Author

Typescript now works. For flow there are some types not being generated yet.

@anotherhale
Copy link

Is this close? This would greatly benefit a project that I am working on.

@koenpunt
Copy link
Contributor Author

I'd like to get some input on what I created thus far. @schickling you have any remarks? Or are you totally fine with where this is going and are you just waiting for me to finish the flow implementation too?

@koenpunt
Copy link
Contributor Author

@schickling do you have any feedback?

This was referenced Dec 17, 2018
@janheinrichmerker
Copy link

@schickling This PR seems to be ready to merge. Is there anything missing?

Copy link
Member

@jasonkuhrt jasonkuhrt left a comment

Choose a reason for hiding this comment

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

Overall this looks great @koenpunt thank-you!

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Dec 20, 2018

Seems like we have three options to go from here:

@schickling I think there could be a fourth option actually, by leveraging a union to force developers into one of many valid implementations. Its like raising both options in your config option to the type level : )

That said, right now, I don't think the added implementation complexity is worth it; and the potential implications for type comprehensibility would need more consideration.

@anotherhale Yes I think its close. I will be checking in with @timsuchanek about it this week.

@jasonkuhrt
Copy link
Member

I'm not 100% convinced but all things considered I think overall a good next step is to merge this and make a 0.6.0-rc1 release. There's a lot of refactoring I want to do, and from reading the comments flow type may not be 100% complete, but I think shipping something for TypeScript users is worth it. The release notes will be clear that flowtype support is not guaranteed.

@jasonkuhrt jasonkuhrt merged commit eb2f3cf into prisma-labs:master Jan 20, 2019
@maggo
Copy link

maggo commented Jan 23, 2019

Hi @jasonkuhrt I'm sooo happy that this is being implemented! Gave me a headache all day before I found the PR. Just discovered an issue though:

The generated resolvers' index.ts doesn't seem to include the Interface resolvers. Since the resolvers are all optional you won't discover this when copying the scaffolding, only after executing them. I guess this is a bug?

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Jan 23, 2019

@maggo cheers : )

Bug or feature, either way, feel free to create an issue, supply some context/example and we can discuss further there 👍 Thanks!

@koenpunt
Copy link
Contributor Author

@jasonkuhrt Nice to see this is merged, since this means I soon no longer have to run my own fork to use graphqlgen. However I have to say that I don't really like how you "hijacked" this PR without notice.

Also, your modifications caused a generation issue, that wasn't there before:

    args: {},
     ctx: Context,
     info: GraphQLResolveInfo
-  ) => Channel | null | Promise<Channel | null>;
+  ) =>
+    | Channel
+    | Channel
+    | Channel
+    | null
+    | null
+    | Promise<Channel | Channel | Channel | null | null>;

And lastly I'm wondering why you removed the InterfaceTypes, because that's something that is actually really useful, and is now again a blocker for using the official version.

@koenpunt koenpunt deleted the interface-and-union-resolvers branch January 24, 2019 10:14
@jasonkuhrt
Copy link
Member

However I have to say that I don't really like how you "hijacked" this PR without notice.

Sorry to hear that, it was actually to give you credit on master branch. It felt to me the least I could to thank you for the contribution. However I forgot or neglected to post a notice about continuing work on this branch (whereas I did do that on some other PRs). So, sorry that you were taken by surprise 🙏. I don't think I was seriously expecting a reply from you so that probably contributed to lowering my communication standards. Thanks for sharing your feeling on this.

Also, your modifications caused a generation issue, that wasn't there before:

We're currently in an rc stage. If you could create a new issue with context that would be helpful toward getting a fix out.

And lastly I'm wondering why you removed the InterfaceTypes, because that's something that is actually really useful, and is now again a blocker for using the official version.

Could you elaborate? Ideally in a new issue, thanks!

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Jan 25, 2019

Also, your modifications caused a generation issue, that wasn't there before:

@koenpunt fixed duplicate null 17cdb85

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

Successfully merging this pull request may close these issues.

6 participants