graphql-解析程序的根/父属性为null

时间:2019-02-26 19:07:50

标签: graphql graphql-tools

给出以下模式:

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 });

我希望给定ID的context.get返回以下形状:

// Asset
{
  id: '1234',
  name: 'abcd',
  ScanProfiles: ['5678', '5679']
}
//ScanProfile
{
  id: '5678',
  name: 'efgh'
}

控制台返回[null]的{​​{1}}。

根解析器将字段作为字符串数组返回,但是当根随后传递到Asset.ScanProfiles时,这些文件为空。我假设是因为它们与定义的typeDefs不匹配。

要解决此问题,我必须单独调用以重新获得父级,然后为子级提取存储的值。

我定义的类型错误吗?我在构造我的解析器吗?

1 个答案:

答案 0 :(得分:0)

问题在于定义类型。

当父对象解析时,它期望子对象是Object数组,但传递的是字符串数组。因此,类型验证根据结果1 here中的定义,每个“冒泡”都会返回一个空数组。

在github上进行了公开讨论,以允许使用unions scalar types