Giter Club home page Giter Club logo

Comments (6)

leebyron avatar leebyron commented on March 28, 2024

Interface fields do not contain resolvers, but just describe the fields which implementing types must also declare.

When a field returns a value of an Interface type (like items in your example), the Interface type's resolveType is called which must return one of the concrete Object types:

var ArticleType = new GraphQLInterfaceType({
  name: 'Article',
  resolveType(obj) {
    if (isBlog(obj)) {
      return BlogType;
    } else if (isNews(obj)) {
      return NewsType;
    }
  }
});

...

This ensures that the appropriate resolve function for the genre field is called depending on which type you're dealing with at runtime.

from graphql-js.

leebyron avatar leebyron commented on March 28, 2024

Another option instead of defining resolveType on the interface is to define isTypeOf on the object types.

If you do this, then the interface will loop through all of it's implementing types calling isTypeOf until it gets a true result and will resolve to using that type.

from graphql-js.

charypar avatar charypar commented on March 28, 2024

It's not the interface field resolvers (there's no such thing) it's resolvers of fields of interface types (base is an object type, it just has a field that's an interface type). What I'd like to do is post-processing the items list itself once it's resolved, e.g. if genre on Blog and genre on News are very different (e.g. you need to run some business logic on tags or something), yet you want to filter the list by it, you end up duplicating the genre fetching logic for filtering items in the items field resolver on Feed.

It would be useful to get to manipulate the list after all the resolvers on Blog or News ran, resulting in a unified object format (i.e. Article) and be able to do some processing then. The only way to do it now is to use the source formats (which Article uses to decide if it's a Blog or a News and which Blog or News then uses to figure out the genre). I suppose this is only ever really useful for lists.

So graphically(-ish) the processing would go like this

top.fetchFeed -> Feed.fetchItems -> Article.resolveType; [type].fetchGenre -> Feed.postProcessItems

Hope that clarifies what I mean.

from graphql-js.

leebyron avatar leebyron commented on March 28, 2024

I understand now what you mean. That is not something we intend to support. It's actually unclear what the "postProcessItems" function would accept or return and probably depends on each application. It may not be the best place to do a filter, as ideally a filter helps you prevent actually fetching that information from a database or other backing service and a post-process would be too late to do that sort of work.

I would suggest creating utility functions which your types can use to avoid duplicating work:

var NewsType = new GraphQLObjectType({
  name: 'News',
  fields: {
    genre: {
      type: GraphQLString,
      resolve(obj) { 
        return postProcessGenre(obj.newsGenre);
      }
    }
  }
});

var BlogType = new GraphQLObjectType({
  name: 'Blog',
  fields: {
    genre: {
      type: GraphQLString,
      resolve(obj) { 
        return postProcessGenre(obj.blogGenre);
      }
    }
  }
});

Does this handle your case?

from graphql-js.

charypar avatar charypar commented on March 28, 2024

Oh I think I see your point - in my example if I don't ask for the genre in the subsection, I won't get it in the return value, therefore I can't filter by it. The input for the post-processing is not well defined... Didn't realise that.

The code suggestion doesn't handle my case (I'm after filter items list based on genre) but I can see how doing the type resolution twice (once to resolve genre, once to filter by genre) and reusing the genre fetching logic is still simpler than trying to nail down post-processing.

from graphql-js.

leebyron avatar leebyron commented on March 28, 2024

I'm going to close this task out for now since we've come to some sort of resolution. But feel free to re-open or open new tasks in the future

from graphql-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.