如何在GraphQL中返回函数结果

2024-04-19 18:52:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在前端使用React、Apollo和GraphQL,在后端使用Django、Python和Graphene

我想实现搜索栏,当用户点击搜索按钮时,我想用用户输入执行GraphQL查询

在后端,我想对该字符串进行绘图,并将其传递给函数,然后将函数返回的结果传递回前端

我的代码如下:

前端

export const GET_RESULTS = gql`
query SearchVinResults($vin: String!){
  searchVinResults(vin: $vin) { 
        id
        label
        url
   }
  }`;


class VinSearch extends Component {       
    onVinSearch(e) {
        e.preventDefault();
        const {value, client: {query}} = this.props;
        query({query: GET_RESULTS, variables: { vin: value }});
    }

    render() {
        const {value, clearSearch, onChange} = this.props;
        return (
            <SearchField
                onChange={e => onChange(e)}
                clearSearch={() => clearSearch()}
                value={value}
                clearIcon="fal fa-times"
                placeholder="Search VIN"
                withButton={true}
                buttonStyles={{borderLeftWidth: 0}}
                onClick={e => this.onVinSearch(e)}
            />
        );
    }
}

export default withApollo(VinSearch);

后端

类查询(对象): 搜索vin结果=我需要在这里做什么

def resolve_search_vin_results(self, info, **kwargs):
    # Here I want to do something like

    vin = info['vin']
    results = get_vins(vin)
    return results

有什么想法吗


Tags: 函数用户getvalueexportthisquerygraphql