Comments (6)
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.
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.
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.
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.
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.
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)
- Uncaught SyntaxError: missing name after . operator HOT 1
- FieldResolver performance HOT 1
- Inline fragment from an interface to an interface that extends it is invalid
- Resource exhaustion exploit when parsing queries HOT 7
- In GraphQL how to set WriteOnce functionality in specific property in the collection schema
- Generator generates alias imports not compatible with TypeScript Type-Only Imports and verbatimModuleSyntax HOT 2
- Printing the @oneOf directive HOT 1
- Allow shallow sorting with lexicographicSortSchema HOT 2
- Passing arguments into the resolvers by just using `buildSchema` and `graphql`? HOT 3
- Error after upgrading from 16.6 to 16.8.1 with globalThis.process.env.NODE_ENV HOT 12
- Add: Link to new api docs in the README of this project.
- Introspection result missing interfaces
- Detect subscription successfully started
- Errors thrown when iterating over subscription source event streams (AsyncIterables) should be caught HOT 1
- graphql resolver circular dependency type error (bi-directional relation) HOT 1
- Current main is up to 50% slower than previous major version HOT 10
- Type definition for `DefinitionNode` appears to be wrong HOT 4
- AST: `IntValueNode` and `FloatValueNode` store values as string HOT 2
- Array-type resolvers: an intuitive solution to the n+1 problem
- Performance of stack traces in errors results in high response latency (>1 second)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-js.