Resource Entities
Entities overview
With Native external references, we introduce the following new entity types that allow us to model data from third-party systems, such as MockShop, in Contentful:
-
Resource Provider- A third-party system that provides resources. Each provider can have multiple resource types. In our tutorial example, this is a MockShop provider. -
Resource Type- A specific type of resource (not the resource itself) provided by a resource provider. In our tutorial example, this is Product.
Below is a representation of how a Resource Provider is structured, using the MockShop app as an illustrative example.
We are representing Resource Types in a similar structure:
Code walkthrough
The example app is using Functions to provide a connection between Contentful and the MockShop API. In the functions/mockShop.ts file we are defining events that are necessary for Native external references to function properly:
search- retrieval of specific content based on search querieslookup- retrieval of specific content based on URNs (IDs)graphql.resourcetype.mapping- retrieval of resource type mappings, which determines what fields in Contentful map to an external type in Graph API.graphql.query- handles GraphQL queries for the external third-party API.
Search request
Search handler expects the following shape for outgoing requests:
Lookup request
Lookup handler expects the following shape for outgoing requests:
This event is triggered to render preview cards in the Entry editor and when resolving Native External References in delivery. See CDA documentation.
Differentiate the context by checking context.originalRequest?.headers['contentful-api']: cda for Content Delivery API, cpa for Content Preview API, or undefined for CMA/WebApp requests.
referencingEntryId is not provided because there is no single referencingEntryId as lookups can batch and deduplicate URNs referenced by multiple entries.Lookup and Search response
Both events return the same shape of the response:
Functions are designed without prior knowledge of the response data structure from third-party systems. Therefore, an additional transformation is required to ensure that all Function events return responses with a consistent shape of Products. This transformation is carried out by the withBadge and withUrn functions located in the function/utils.ts file. In our example, we are expecting Resource to conform to a particular shape:
GraphQL query request
GraphQL Query handler expects the following shape for outgoing requests:
GraphQL query response
The GraphQL query response adheres to the standard GraphQL specification.
GraphQL resourcetype mapping request
GraphQL resourcetype mapping handler expects the following shape for outgoing requests:
GraphQL resourcetype mapping response
Here is the typing for GraphQLResourceTypeMapping:
GraphQLResourceTypeMapping would have the following values for the product field:
Property mapping for rendering in the Web app
The Contentful web app displays entries using components that require specific data structures to fill their UI elements.
The mapping between these components and external system data is established using JSON pointers. This mapping is defined in the defaultFieldMapping property of each Resource Type and must adhere to the structure used for mapping the values shown in the entry component:
The definitions of Product Resource Type representations can be found in the src/tools/entities/product.json file, respectively.
Create additional resource type
In our example, we focus on a single Resource Type: Product, because the MockShop API provides only one type: product. If you want to include additional resource types, consider the following:
- Discover more entities available in the third party system.
- Create a new entity file in
src/tools/entities/, similar tosrc/entities/product.jsonwith a Resource Type definition. - Update the
src/tools/import.tsfile to import the newly created JSON file. - Update the resource entities creations script in
src/tools/create-resource-entities.tsto include the new type. - Update the resources by running the creation script:
npm run create-resource-entities. - Build the function and update the bundled version stored in Contentful: