2016-11-08 15 views
1

我使用django和django graphene來製作graphql API。Graphene-django - 如何捕捉查詢的響應?

在我的申請中,我使用reactJS和react-bootstrap-table。 React-bootstrap-table期望我將它傳遞給一個對象數組,但不支持nested objects

我創建的查詢在我schema.py

class ApplicationNode(DjangoObjectType): 
    class Meta: 
     model = Application 
     filter_fields = ['name', 'sonarQube_URL'] 
     interfaces = (relay.Node,) 

class Query(ObjectType): 
    application = relay.Node.Field(ApplicationNode) 
    all_applications = DjangoFilterConnectionField(ApplicationNode) 

這些疑問的答案是JSON對象嵌套這樣的:

{ 
    "data": { 
    "allApplications": { 
     "edges": [ 
     { 
      "node": { 
      "id": "QXBwbGljYXRpb25Ob2RlOjE=", 
      "name": "foo", 
      "sonarQubeUrl": "foo.com", 
      "flow":{ 
       "id": "QYBwbGljYXRpb45Ob2RlOjE=", 
       "name": "flow_foo" 
      } 
      } 
     }, 
     { 
      "node": { 
      "id": "QXBwbGljYXRpb25Ob2RlOjI=", 
      "name": "bar", 
      "sonarQubeUrl": "bar.com" 
      "flow":{ 
       "id": "QXBwbGljYXRpb26Ob2RlOjA=", 
       "name": "flow_bar" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

我必須把它們平給他們之前到React-bootstrap-table。

什麼是更好的方法,截獲graphene-django查詢的結果,以使它們平坦或在ReactJS視圖中完成這項工作?

如果第一種方式更好,如何攔截graphene-django查詢的結果以使它們平坦?

回答

2

最好的辦法是將react-bootstrap-table包裝在一個新組件中。在組件中,根據需要將繼電器支撐件按摩成平坦結構以用於反應引導表。

例如:

MyReactTable = ({allApplications}) => { 
    let flatApplications = allApplications.edges.map(({node: app}) => { 
    return { 
     name: app.name, 
     sonarQubeUrl: app.sonarQubeUrl, 
     flowName: app.flow.name 
    }; 
    }); 
    return (
    <BootstrapTable data={flatApplications} striped={true} hover={true}> 
     <TableHeaderColumn dataField="name" isKey={true} dataAlign="center" dataSort={true}>Name</TableHeaderColumn> 
     <TableHeaderColumn dataField="sonarQubeUrl" dataSort={true}>Sonar Qube Url</TableHeaderColumn> 
     <TableHeaderColumn dataField="flowName" dataFormat={priceFormatter}>Flow Name</TableHeaderColumn> 
    </BootstrapTable> 
); 
};