AWS Amplify Graphql 最佳嵌套深度
创始人
2024-11-13 22:30:20
0

在AWS Amplify GraphQL中,嵌套深度是指查询中嵌套对象的层数。为了提高性能,避免不必要的数据加载,以及减少查询的复杂性,建议限制嵌套深度。

以下是一个示例解决方法,用于限制嵌套深度:

  1. 在GraphQL模式文件中定义自定义指令。
directive @nestingDepth(maxDepth: Int) on FIELD_DEFINITION
  1. 在查询类型中使用指令,并指定最大嵌套深度。
type Query {
  user(id: ID!): User @nestingDepth(maxDepth: 2)
  ...
}
  1. 在AWS Amplify项目中创建自定义指令的解析器。

在项目根目录创建 amplify/backend/api/{API_NAME}/stacks/CustomResources 目录,并在该目录下创建 nestingDepth.js 文件。

const fs = require('fs-extra');
const path = require('path');

const amplifyMetaFilePath = path.join(__dirname, '../../../amplify-meta.json');
const amplifyMeta = JSON.parse(fs.readFileSync(amplifyMetaFilePath, 'utf8'));

const resolverFilePath = path.join(__dirname, '../../../amplify/backend/api/{API_NAME}/build/resolvers');
const resolverFiles = fs.readdirSync(resolverFilePath);

resolverFiles.forEach(file => {
  const filePath = path.join(resolverFilePath, file);
  const resolver = JSON.parse(fs.readFileSync(filePath, 'utf8'));

  if (resolver && resolver.kind === 'UNIT' && resolver.providerName === 'AMAZON_DYNAMODB') {
    const typeName = resolver.metadata.typeName;
    const fieldName = resolver.metadata.fieldName;
    const maxDepth = getNestingDepth(typeName, fieldName);

    if (maxDepth) {
      const resolverContent = fs.readFileSync(filePath, 'utf8');
      const updatedResolverContent = addNestingDepthDirective(resolverContent, maxDepth);
      fs.writeFileSync(filePath, updatedResolverContent);
    }
  }
});

function getNestingDepth(typeName, fieldName) {
  const apiId = amplifyMeta.providers.awscloudformation.AmplifyAppId;
  const graphqlApiId = amplifyMeta.providers.awscloudformation.Region;
  const resourcePath = `amplify/backend/api/${apiId}/build/resolvers/${graphqlApiId}/${typeName}_${fieldName}.resolver.json`;

  if (fs.existsSync(resourcePath)) {
    const resourceContent = fs.readFileSync(resourcePath, 'utf8');
    const resource = JSON.parse(resourceContent);

    if (resource && resource.kind === 'UNIT' && resource.providerName === 'AMAZON_DYNAMODB' && resource.nestingDepth) {
      return resource.nestingDepth;
    }
  }

  return null;
}

function addNestingDepthDirective(resolverContent, maxDepth) {
  const resolver = JSON.parse(resolverContent);
  const pipelineFunctions = resolver.pipelineFunctions || [];
  
  const directive = {
    kind: 'DYNAMODB_NESTING_DEPTH',
    nestingDepth: maxDepth
  };

  const updatedPipelineFunctions = [directive, ...pipelineFunctions];
  resolver.pipelineFunctions = updatedPipelineFunctions;

  return JSON.stringify(resolver, null, 2);
}
  1. 在AWS Amplify控制台或命令行中运行部署命令。
amplify push
  1. 更新相关的GraphQL解析器。

amplify/backend/api/{API_NAME}/resolvers 目录下的相关解析器文件中,添加以下代码片段:

{
  "kind": "DYNAMODB_NESTING_DEPTH",
  "nestingDepth": 2
}

这样,最大的嵌套深度就被限制为2层。您可以根据需求调整最大深度。

请注意,这只是一个示例解决方法,您可能需要根据自己的项目结构和需求进行适当的调整。

相关内容

热门资讯

外挂绝活!uupoker透视,... 外挂绝活!uupoker透视,扑克之星辅助-总是是真的有辅助教程(哔哩哔哩)1、点击下载安装,扑克之...
外挂妙计!智星菠萝可以辅助吗,... 外挂妙计!智星菠萝可以辅助吗,佛手在线大菠萝为什么都输-一贯真的是有辅助方法(哔哩哔哩)1、下载好佛...
外挂攻略!来玩app 德州 辅... 外挂攻略!来玩app 德州 辅助,约局吧如何查看是否有挂-果然一直总是有辅助方法(哔哩哔哩)1、每一...
外挂烘培!sohoo辅助,po... 外挂烘培!sohoo辅助,pokeplus脚本-真是有辅助教程(哔哩哔哩)一、pokeplus脚本可...
外挂大纲!大菠萝免费辅助,哈糖... 外挂大纲!大菠萝免费辅助,哈糖大菠萝软件下载-切实存在有辅助工具(哔哩哔哩)1、哈糖大菠萝软件下载透...
外挂教程书!聚星ai辅助工具激... 外挂教程书!聚星ai辅助工具激活码,德普之星透视辅助软件激活码-原来有辅助技巧(哔哩哔哩)1、很好的...
外挂资料!佛手大菠萝有挂吗,拱... 外挂资料!佛手大菠萝有挂吗,拱趴大菠萝万能挂-竟然一直总是有辅助脚本(哔哩哔哩)1、超多福利:超高返...
外挂攻略!aa poker辅助... 外挂攻略!aa poker辅助,德州局透视-原来是真的有辅助教程(哔哩哔哩)1、许多玩家不知道德州局...
外挂手筋!德州局脚本,poke... 外挂手筋!德州局脚本,pokemomo辅助工具-原来是有辅助脚本(哔哩哔哩)1、玩家可以在pokem...
外挂办法!pokermaste... 外挂办法!pokermaster辅助器,pokerworld修改器-好像是真的有辅助方法(哔哩哔哩)...