help in converting fragments query

default discord avatar
megetron
last month
7

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}
`
  • default discord avatar
    TheDunco
    last month

    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

  • default discord avatar
    megetron
    last month

    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 ).

  • default discord avatar
    TheDunco
    last month

    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

  • default discord avatar
    megetron
    last month

    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

  • default discord avatar
    TheDunco
    last month

    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

  • default discord avatar
    megetron
    last month

    ok cool i will try that, thanks

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.