#help in converting fragments query

7 messages · Page 1 of 1 (latest)

dapper crag
#

i am in a phase of converting from another headless cms.to payloadjs.

i would need to convert the below to payloadjs, but i could find how to do fragments nor a way to define the SearchInput type.

can you please give me some directions on how to start implementing this on payloadjs:

export const searchResultFragment =  `
  fragment SearchResult on SearchResult {
    productId
    productName
    description
    slug
    sku
    currencyCode
    productAsset {
      id
      preview
    }
    priceWithTax {
      ... on SinglePrice {
        value
      }
      ... on PriceRange {
        min
        max
      }
    }
  }
`

export const searchQuery = `
  query search($input: SearchInput!) {
    search(input: $input) {
      docs {
        ...SearchResult
      }
      totalDocs
    }
  }
  ${searchResultFragment}
`
tranquil plank
#

For writing/converting your GraphQL queries you can mess around in the graphql playground! https://payloadcms.com/docs/graphql/overview#graphql-playground

If you're running locally it'd be at localhost:<your port/3000>/api/graphql-playground. That's where I write all of my queries first. It has really nice self documentation and schema generation

Payload CMS

Payload ships with a fully featured and extensible GraphQL API, which can be used in addition to the REST and Local APIs to give you more flexibility.

dapper crag
#

thanks i use this cool tool. still i can't find on the documentations how to define a custom type for graphql (the SearchInput variable ).

tranquil plank
#

Payload will generate the types automatically based on your schema. So if you have a field called searchInput it will generate the schema you need to access it automatically

dapper crag
#

cool your suggestion works like a magic. and what about the fragments? do you support this as well?

for example, i would need to query with the 3 dots to spread the fragment:
...SearchResult

tranquil plank
#

I do this sort of thing all the time! Fragments, inline fragments... Payload implements a fully functional GraphQL API, so you can do anything you can normally with GraphQL (and more!)

#

You can even have relationship fields that will automatically expand and give you the fields from that related document