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)
- IHeyReally.org
- Suggestion: Bundling in v17, ESM, CJS, and the dual package hazard HOT 9
- Just to give my 2c, I'm not sure if `exports.development` and `exports.production` target conditionsa are widely supported, so some `import.env` shenanigans may still be necessary until that's widely adopted. HOT 1
- Collection of libraries and how they import from `graphql` HOT 20
- `process.env`, `globalThis`, and `typeof process` HOT 21
- Introspection queries don't support `@oneOf` HOT 2
- Tutorial data HOT 2
- astFromValue fails with a custom scalar serializing to an object value HOT 5
- In a response to a query about an Issue, the URL and other info is missing for links created with Reference editor button HOT 5
- author/committer -> user fields returning NULL for commits committed by user
- [email protected]
- npm link with graphql package breaks application HOT 2
- IHeyReally.com HOT 1
- Can I ask what is the progress here? Is there a solution being worked on? Do we have some timeline? Or progress with issue? Thanks!
- IHeyReally.com
- Typescript error with 16.9.0 (re ThunkObjMap) HOT 3
- Notice: default branch is now `16.x.x` HOT 2
- <a href="https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/12261526/38307428/4135/#/?version=2"><img src="https://s3.amazonaws.com/cla-project-logo-prod/cla-not-signed.svg" alt="CLA Not Signed" align="left" height="28" width="328"></a><br/><br /><ul><li><a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/12261526/38307428/4135/#/?version=2' target='_blank'>:x:</a> - login: @Heyitsquoracom / name: [email protected] . The commit (b6081f914f0c5c22ee48d26aff4b473bf17627ce) is not authorized under a signed CLA. <a href='https://api.easycla.lfx.linuxfoundation.org/v2/repository-provider/github/sign/12261526/38307428/4135/#/?version=2' target='_blank'>Please click here to be authorized</a>. For further assistance with EasyCLA, <a href='https://jira.linuxfoundation.org/servicedesk/customer/portal/4' target='_blank'>please submit a support request ticket</a>.</li></ul><!-- Date Modified: 2024-06-28 04:40:51.630768 -->
- Error with version v16.9.0: `Can't resolve '@apollo/subgraph/dist/directives''` HOT 1
- Type is incorrect in graphql ExecutionResult with new type GraphQLFormattedError HOT 1
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.