Skip to content

Commit

Permalink
Merge pull request #82 from Kentico/issue/linkeditemsschema
Browse files Browse the repository at this point in the history
Fix Kontent item relations
  • Loading branch information
Simply007 committed Nov 14, 2019
2 parents 91079e0 + d342b85 commit dcd978e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 40 deletions.
34 changes: 13 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ Should a *Linked items* element in KC contain items of only *one* type, you'll b

The `related_project_refereces.linked_items` will give you the full-fledged Gatsby GraphQL nodes with all additional properties and links.

> :bulb: Notice the encapsulation into the `... on Node` [GraphQL inline fragment](https://graphql.org/learn/queries/#inline-fragments). This prevent failing creating GraphQL model when this field does not contain i.e. Blog post (`KontentItemBlogpostReference`) linked item.
```gql
{
allKontentItemProjectReference {
Expand All @@ -185,20 +183,17 @@ The `related_project_refereces.linked_items` will give you the full-fledged Gats
type
itemCodenames
linked_items {
... on Node {
__typename
... on KontentItemBlogpostReference {
elements {
title {
value
}
... on KontentItemBlogpostReference {
elements {
title {
value
}
}
... on KontentItemProjectReference {
elements {
project_name {
value
}
}
... on KontentItemProjectReference {
elements {
project_name {
value
}
}
}
Expand Down Expand Up @@ -274,13 +269,10 @@ As with the previous example, all rich text element containing [inline content i
summary {
value
linked_items {
... on Node {
__typename
... on KontentItemBlogpostReference {
elements {
title {
value
}
... on KontentItemBlogpostReference {
elements {
title {
value
}
}
}
Expand Down
38 changes: 37 additions & 1 deletion docs/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,46 @@ Rich text elements internal structure was extended. The main difference is that
}
```

### Schema definition API & All items query
### Schema definition API

Thanks to [#80](https://github.com/Kentico/gatsby-source-kontent/pull/80) it is possible remove the [fully filled dummy content items](https://github.com/Kentico/gatsby-source-kontent/issues/59#issuecomment-496412677) from Kentico Kontent to provide Gatsby inference engine information about content structure.

For linked items in linked items element nor for rich text element encapsulation into the `... on Node` [GraphQL inline fragment](https://graphql.org/learn/queries/#inline-fragments) is not required any more ([#82](https://github.com/Kentico/gatsby-source-kontent/pull/82)).

```gql
{
allKontentItemProjectReference {
nodes {
elements {
related_project_references {
linked_items {
... on Node { // NOT REQUIRED
__typename
... on KontentItemBlogpostReference {
elements {
title {
value
}
}
}
... on KontentItemProjectReference {
elements {
project_name {
value
}
}
}
}
}
}
}
}
}
}
```

#### All Item query

As a part of that adjustment there are two queries (`allKontentItem` and `kontentItem`) allows to load content items from unified endpoint regardless of type

```gql
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kentico/gatsby-source-kontent",
"version": "4.0.0-beta3",
"version": "4.0.0-beta4",
"description": "Gatsby source plugin for Kentico Kontent",
"main": "./gatsby-node.js",
"scripts": {
Expand Down Expand Up @@ -45,7 +45,7 @@
"rxjs": "^6.5.3"
},
"peerDependencies": {
"gatsby": ">=2.1.18"
"gatsby": ">=2.2.0"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Array [
name: String!
type: String!
itemCodenames: [String]
value: [KontentItem] @link(by: \\"system.codename\\")
linked_items: [KontentItem] @link(by: \\"id\\", from: \\"linked_items___NODE\\")
}
type KontentMultipleChoiceElement implements KontentElement @infer {
name: String!
Expand All @@ -84,7 +84,7 @@ Array [
value: String
images: [KontentRichTextImage]
links: [KontentRichTextLink]
linked_items: [KontentItem] @link(by: \\"system.codename\\")
linked_items: [KontentItem] @link(by: \\"id\\", from: \\"linked_items___NODE\\")
linkedItemCodenames: [String]
resolvedData: KontentElementRichTextResolvedData
}
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const customTrackingHeader = {
header: 'X-KC-SOURCE',
value: '@kentico/gatsby-source-kontent;4.0.0-beta3',
value: '@kentico/gatsby-source-kontent;4.0.0-beta4',
};

module.exports = {
Expand Down
23 changes: 11 additions & 12 deletions src/typeNodesSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ const changeCase = require('change-case');
* @param {function} createTypes - Gatsby function to create a type
*/
const createTypeNodesSchema = async (client, schema, createTypes) => {
createTypes(getKontentBaseTypeDefintions());
const baseSchemaTypes = getKontentBaseTypeDefinitions();
createTypes(baseSchemaTypes);

const kontentTypesResponse = await client.types().toPromise();

createTypes(
kontentTypesResponse.types.reduce(
(typeDefinitions, type) => {
const fieldTypeDefinition = createFieldDefinitionsForType(schema, type);
return typeDefinitions.concat(fieldTypeDefinition);
}, [],
),
);
const schemaTypes =
kontentTypesResponse.types.reduce((typeDefinitions, type) => {
const fieldTypeDefinition = createFieldDefinitionsForType(schema, type);
return typeDefinitions.concat(fieldTypeDefinition);
}, []);
createTypes(schemaTypes);
};

const createFieldDefinitionsForType = (schema, type) => {
Expand Down Expand Up @@ -59,7 +58,7 @@ const getElementValueType = (elementType) => {
return `Kontent${changeCase.pascalCase(elementType)}Element`;
};

const getKontentBaseTypeDefintions = () => {
const getKontentBaseTypeDefinitions = () => {
const typeDefs = `
interface KontentItem @nodeInterface {
id: ID!
Expand Down Expand Up @@ -123,7 +122,7 @@ const getKontentBaseTypeDefintions = () => {
name: String!
type: String!
itemCodenames: [String]
value: [KontentItem] @link(by: "system.codename")
linked_items: [KontentItem] @link(by: "id", from: "linked_items___NODE")
}
type KontentMultipleChoiceElement implements KontentElement @infer {
name: String!
Expand All @@ -141,7 +140,7 @@ const getKontentBaseTypeDefintions = () => {
value: String
images: [KontentRichTextImage]
links: [KontentRichTextLink]
linked_items: [KontentItem] @link(by: "system.codename")
linked_items: [KontentItem] @link(by: "id", from: "linked_items___NODE")
linkedItemCodenames: [String]
resolvedData: KontentElementRichTextResolvedData
}
Expand Down

0 comments on commit dcd978e

Please sign in to comment.