[interface] how to return an interface in data-fetcher? #151
Answered
by
srinivasankavitha
jessepinkman9900
asked this question in
Q&A
Replies: 1 comment 1 reply
-
The DgsTypeResolver would stay the same. The types ActionMovie and ScaryMovie would define the additional fields, i.e, year and director ( title is already there). The datafetcher can create an instance of either ActionMovie or ScaryMovie with the additional fields populated by the same datafetcher.
You can also define field specific data fetchers if you are not able to populate all the fields in the same movies data fetcher. graphql-java will take care of calling every data fetcher for the fields you specify.
Hope that helps.
… On Mar 13, 2021, at 10:38 AM, Srinivas Kota ***@***.***> wrote:
Hi,
I'm working with a graphql schema that has interfaces. I'm having trouble returning them in the data-fetcher.
Looking for a worked-out example.
I have read: https://netflix.github.io/dgs/advanced/type-resolvers-for-abstract-types/
# original schema
type Query {
movies: [Movie]
}
interface Movie {
title: String
}
type ScaryMovie implements Movie {
title: String
gory: Boolean
scareFactor: Int
}
type ActionMovie implements Movie {
title: String
nrOfExplosions: Int
}
@DgsComponent
public class MovieDataFetcher {
@DgsData(parentType = "Query", field = "movies")
public List<Movie> movies() {
return Lists.newArrayList(
new ActionMovie("Crouching Tiger", 0),
new ActionMovie("Black hawk down", 10),
new ScaryMovie("American Horror Story", true, 10),
new ScaryMovie("Love Death + Robots", false, 4)
);
}
}
They have solved for a case when the interface has ONLY ONE attrribute, i.e title in case for interface Movie
@DgsTypeResolver(name = "Movie")
public String resolveMovie(Movie movie) {
if(movie instanceof ScaryMovie) {
return "ScaryMovie";
} else if(movie instanceof ActionMovie) {
return "ActionMovie";
} else {
throw new RuntimeException("Invalid type: " + movie.getClass().getName() + " found in MovieTypeResolver");
}
}
Looking for a solution where Movie had more than one attribute.
For example IF schema was
# example schema
type Query {
movies: [Movie]
}
interface Movie {
title: String
year: Int
director: String
}
type ScaryMovie implements Movie {
title: String
gory: Boolean
scareFactor: Int
}
type ActionMovie implements Movie {
title: String
nrOfExplosions: Int
}
THEN what would the resulting @DgsData or @DgsTypeResolver be?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
jessepinkman9900
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm working with a graphql schema that has interfaces. I'm having trouble returning them in the data-fetcher.
Looking for a worked-out example.
Getting this error in spring logs when I try to execute query in graphiql:
I have read: https://netflix.github.io/dgs/advanced/type-resolvers-for-abstract-types/
They have solved for a case when the interface has ONLY ONE attrribute, i.e
title
in case forinterface Movie
Looking for a solution where Movie had more than one attribute.
For example IF schema was
THEN what would the resulting
@DgsData
or@DgsTypeResolver
be?Beta Was this translation helpful? Give feedback.
All reactions