简体   繁体   English

Relay.js无法正确解析组成的片段

[英]Relay.js is not resolving composed fragment correctly

I'm working on converting a Flux app to Relay.js and I'm running into some issues. 我正在将Flux应用程序转换为Relay.js,但遇到了一些问题。 I can't seem to get component fragment composition to work properly. 我似乎无法使组件片段组成正常工作。 The correct data comes back from the server, but the composed data is never passed back into the props of the parent component for some reason. 正确的数据从服务器返回,但是由于某种原因,组合的数据永远不会传递回父组件的props中。

here's my code so far: 到目前为止,这是我的代码:

LibraryLongDescription.js LibraryLongDescription.js

import React, {Component, PropTypes} from 'react';
import Relay from 'react-relay';
import DocumentTitle from 'react-document-title';
import Address from '../components/Address';
import Orders from '../pages/Orders';

export default class LibraryLongDescription extends Component {
    constructor(props)
    {
        super(props);

        this.state = {
            library: {}
        };

        console.log(props);
        if(props.library){
            this.state.library = props.library;
        }
    }

    render()
    {
        return (
            <DocumentTitle title="Libraries">
                <div>
                    {this.state.library.name}
                    <div className="row">
                        <Address type={"Address"} address={this.state.library.address} />
                    </div>

                    <div className="top-space-60">
                        <h3>Orders</h3>
                        <Orders />
                    </div>


                </div>
            </DocumentTitle>
        );
    }
}


export default Relay.createContainer(LibraryLongDescription, {
    fragments: {
        library: () => Relay.QL`fragment on Library {
            id,
            name,
            address{
                id
                sanNumber,
                addressLine1,
                addressLine2,
                city,
                state,
                zip
            },
            ${Orders.getFragment('orders')}
        }`,
    }
});

Orders.js Orders.js

import React, {Component, PropTypes} from 'react';
import Relay from 'react-relay';

class Orders extends Component {
    constructor(props)
    {
        super(props);
        console.log(props);
    }

    render()
    {
        return (<h1>This is where the order goes</h1>);
    }
}

export default Relay.createContainer(Orders, {
    fragments: {
        orders: () => Relay.QL`fragment on Library {
            orders(first: 10){
                edges{
                    node{
                        id,
                        message_number,
                        order_total
                    }
                }
                pageInfo{
                    hasPreviousPage,
                    hasNextPage
                }
            }
        }`
    }
});

This does not resolve correctly. 这不能正确解决。 When I console log props in LibraryLongDescription.js I get all the values from that query, but I don't get anything from the Orders fragment. 当我在LibraryLongDescription.js中管理日志道具时,我从该查询中获取了所有值,但从Orders片段中却没有任何获取。 When I look to see what came over the network I get data in this form: 当我查看通过网络传输的内容时,我会以这种形式获取数据:

{  
   "data":{  
      "library":{  
         "id":"valid",
         "name":"valid",
         "address":{  
            correct data
         },
         "_orders1mpmPX":{  
            "edges":[  
               {  
                  "node":{  
                     correct data
                  },
                  "cursor":"correct data"
               },
            ],
            "pageInfo":{  
               correct data
            }
         }
      }
   }
}

but when I console log props from library Long description I don't see anything for orders. 但是当我从库中对日志道具进行控制台详细说明时,我看不到任何订单。 I also get this property: __fragment__ which seems to not really have anything useful on it. 我也得到了这个属性: __fragment__似乎没有什么用。 Any help with this would be greatly appreciated. 任何帮助,将不胜感激。 I've been searching the internet for solutions for hours. 我一直在互联网上寻找解决方案数小时。 If there's any info I did not provide that would be of use let me know. 如果有我未提供的任何信息,请告诉我。

After spending a stupid amount of time trying to solve this issue I have discovered relay does not like you defining a type field in a fragment query. 在花费了愚蠢的时间尝试解决此问题之后,我发现中继不喜欢您在片段查询中定义类型字段。 here's what I mean... the library query changed to this: 这就是我的意思。。。库查询更改为:

export default Relay.createContainer(LibraryLongDescription, {
    fragments: {
        library: () => Relay.QL`
            fragment on Library {
                id,
                name,
                address{
                    id
                    sanNumber,
                    addressLine1,
                    addressLine2,
                    city,
                    state,
                    zip
                },
                orders(first: 500){
                    ${Orders.getFragment('orders')}
                }
            }
        `,
    }
});

and the orders query changed to this: 订单查询更改为:

export default Relay.createContainer(Orders, {
    fragments: {
        orders: () => Relay.QL`fragment on OrderConnection {
            edges{
                node{
                    id
                    purchaseDate
                }
            }
            pageInfo{
                hasPreviousPage
                hasNextPage
            }
        }`
    },
});

if you don't have some sort of root field like orders defined on the parent, relay won't know how to resolve that field back to the parent to be passed back into your child component. 如果您没有某种根字段,例如在父级上定义的订单,那么中继将不知道如何将该字段解析回父级,然后再传递回您的子级组件。 by doing this: orders(first: 500) you are declaring that name as a dependency for relay to pass it into that component. 通过执行以下操作: orders(first: 500)您将该名称声明为中继将其传递到该组件的依赖项。 Without that name relay does not see your component requiring that name and it won't pass it. 没有该名称的中继不会看到您的组件需要该名称,并且该名称也不会通过。 I hope this helps someone else out someday. 我希望这有一天能帮助别人。 I spent more than a full day trying to figure this one out. 我花了整整一天的时间试图弄清楚这一点。

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

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