Apollo-client: Question: What is the proper way to use multiple fragments in a readFragment call (`fragmentName` must be provided when there is not exactly 1 fragment)

Created on 23 Jan 2018  ·  1Comment  ·  Source: apollographql/apollo-client

Intended outcome:
I am trying to read an item (book) previously added to the cache. For this I am re-using an existing fragment.

const result = this.props.client.readFragment({
  id: this.props.book.id,
  fragment: gql`fragment BookDetail on Book {
      ...BookCover
      isEditable
    }
    ${BookCoverFragment}
  `
})

const BookCoverFragment = gql`
  fragment BookCover on Book {
    __typename
    id
    title
}
`

I expect result to be the Book data populated from the cache. It exists in the cache and it works when adding the fields (__typename, id, title) directly to the BookDetail fragment instead of using the BookCover fragment.

Actual outcome:

I get an error:

Found 2 fragments.fragmentNamemust be provided when there is not exactly 1 fragment.

I know this is intended but what is the correct way to write multiple fragments in a readFragment call? I can't seem to find this documented anywhere.

Thank you.

How to reproduce the issue:

Version

Most helpful comment

I see - the read fragment options take a fragmentName prop that specifies the name of the entry fragment to use:

const result = this.props.client.readFragment({
  id: this.props.book.id,
  fragment: gql`fragment BookDetail on Book {
      ...BookCover
      isEditable
    },
    ${BookCoverFragment}
  `,
  fragmentName: "BookDetail",
})

Needs some doco here:
https://www.apollographql.com/docs/react/reference/index.html#ApolloClientmd5-fe68869488ca9f5c636933ba74eb9a3breadFragment

>All comments

I see - the read fragment options take a fragmentName prop that specifies the name of the entry fragment to use:

const result = this.props.client.readFragment({
  id: this.props.book.id,
  fragment: gql`fragment BookDetail on Book {
      ...BookCover
      isEditable
    },
    ${BookCoverFragment}
  `,
  fragmentName: "BookDetail",
})

Needs some doco here:
https://www.apollographql.com/docs/react/reference/index.html#ApolloClientmd5-fe68869488ca9f5c636933ba74eb9a3breadFragment

Was this page helpful?
0 / 5 - 0 ratings