ReactJS / Graphql:如何导出查询以在另一个组件中使用?

时间:2018-07-31 17:32:43

标签: reactjs graphql

对于该项目的下一个阶段,我需要导出查询,以便可以使用查询变量从另一个组件中调用它。我见过的所有示例都没有使用'或'query =',因此我在语法语句上迷失了导出查询的方法。任何帮助将不胜感激。

C:/Users/Dhanu/Documents/R/win-library/3.3"

1 个答案:

答案 0 :(得分:0)

export const n6dynQuery = gql`
      query action($timestamp: Float! ){
        action(timestamp: $timestamp){
        action
        timestamp
        object{
          filename
        }
      }
    }`

在组件中将其用作:-

import {n6dynQuery} from "filepath"
const UserList = (props) => (
  <Query query={n6dynQuery} >
    {({ loading, error, data }) => {
      if (loading) return <p>Loading...</p>;
      if (error) return <p>Error</p>;

          return (

            <Item.Group divided>
              {data.action.map(action =>
                <div>
                  <ul>
                  <li>{action.action}</li>
                  <li>{action.timestamp}</li>
                  <ul>
                  {action.object.map( (obj) => {
                    return (<li>{obj.filename}</li>)
                  })}
                  </ul>
                  </ul>
                </div>
              )}

              </Item.Group>
            );
        }}
  </Query>
);

export default (UserList);
相关问题