Documentation Center

Approaches to defining a custom GraphQL Schema

You can approach the definition of a custom schema in two ways. The first approach, adding a custom method to the Query type, only allows you to fetch JSON content, no other fields, and restricts you to using predefined parameters for search. The second approach, implementing the Content interface, allows you to use existing GraphQL methods and transform the JSON returned to return into smaller and more efficient JSON, rather than returning all JSON as untyped content.

Approach 1: Adding a custom method to the Query type
In your custom schema, find the type Query section, which reads as follows:
type Query {
  getCpContent(namespaceId: Int! publicationId: Int! componentId: Int! templateId: Int) : CdContent @dataProvider(dataSource: pipelineComponentPresentation)
}
Add a new method in this section called, for example, getMyCpContent. It has the same signature as the normal method, but references your custom data type, called, for example, MyCdContent. This custom method returns a Component Presentation object transformed using the custom data type. Here's the result:
type Query {
  getCpContent(namespaceId: Int! publicationId: Int! componentId: Int! templateId: Int) : CdContent @dataProvider(dataSource: pipelineComponentPresentation)
  getMyCpContent(namespaceId: Int! publicationId: Int! componentId: Int! templateId: Int) : MyCdContent @dataProvider(dataSource: pipelineComponentPresentation)
}
Also, directly below the type Query section, add the definition of your custom data type (in this case, we imagine that MyCdContent consists of two strings, a headline and a body):
type MyCdContent {
  headline: String
  body: String
}
You can add as many methods to the type Query section as you like, so long as each has a unique name. They can all reference the same custom data type, or each can reference its own custom data type, so long as all custom data types that are used are included.
Approach 2: Implementing the Content interface
This approach requires you to use the custom data type in all existing GraphQL methods instead of UntypedContent. To use this approach, in your custom schema, add two sections, one for your Content implementation (here called ArticleContent), and another to define the custom data type (here called ArticleContentBody):
type ArticleContent implements Content {
  id: ID
  implementationName: String @typeName
  type: String @source(path: "$.$type")
  data: ArticleContentBody
}

type ArticleContentBody {
  type: String
  info: String @source(path: "$.Regions[0].ComponentPresentations[0].Component.Title")
}
Here's an example of a query using the custom data type:
{
  items(filter:: {itemTypes: [PAGE], publicationIds: [8], namespaceIds: [1]}) {
    edges {
      node {
        ... on Page {
          itemId
          content {
            ... on ArticleContent {
              data {
                type
                info
              }
            }
          }
        }
      }
    }
  }
}