Skip to content

Instantly share code, notes, and snippets.

@fduch-stranger
Last active September 3, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fduch-stranger/419eb8b92f5de6565b3c33a416c4c7de to your computer and use it in GitHub Desktop.
Save fduch-stranger/419eb8b92f5de6565b3c33a416c4c7de to your computer and use it in GitHub Desktop.
GraphQL basics

You can copy and paste each query to the Graphiql IDE

First simple query

Get first 10 publications

Request:
{
  # Get first 10 publications
  publications(namespaceId: 1, first:10) {
    edges {
      node {
        publicationId
        title
      }
    }
  }
}

Pagination

Request:
{
 # Get first two publications with cursor

 # First two publications
 firstTwo: publications(namespaceId: 1, first:2) {
    edges {
      cursor
      node {
        publicationId
        title
      }
    }
  }
  
  # Prints the second publication 
  secondOne: publications(namespaceId: 1, first:1, after:"MQ==") {
    edges {
      cursor
      node {
        publicationId
        title
      }
    }
  }
}

Page queries

1. Get the page by ID and by URL

Request:
{
  # Get the page by ID and by URL

  byId: page(namespaceId: 1, publicationId: 5, pageId: 262) {
    url
  }
  byUrl: page(namespaceId: 1, publicationId: 5, url: "/index.html") {
    itemId
  }
}

2. Get pages by URL

Request
{
  # Get pages by url

  pages(namespaceId: 1, url: "/index.html") {
    edges {
      node {
        title
        url
      }
    }
  }
}

BinaryComponent

1. Get the binary component by ID and by URL

Request:
{
  # Get the binary component by ID and by URL

  # Get binary component by Id with binary variants filtered by url
  byId: binaryComponent(namespaceId: 1, publicationId: 5, binaryId: 287) {
    title
    multiMedia
    variants(url: "/media/company-news-placeholder_tcm5-287.png") {
      edges {
        node {
          binaryId
          description
          downloadUrl
          path
          url
        }
      }
    }
  }

  # Get binary component that has any binary variant with specified url
  byUrl: binaryComponent(namespaceId: 1, publicationId: 5, url: "/media/company-news-placeholder_tcm5-287.png") {
    title
    multiMedia
    variants {
      edges {
        node {
          binaryId
          description
          downloadUrl
          path
          url
        }
      }
    }
  }
}

ComponentPresentation

1. Get ComponentPresentation by ID

Request:
{
  # Get ComponentPresentation directly from COMPONENT_PRESENTATIONS table
  componentPresentation(namespaceId: 1, publicationId: 5, componentId: 256, templateId: 123) {
    title
    rawContent {
      data
    }
    component {
      itemId
    }
    componentTemplate {
      itemId
    }
  }
}

2. Get contained ComponentPresentation's

Request:
{
  # Get contained ComponentPresentation's
  # Note that such ComponentPresentation's are a combination of a component and template linked to the page through LINK_INFO table
  # Such combinations may not exist in COMPONENT_PRESENTATIONS table and you can not use GQL 'componentPresentation' 
  # query to fetch them directly.

  page(namespaceId: 1, publicationId: 5, pageId: 272) {
    url
    title
    containerItems {
      ... on ComponentPresentation {
        component {
          itemId
          title
        }
        componentTemplate {
          itemId
        }
      }
    }
  }
}

Note that for containerItems we are supporting only ComponentPresentation type right now!

3. Component hierarchy

Request:
{
  # Narrows down Component to specific types.
  # In response you can see that each entry has publicationId, 
  # however namespaceId available only for BinaryComponent, and id fetched only for Component
  # ContentComponent is an interface, however Component and BinaryComponent are objects.
  # BinaryComponent is multimedia component.
  page(namespaceId: 1, publicationId: 5, pageId: 272) {
    url
    title
    containerItems {
      ... on ComponentPresentation {
        component {
          multiMedia
          ... on ContentComponent {
            publicationId
          }
          ... on Component {
            id
          }
          ... on BinaryComponent {
            namespaceId
          }
        }
      }
    }
  }
}

Taxonomies

1. Category

Request:
{
  # Get first category and its children keywords
  categories(namespaceId: 1, publicationId: 5, first: 1) {
    edges {
      node {
        taxonomyType
        taxonomyId
        itemId
        itemType
        children {
          edges {
            node {
              taxonomyType
              taxonomyId
              itemId
              itemType
            }
          }
        }
      }
    }
  }
}

2. Keyword

Request:
{
  # Get keyword by ID
  keyword(namespaceId: 1, publicationId: 5, categoryId: 17, keywordId: 77) {
    itemId
    name
  }
}

3. Taxonomy connection

Request:
{
  # Get keywords and  structure groups related to the page, find parents and expand children
  # You can see structure group in response as it was deployed to ITEM_CATEGORIES_AND_KEYWORDS table,
  # but you can not query it directly using 'structureGroup' query as this SG is absent in TAXFACETS
  page(namespaceId: 1, publicationId: 5, pageId: 272) {
    url
    title
    taxonomies {
      ... on StructureGroup {
        itemId
        itemType
        taxonomyId
        taxonomyType
        key
        title
      }
      ... on Keyword {
        itemId
        itemType
        taxonomyId
        taxonomyType
        key
        title
        parent {
          itemId
          itemType
          taxonomyId
          taxonomyType
          key
          title
          children {
            edges {
              node {
                itemId
                itemType
                taxonomyId
                taxonomyType
                key
                title
              }
            }
          }
        }
      }
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment