Comments (4)
Thanks @helfer, it took me a while to figure out how to deal with my connectors, but it worked. The app is more complex than what's shown, but I hope my solution will aid anyone who might be struggling with this:
Schema:
type Artist {
id: Int,
name: String,
}
type Venue {
id: Int,
name: String,
}
union Owner = Artist | Venue
type Event {
id: Int!,
owner: Owner,
}
type RootQuery {
event(id: Int): Event,
venue(id: Int): Venue,
}
Resolvers:
RootQuery: {
event(_, args) {
return eventModel.getEventById(args.id);
},
venue(_, args) {
return venueModel.getVenueById(args.id);
},
},
Event: {
owner(event) {
return eventModel.getOwner(event.id)
},
},
Venue: {
id(venue) {
return venue.id;
},
name(venue) {
return venue.name;
},
},
Artist: {
id(artist) {
return artist.id;
},
name(artist) {
return artist.name;
},
},
Owner: { // this needs to receive an object with a type property for it to work, check your connectors!
__resolveType(owner, ctx, info) {
if(owner.type == 'artist') {
return info.schema.getType('Artist');
} else return info.schema.getType('Venue');
},
}
The query:
{
event(id: 3) {
owner {
... on Artist {
name
}
}
}
}
If anyone's using Knex, here's my connector:
export class Event {
getOwner(eventId) {
return knex('event')
// Check the owner type
.where('event.id', eventId)
.then(result => {
// Customise result for each owner type
if(result[0].owner_type === 'artist') {
return knex('event')
.join('artist', 'artist.id', 'event.owner_id')
.select('artist.id', 'artist.name')
.where('event.id', eventId)
.limit(1)
.first()
.then(response => {
response.type = 'artist';
return response;
})
} else if(result[0].owner_type === 'venue') {
return knex('event')
.join('venue', 'venue.id', 'event.venue_id')
.select('venue.id', 'venue.name')
.where('event.id', eventId)
.limit(1)
.first()
.then(response => {
response.type = 'venue';
return response;
})
}
});
}
}
from apollo-server.
You can use a union:
union ArtistOrVenue = Artist | Venue
from apollo-server.
Hi @twonmulti
It will return the GraphQLObjectType defined for them.
Then this is what resolveType will return to resolve the abstract type..
from apollo-server.
@savovs In you Resolvers section, what does info.schema.getType('Artist')
return?
Owner: { // this needs to receive an object with a type property for it to work, check your connectors!
__resolveType(owner, ctx, info) {
if(owner.type == 'artist') {
return info.schema.getType('Artist');
} else return info.schema.getType('Venue');
},
}
I'm trying to figure out exactly what I should be returning here. I have a type field on my unioned objects. Is it a string, object, or something else?
from apollo-server.
Related Issues (20)
- Error from resolver without extensions[V3 was not adding extensions in any error type from resolvers] HOT 2
- Integration of AS4 with graphql-jit not working HOT 3
- Bun integration on subscription (Query and Mutation Works, but not Subscription) HOT 3
- TypeSafe Custom GQLError HOT 2
- support for fastify version 4 HOT 2
- Allow passing http2 server to ApolloServerPluginDrainHttpServer
- ApolloServerPluginUsageReporting reportTimer has incorrect type HOT 1
- Elasticache cluster - CROSSLOT Keys in request don't hash to the same slot HOT 4
- Unable to package/deploy with serverless using Node14 and apollo-serverless-core HOT 3
- ApolloServerPluginUsageReporting does not allow to pass http agent HOT 11
- Misconfigured `sendVariableValues` results in similar behavior as `{all: true}` HOT 3
- ApolloServerPluginUsageReporting does not allow to pass http agent HOT 2
- Apis that have their requests cached dont need to make unnecessary auth token fetches
- Document known mitigated security issue in `@koa/cors` for AS3 users HOT 1
- Update `@koa/cors` dependency for AS3 (or why we don't intend to) HOT 2
- Share and modify variables between Express and the GraphQL/Apollo server HOT 1
- Error: ApolloServer<BaseContext>' is not assignable to type 'ApolloServer<ExpressContext> HOT 3
- callback function for onSchemaLoadOrUpdate when upgrading from AS v3 to AS v4 HOT 3
- Allow plugins to throw meaningful GraphQL errors
- Getting started tutorial has typescript issues...
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 apollo-server.