Given this schema:
const typeDefs = gql`type Asset {
id: ID!
name: String!
ScanProfiles: [ScanProfile]
}
type ScanProfile {
id: ID!
name: String!
Assets: [Asset]
}
type Query {
Asset(id: ID!): Asset
ScanProfile(id: ID!): Asset
}`;
// Some generic resolvers...
const resolvers = {
Asset: (root, args, context) => context.get('asset', args),
ScanProfile: (root, args, context) => context.get('scanProfile, args),
Query: {
Asset: (root, args, context) => context.get('asset', args),
ScanProfile: (root, args, context) => context.get('scanProfile', args)
}
};
const schema = makeExecutableSchema({typeDefs, resolvers
const joinResolvers = {
Asset: {
ScanProfiles: (root, args, context) => {
console.log(root.ScanProfiles);
return root.ScanProfiles.map(id => context.get('scanProfile', {id});
}
},
ScanProfiles: {
Assets: (root, args, context) => {
console.log(root.id);
return context.filter('asset', {ScanProfiles: ({ScanProfiles}) => ScanProfiles.includes(root.id)});
}
}
};
addResolveFunctionsToSchema({ schema, resolvers: joinResolvers });
I expect context.get
for a given id to return these shapes:
// Asset
{
id: '1234',
name: 'abcd',
ScanProfiles: ['5678', '5679']
}
//ScanProfile
{
id: '5678',
name: 'efgh'
}
Console is returning [null]
for Asset.ScanProfiles
.
The root resolvers return the fields as an array of strings, but when the root is then passed to the joinResolvers
those files are then null. I'm assuming because they don't match the defined typeDefs.
To work around this, I'm having to make a separate call to re-get the parent and then extract the stored vales for the child.
Am I defining my type incorrectly? Am I structuring my resolvers wrong?