Home > Blockchain >  How to fetch graphQl fragment?
How to fetch graphQl fragment?

Time:03-02

I need to set fragment to my gql. It is working code:

const findEntity = gql`
  query findEntity($pagination: Pagination, $sort: Sort) {
    organizationList(pagination: $pagination, sort: $sort) {
      data {
        _id
        name
      }
    }
  }
`;

This code doesn't work:

const TEST_FRAGMENT = gql`
  fragment organizationList on OrganizationsPage {
    data {
      _id
      name
    }
  }
`;

const findEntity = gql`
  query findEntity($pagination: Pagination, $sort: Sort) {
    organizationList(pagination: $pagination, sort: $sort) {
      ${TEST_FRAGMENT}
    }
  }
`;

errors: [{message: "Cannot query field "fragment" on type "OrganizationsPage".",…},…]

What I do wrong? I think I repeated documentation sample...

CodePudding user response:

Fragments use pseudo-spread syntax and you need to include them in the query. I also used caps for the fragment name for convention compliance.

const findEntity = gql`
  ${TEST_FRAGMENT}
  query findEntity($pagination: Pagination, $sort: Sort) {
    organizationList(pagination: $pagination, sort: $sort) {
      ...OrganizationListFields
    }
  }
`;
  • Related