简体   繁体   English

GraphQL 选择集解析

[英]GraphQL selection set parsing

I have this GraphQL Selection Set inside my AppSync Lambda Resolver:我的 AppSync Lambda 解析器中有这个 GraphQL 选择集:

{
  recommendationId
  items {
    id
    name
  }
}

I need the selection set inside items , which will be heavily nested.我需要在items中设置选择,这将是严重嵌套的。

How can I get this output?我怎样才能得到这个 output?

{
  id
  name
}

Context(replaced business object with Object ):上下文(将业务 object 替换为Object ):

schema.graphql架构.graphql

type Object {
 id: ID!
 name: String!
}

type Query {
 getObjectsById(objectsById: ObjectsByID!): [Object] 
 listRecommendedObjects(personId: ID!): RecommendedObjects
}

input ObjectsByID {
  ids: [ID!]!
}

type RecommendedObjects {
  items: [Object]
  recommendationId: String
}

Inside resolver:内部解析器:

const buildQuery = (selectionSetGraphQL) =>
  `query MyQuery($objectsById: ObjectsByID!) {
    getObjectsById(objectsById: $objectsById) ${selectionSetGraphQL}
  }`

Goal:目标:

Call listRecommendedObjects which will invoke custom lambda that will talk to AWS Personalize to get item IDs.调用 listRecommendedObjects 将调用自定义 lambda 将与 AWS Personalize 对话以获取项目 ID。 Then, i want to use the selection set of Object to call getObjectsById within that Lambda to get the data of Item IDs from Personalize然后,我想使用 Object 的选择集在Object中调用 getObjectsById 以从 Personalize 中获取项目 ID 的数据

I got a sensible solution using graphql-js .我使用graphql-js得到了一个明智的解决方案。

I use parse , then select what I need, then convert it back to a string with print我使用parse ,然后使用 select 我需要的东西,然后将其转换回带有print的字符串

const { parse, print } = require("graphql/language")

const document = parse('{ id items {id name} }')

const subSet = document
  .definitions[0]
  .selectionSet
  .selections
  .find(elem => elem.name.value == 'items')

print(subSet) // => `items {id name}`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM