如何将变量传递给GraphQL过滤器查询?

时间:2019-09-02 14:00:35

标签: reactjs gatsby

我正在尝试查看是否有可能将变量传递给查询。

这是我的组件 data.js

 protected void egrd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (GridViewRow gvr in egrd.Rows)
        {
            string val = egrd.DataKeys[gvr.RowIndex].Values[0].ToString();

            Literal ltr = egrd.FindControl("ltrbalance") as Literal;

            string str = "";
            DataTable dt = new DataTable();
            str = "SELECT       a.[ID] ,a.[strUserName],";
            str += " (select intCurrentBalance from accountmappings where intUserID = a.[ID] and intAccountType = 1 ) as Prm_Balance ";
            str += " ,(select intCurrentBalance from accountmappings where intUserID = a.[ID] and intAccountType = 2 ) as Trn_Balance ";
            str += " ,(select intCurrentBalance from accountmappings where intUserID = a.[ID] and intAccountType = 3 ) as Opt_Balance ";
            str += " FROM users a where ID='" + int.Parse(val) + "'";

            dt = obj.Get_Data_Table_From_Str(str);

            switch (ddl_accounttype.SelectedItem.Text.ToString())
            {
                case "All":

                    ltr.Text = "<SPAN>Promotional : " + dt.Rows[0]["Prm_Balance"].ToString() +
                               " </br> Transactional : " + dt.Rows[0]["Trn_Balance"].ToString() +
                               " <br/> Optin : " + dt.Rows[0]["Opt_Balance"].ToString() + "</SPAN>";
                    break;
                case "Promotional":
                    ltr.Text = "<SPAN>Promotional : " + dt.Rows[0]["Prm_Balance"].ToString() + " </SPAN>";
                    break;
                case "Transactional":
                    ltr.Text = "<SPAN>Transactional : " + dt.Rows[0]["Trn_Balance"].ToString() + "</SPAN>";
                    break;
                case "OptIn":
                    ltr.Text = "<SPAN>Optin : " + dt.Rows[0]["Opt_Balance"].ToString() + "</SPAN>";
                    break;
                default:
                    ltr.Text = "<SPAN>Promotional : " + dt.Rows[0]["Prm_Balance"].ToString() +
                               " </br> Transactional : " + dt.Rows[0]["Trn_Balance"].ToString() +
                               " <br/> Optin : " + dt.Rows[0]["Opt_Balance"].ToString() + "</SPAN>";
                    break;
            }
        }
    }

然后

export default function Data() {
    const data = useStaticQuery(
        graphql`
            query {
                allGoogleSheetDataRow(filter: { state: { eq: "ALABAMA" } }) {
                    edges {
                        node {
                            snap
                            tanf
                            medicaid
                            unemployment
                        }
                    }
                }
            }
        `
    );
    return (
    .
    .
    . 
    .
    );
}

这就是我目前正在做的事情,如果只有一页,也可以。我认为使组件“可重用”的正确方法是,在这里,我想知道如何将变量传递给过滤器

import DATA from './src/components/data' <DATA />

所以我可以用

导入
allGoogleSheetDataRow(filter: { state: { eq: "--- some variable --" } })

知道的人能指出我正确的方向吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您使用的是盖茨比(我假设这是useStaticQuery的来源),那么您就不能这样做。名称中的Static表示您不能在其中使用变量。可悲的是:(

您可能可以在此处了解更多信息:https://spectrum.chat/gatsby-js/general/using-variables-in-a-staticquery~abee4d1d-6bc4-4202-afb2-38326d91bd05


或者,您可以创建一个自定义的“ useQuery”,这是一种命名返回值的函数的好方法。 ;)

为此,您有:

const useCustomQuery = () => {
  const data = useStaticQuery(
    graphql`
      query {
          allGoogleSheetDataRow(filter: { state: { eq: "ALABAMA" } }) {
              edges {
                  node {
                      snap
                      tanf
                      medicaid
                      unemployment
                  }
              }
          }
      }
    `
  );
  return data
}

并且在“页面”或您需要数据的任何组件中,您只需要使用

const Component = () => {
  const data = useCustomQuery()

  // use data anyway you want
}