0

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?

Plummer
  • 6,522
  • 13
  • 47
  • 75

1 Answers1

0

The issue was with defining the type.

When the parent resolves, it is expecting the child to be an array of Object but what is being delivered is an array of strings. Thus, the type validation returns an array of nulls per the "bubble-up" as defined in result 1 here.

There is an open discussion on github to allow for unions scalar types.

Plummer
  • 6,522
  • 13
  • 47
  • 75