简体   繁体   English

如何使用 Apollo GraphQL 更改查询值(其中:{})

[英]How change query value (where : { }) with Apollo GraphQL

with graphQL we can fetch data with "where"使用 graphQL 我们可以使用“where”获取数据

query{
  activities(where: {date: "2020-08-01"}){
    date
    collect
  }
}

How change the date value to params ?如何将日期值更改为 params ?

I try this :我试试这个:

let date = "2020-01-08"
const { loading, error, data } = useQuery(GET_ACTIVITY, {
    variables: {
      input: {
        where: { date: date }
      }
    }
  });

First theschema definition from your back-end must be something like this:首先,来自后端的架构定义必须是这样的:

type Activity {
  date: String
  collect: String
  ...
}

type Query {
  activities(date: String): [Activity]
  ...
}

then in the front-end create the query using the gql function from @apollo/client :然后在前端使用@apollo/clientgql函数创建查询:

import { gql, useQuery } from '@apollo/client';

...

const GET_ACTIVITY = gql`
  query Activities($date: String!) {
    activities(date: $date){
      date
      collect
    }
  }
`;

And finally call it like this:最后这样称呼它:

let date = "2020-01-08";
const { loading, error, data } = useQuery(GET_ACTIVITY, {
  variables: {
    date
  }
});

More about queries: https://www.apollographql.com/docs/react/data/queries有关查询的更多信息: https : //www.apollographql.com/docs/react/data/queries

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

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