Can't make interface general resolvers #1896
-
What happened?I have an interface for a stocklist. Portfolio and Watchlist both implement Stocklist. The Stocklist type has its own defined fields including name, id and a list of stocks. I want to make a generalised resolver for both portfolio and watchlist to be able to add a stock to the stocklist. What did you expect?So the variable I'm working with is of type stocklist, as that's what the find (from mongodb) function returns. Even though the fields are defined for stocklist however, the only thing I can apparently access is a Minimal graphql.schema and models to reproduceI do apologise for the poor naming, that the interface is called stocklist but the stocklists have a field called stocklist 😅, I'll try to name it something a bit better in the future interface Stocklist {
id: String!
name: String!
stockList: [String!]
}
type Portfolio implements Stocklist {
id: String!
name: String!
stockList: [String!]
isPublic: Boolean!
total: Float!
cashHolding: Float
dayGain: Float
totalGain: Float
followedBy: [User!]
}
type Watchlist implements Stocklist {
id: String!
name: String!
stockList: [String!]
} versions
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The Go interfaces generated by gqlgen will only contain the one method to identify them. You basically have two options.
swtich v := obj.(type) {
case WatchList:
// v is a WatchList here
case Portfolio:
// v is a Portfolio here
}
|
Beta Was this translation helpful? Give feedback.
The Go interfaces generated by gqlgen will only contain the one method to identify them. You basically have two options.
Stocklist
passed to your resolver is either aWatchlist
orPortfolio
. You can do something like the following to get the concrete value to work with.Stocklist
interface yourself. gqlgen can pick up existing model types. You can define a different set of methods and the resolver will be able to access them. You'll just need to make sure you satisfy that interface forWatchlist
andPortfolio
. (Don't define things…