apollostack - Apollo GraphQL: Schema for Query to Return Objects of Different Types? -
i have 3 different postgres tables, each containing different types of associates. database fields different each type-- that's why in 3 separate tables.
i have 1 component can potentially access type of associate. seems examples i've encountered far, component typically associated 1 graphql query, e.g.:
const withdata = graphql(getoneassociate_query, { options({ navid }) { return { variables: { _id: navid} }; } , props({ data: { loading, getoneassociate } }) { return { loading, getoneassociate }; }, }); export default compose( withdata, withapollo )(associateslist); and appears given graphql query, can return single type of record, e.g. in schema:
getoneassociate(associatetype: string): [associateaccountingtype] question: possible design graphql schema such single query, can return objects of different types? resolver receive associatetype parameter tell postgres table reference. schema like, return objects of type associateaccountingtype, associateartdirectortype, associateaccountexectype, etc. needed?
thanks in advance info.
you have 2 options here. declare interface returned type , make sure each of these associatetypes extends interface. idea in cases have common fields on of types together. so:
interface associatetype { id: id department: department employees: [employee] } type associateaccountingtype implements associatetype { id: id department: department employees: [employee] accounts: [account] } type associateartdirectortype implements associatetype { id: id department: department employees: [employee] projects: [project] } if don't have common fields or you'd rather have types unrelated reason, can use union type. declaration simpler requires use fragment each field query engine assumes these types have no common fields.
union associatetype = associateaccountingtype | associateartdirectortype | associateaccountexectype one more important thing how implement resolver tell graphql server , client actual concrete type. apollo, need supply __resolvetype function on union/interace type:
{ associatetype: { __resolvetype(associate, context, info) { return associate.isaccounting ? 'associateaccountingtype' : 'associateartdirectortype'; }, } }, this function can implement whatever logic want must return name of type using. associate argument actual object returned parent resolver. context usual context object , info holds query , schema information.
Comments
Post a Comment