如何在 awsappsync 中对简单的 graphQL 查询进行排序?

How to sort a simple graphQL query in awsappsync?

我需要的很简单,在 SQL 我会说我只需要一个 SORT ASCENDING 或 DESCENDING。

但是我使用 GraphQL、AppSync、DynamoDB 和 React-Native 已经有一段时间了,但我仍然不知道如何按名称对输出列表进行排序...

我使用的是简单场景,没有 React-Native Apollo,只有 Connect 组件。

我的输出列表:

class PlantScreenNoApollo extends React.Component {

...

    render() {
       
        return (
    
    <View style={{flex:1, flexDirection:"column", justifyContent:"flex-start"}}>
        
        <View style={{flex:1,
                  flexDirection:"row",
                  justifyContent:"center",
        }}>
            <TextInput style={styles.sTextInput}
                        onChangeText={ (text) => this._updateFilter(text) }
                        value={this.state.filter}/>
            <Ionicons name={"ios-search"} size={30} />
        </View>

        <View style={{  flex:1, 
                        flexDirection:"column",
                        justifyContent:"flex-start",
                        alignContent: "center",
                        minHeight: "70%",}}>
                        
        <Connect query={ this.state.filter!=="" ? 
                         graphqlOperation(queries.listPlants, {
                            filter: {
                                name: {
                                    contains: this.state.filter
                                }
                            }
                         })
                         :
                         graphqlOperation(queries.listPlants)
                        
        }>
            {
                ( { data: { listPlants }, loading, error } ) => {
                    
                    if(error) return (<Text>Error</Text>);
                    
                    if(loading || !listPlants) return (<ActivityIndicator />);
                    
                    return (
                    
                          <FlatList
                            data={listPlants.items}
                            renderItem={({item}) => {
                                return (
                                    <View style={{borderBottomWidth:1, borderBottomColor:"green", padding:5}}>
                                    <TouchableOpacity onPress={()=>this._onPress(item)}>
                                        <View style={styles.hcontainer}>
                                            <Image source={{uri:this.state.logoURL}}
                                                style={styles.iconImage}
                                            />
                                            <View style={styles.vcontainer}>
                                                <Text style={styles.textH3}>{item.name}</Text>
                                                <View style={styles.hcontainerflexstart}>
                                                    { item.tea &&  <Image source={{uri:this.state.teaIconURL}} style={styles.iconImageSmall} />}
                                                    { item.bath &&  <Image source={{uri:this.state.bathIconURL}} style={styles.iconImageSmall} />}
                                                    { item.insence &&  <Image source={{uri:this.state.insenceIconURL}} style={styles.iconImageSmall} />}
                                                    { item.children &&  <Image source={{uri:this.state.childrenIconURL}} style={styles.iconImageSmall} />}
                                                </View>
                                            </View>
                                            <Text style={styles.textP}>{item.description.substr(0,50) + "(...)"}</Text>
                                        </View>
                                    </TouchableOpacity>    
                                    </View>
                                );
                            }}
                            keyExtractor={(item, index) => item.id}
                        />

                    );
                    
                    
                }
            }
        </Connect>
        </View>
        <View style={{flex:1, flexDirection:"column", justifyContent:"flex-start"}}>


...

        </View>
    </View>
      );  
    };
    
}

这是我的查询描述(由模型 + codegen 自动生成)

export const listPlants = `query ListPlants(
  $filter: ModelPlantFilterInput
  $limit: Int
  $nextToken: String
) {
  listPlants(filter: $filter, limit: $limit, nextToken: $nextToken) {
    items {
      id
      name
      description
      ...
    }
    nextToken
  }
}

我只想按名称对扫描进行排序。

我什至尝试创建二级索引,但它没有改变任何东西...

有谁知道完成此任务的最佳方法吗?

更改您的 listPlants 解析器以执行查询而不是扫描,您可以利用 scanIndexForward 布尔值来确定是否要 return 根据提供的索引(在您的案例 - 'name').

这将要求您在 DynamoDB 中为名称属性编制索引(如果尚未编制索引)。

有关详细信息 - 请查看以下文档中的查询部分:

特别是:

ScanIndexForward

Specifies the order for index traversal: If true (default), the traversal is performed in ascending order; if false, the traversal is performed in descending order.

Items with the same partition key value are stored in sorted order by sort key. If the sort key data type is Number, the results are stored in numeric order. For type String, the results are stored in order of UTF-8 bytes. For type Binary, DynamoDB treats each byte of the binary data as unsigned.

If ScanIndexForward is true, DynamoDB returns the results in the order in which they are stored (by sort key value). This is the default behavior. If ScanIndexForward is false, DynamoDB reads the results in reverse order by sort key value, and then returns the results to the client.