ho un server GraphQL in esecuzione con uno schema più o meno in questo modo:Come arrivare RelayJS per capire che una risposta da GraphQL è un array di oggetti, non solo un singolo elemento
type Card {
id: String!
name: String
}
type Query {
card(name: String!): Card
cards(power: String): [Card]
}
Si noti che ho un interrogare su una singola carta, ma anche su più schede. Quando uso l'interfaccia utente GraphIQl e faccio una query come questa "query {cards {name}}" torno indietro una serie di carte, come previsto.
Tuttavia, ho un RelayContainer che sta facendo la stessa query, ma gli oggetti di scena che ritornano sono solo il primo risultato, piuttosto che una serie di risultati.
class MyApp extends React.Component {
render() {
return (
<div>
<h1> Hello world!</h1>
<CardList cards={this.props.cards} />
</div>
);
}
}
export default Relay.createContainer(MyApp, {
fragments: {
card:() => Relay.QL`
fragment on Card {
name
}
`,
cards:() => Relay.QL`
fragment on Card {
${CardList.getFragment('cards')}
}
`
},
});
e la componente cardlist è impostato con il contenitore come questo:
class CardList extends React.Component {
render() {
return <div>{this.props.cards}</div> // UNEXPECTED - this.props.cards is the first card, not an array for me to .map over
}
}
export default Relay.createContainer(CardList, {
fragments: {
cards:() => Relay.QL`
fragment on Card {
name
}
`,
},
});
Qualcuno ha qualche suggerimento? Questo è il mio secondo giorno di immersione in GraphQL e Relay, quindi potrei fare un errore molto semplice per quanto ne so.
Dai un'occhiata a come funziona l'esempio TODO Relay. Lo schema GraphQL non è corretto per Relay poiché è necessario utilizzare 'connectionsDefinition' da https://github.com/graphql/graphql-relay-js https://github.com/facebook/relay/blob/a04a51486c2576c4a0dcdb091023e1bda0d0a5fe/examples /todo/data/schema.js#L93-L100 –