How to use the graphql-mapping-template.ifElse function in graphql-mapping-template

To help you get started, we’ve selected a few graphql-mapping-template examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aws-amplify / amplify-cli / packages / graphql-http-transformer / src / resources.ts View on Github external
qref('$headers.put("accept-encoding", "application/json")'),
            ...parsedHeaders,
            HttpMappingTemplate.getRequest({
              resourcePath: path,
              params: obj({
                query: ref('util.toJson($ctx.args.query)'),
                headers: ref('util.toJson($headers)'),
              }),
            }),
          ])
        )
      ),
      ResponseMappingTemplate: print(
        ifElse(
          raw('$ctx.result.statusCode == 200'),
          ifElse(
            ref('ctx.result.headers.get("Content-Type").toLowerCase().contains("xml")'),
            ref('utils.xml.toJsonString($ctx.result.body)'),
            ref('ctx.result.body')
          ),
          ref('util.qr($util.appendError($ctx.result.body, $ctx.result.statusCode))')
        )
      ),
    }); // .dependsOn(ResourceConstants.RESOURCES.GraphQLSchemaLogicalID)
  }
github aws-amplify / amplify-cli / packages / graphql-auth-transformer / src / resources.ts View on Github external
public operationCheckExpression(operation: string, field: string) {
    return block('Checking for allowed operations which can return this field', [
      set(ref('operation'), raw('$util.defaultIfNull($context.source.operation, "null")')),
      ifElse(raw(`$operation == "${operation}"`), ref('util.toJson(null)'), ref(`util.toJson($context.source.${field})`)),
    ]);
  }
github aws-amplify / amplify-cli / packages / graphql-dynamodb-transformer / src / resources.ts View on Github external
qref('$condition.expressionNames.put("#id", "id")'),
                ])
              ),
            ]),
            ifElse(
              ref(ResourceConstants.SNIPPETS.ModelObjectKey),
              compoundExpression([
                set(
                  ref('condition'),
                  obj({
                    expression: str(''),
                    expressionNames: obj({}),
                  })
                ),
                forEach(ref('entry'), ref(`${ResourceConstants.SNIPPETS.ModelObjectKey}.entrySet()`), [
                  ifElse(
                    raw('$velocityCount == 1'),
                    qref('$condition.put("expression", "attribute_exists(#keyCondition$velocityCount)")'),
                    qref('$condition.put(\
"expression", "$condition.expression AND attribute_exists(#keyCondition$velocityCount)")')
                  ),
                  qref('$condition.expressionNames.put("#keyCondition$velocityCount", "$entry.key")'),
                ]),
              ]),
              set(
                ref('condition'),
                obj({
                  expression: str('attribute_exists(#id)'),
                  expressionNames: obj({
                    '#id': str('id'),
                  }),
                })
github aws-amplify / amplify-cli / packages / graphql-dynamodb-transformer / src / resources.ts View on Github external
set(ref('condition.expressionValues'), ref('conditionExpressionValues')),
              qref(`$condition.expressionValues.putAll($conditionFilterExpressions.expressionValues)`),
            ])
          ),
          iff(
            and([ref('condition.expressionValues'), raw('$condition.expressionValues.size() == 0')]),
            set(
              ref('condition'),
              obj({
                expression: ref('condition.expression'),
                expressionNames: ref('condition.expressionNames'),
              })
            )
          ),
          DynamoDBMappingTemplate.deleteItem({
            key: ifElse(
              ref(ResourceConstants.SNIPPETS.ModelObjectKey),
              raw(`$util.toJson(\$${ResourceConstants.SNIPPETS.ModelObjectKey})`),
              obj({
                id: ref('util.dynamodb.toDynamoDBJson($ctx.args.input.id)'),
              }),
              true
            ),
            condition: ref('util.toJson($condition)'),
            isSyncEnabled,
          }),
        ])
      ),
      ResponseMappingTemplate: isSyncEnabled ? print(DynamoDBMappingTemplate.dynamoDBResponse()) : print(ref('util.toJson($ctx.result)')),
      ...(syncConfig && { SyncConfig: SyncUtils.syncResolverConfig(syncConfig) }),
    });
  }
github aws-amplify / amplify-cli / packages / graphql-connection-transformer / src / resources.ts View on Github external
String(keySchema[1].AttributeName)));
            }
        }

        let queryArguments = {
            query: raw('$util.toJson($query)'),
            scanIndexForward: ifElse(
                ref('context.args.sortDirection'),
                ifElse(
                    equals(ref('context.args.sortDirection'), str('ASC')),
                    bool(true),
                    bool(false)
                ),
                bool(true)
            ),
            filter: ifElse(
                ref('context.args.filter'),
                ref('util.transform.toDynamoDBFilterExpression($ctx.args.filter)'),
                nul()
            ),
            limit: ref('limit'),
            nextToken: ifElse(
                ref('context.args.nextToken'),
                str('$context.args.nextToken'),
                nul()
            ),
            index: indexName ? str(indexName) : undefined
        }

        if (!indexName) {
            const indexArg = 'index'
            delete queryArguments[indexArg];
github aws-amplify / amplify-cli / packages / graphql-key-transformer / src / KeyTransformer.ts View on Github external
RequestMappingTemplate: print(
            compoundExpression([
                setQuerySnippet(definition, directive, ctx),
                set(ref('limit'),
                ref(`util.defaultIfNull($context.args.limit, ${defaultPageLimit})`)),
                set(
                    ref(requestVariable),
                    obj({
                        version: str('2017-02-28'),
                        operation: str('Query'),
                        limit: ref('limit'),
                        query: ref(ResourceConstants.SNIPPETS.ModelQueryExpression),
                        index: str(index)
                    })
                ),
                ifElse(
                    raw(`!$util.isNull($ctx.args.sortDirection)
                    && $ctx.args.sortDirection == "DESC"`),
                    set(ref(`${requestVariable}.scanIndexForward`), bool(false)),
                    set(ref(`${requestVariable}.scanIndexForward`), bool(true)),
                ),
                iff(
                    ref('context.args.nextToken'),
                    set(
                        ref(`${requestVariable}.nextToken`),
                        str('$context.args.nextToken')
                    ),
                    true
                ),
                iff(
                    ref('context.args.filter'),
                    set(
github aws-amplify / amplify-cli / packages / graphql-elasticsearch-transformer / src / resources.ts View on Github external
public makeSearchResolver(type: string, nameOverride?: string, queryTypeName: string = 'Query') {
        const fieldName = nameOverride ? nameOverride : graphqlName('search' + plurality(toUpper(type)))
        return new AppSync.Resolver({
            ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
            DataSourceName: Fn.GetAtt(ResourceConstants.RESOURCES.ElasticsearchDataSourceLogicalID, 'Name'),
            FieldName: fieldName,
            TypeName: queryTypeName,
            RequestMappingTemplate: print(
                compoundExpression([
                    set(ref('indexPath'), str(`/${type.toLowerCase()}/doc/_search`)),
                    ElasticsearchMappingTemplate.searchItem({
                        path: str('$indexPath'),
                        size: ifElse(
                            ref('context.args.limit'),
                            ref('context.args.limit'),
                            int(10),
                            true),
                        from: ifElse(
                            ref('context.args.nextToken'),
                            ref('context.args.nextToken'),
                            int(0),
                            true),
                        query: ifElse(
                            ref('context.args.filter'),
                            ref('util.transform.toElasticsearchQueryDSL($ctx.args.filter)'),
                            obj({
                                'match_all': obj({})
                            })),
                        sort: ifElse(
github aws-amplify / amplify-cli / packages / graphql-connection-transformer / src / resources.ts View on Github external
}))
        ];
        if (sortKeyInfo) {
            setup.push(applyKeyConditionExpression(sortKeyInfo.fieldName, sortKeyInfo.attributeType, 'query'));
        }
        return new Resolver({
            ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
            DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(relatedType), 'Name'),
            FieldName: field,
            TypeName: type,
            RequestMappingTemplate: print(
                compoundExpression([
                    ...setup,
                    DynamoDBMappingTemplate.query({
                        query: raw('$util.toJson($query)'),
                        scanIndexForward: ifElse(
                            ref('context.args.sortDirection'),
                            ifElse(
                                equals(ref('context.args.sortDirection'), str('ASC')),
                                bool(true),
                                bool(false)
                            ),
                            bool(true)
                        ),
                        filter: ifElse(
                            ref('context.args.filter'),
                            ref('util.transform.toDynamoDBFilterExpression($ctx.args.filter)'),
                            nul()
                        ),
                        limit: ref('limit'),
                        nextToken: ifElse(
                            ref('context.args.nextToken'),
github aws-amplify / amplify-cli / packages / graphql-auth-transformer / src / resources.ts View on Github external
public setUserGroups(customGroup?: string): Expression {
    if (customGroup) {
      return compoundExpression([
        set(ref('userGroups'), raw(`$util.defaultIfNull($ctx.identity.claims.get("${customGroup}"), [])`)),
        iff(
          raw('$util.isString($userGroups)'),
          ifElse(
            raw('$util.isList($util.parseJson($userGroups))'),
            set(ref('userGroups'), raw('$util.parseJson($userGroups)')),
            set(ref('userGroups'), raw('[$userGroups]'))
          )
        ),
      ]);
    }
    return set(ref('userGroups'), raw(`$util.defaultIfNull($ctx.identity.claims.get("${DEFAULT_GROUP_CLAIM}"), [])`));
  }