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

Type resolvers for union and interface types #283

Open
mmmdreg opened this issue May 8, 2019 · 2 comments
Open

Type resolvers for union and interface types #283

mmmdreg opened this issue May 8, 2019 · 2 comments

Comments

@mmmdreg
Copy link

mmmdreg commented May 8, 2019

I have my first lacinia app in production and am enjoying it so far so thanks for the hard work up front.

Currently to resolve union and interface types correctly, we have to tag-with-type or use records. I find these a bit troublesome especially if the type to be tagged is nested somewhere in the data I am returning.

I like to keep my graphql resolvers as a very thin layer on top of business logic which may or may not be shared by other non-graphql access methods. In the above case of nested data structures, I either have to tag it where the child value is created, polluting the namespace with lacinia specific code, or in the resolver, do some cumbersome unwrapping, tagging, and wrapping.

I’ve written several graphql services in javascript before and one thing I liked there was being able to define a resolveType function (https://graphql.org/graphql-js/type/#graphqluniontype). This is literally a function that takes the value and returns some type identifier, eg based on some attribute of a map. This could perhaps be implemented as additional placeholder attributes in the schema.edn and nicely prevents graphql requirements to leak into other code.

What do you think?

@hlship
Copy link
Member

hlship commented May 9, 2019

That's an interesting idea.

@matteoredaelli
Copy link
Contributor

matteoredaelli commented Jan 4, 2023

Any news about this feature? I'd like to have in lacinia something like I have in javascript

  LdapObject: {
    __resolveType(obj, context, info) {
      if (obj && obj.objectClass.includes("computer")) {
        return "LdapComputer";
      }
      if (obj && obj.objectClass.includes("group")) {
        return "LdapGroup";
      }
      if (obj && obj.objectClass.includes("user")) {
        return "LdapUser";
      }
      return null; // GraphQLError is thrown
    },
  },

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

No branches or pull requests

3 participants