0.0.11 • Published 2 years ago
contentful-helper v0.0.11
Contentful helper
This is an npm module that provides a REST client for fetching data from a Contentful space and a suite of GraphQL queries to do the same.
Installation
You can install this module using npm:
npm install contentful-helper
REST client
The module exports a RESTClient
class with a getComponentStyles
method for fetching data from Contentful.
Here's an example of how to use the module to fetch an array of ComponentStyle
objects filtered by a given affiliate:
import { RESTClient } from 'contentful-helper'
const space = 'your-space-id'
const accessToken = 'your-access-token'
const environment = 'master'
const client = new RESTClient(space, accessToken, environment)
const affiliate = 'some-affiliate'
const contentType = 'styleComponentStyle'
const componentStyles = await client.getComponentStyles(affiliate, contentType)
console.log(componentStyles)
GraphQL queries
The module exports a GraphQL query called getComponentStylesQuery
used to get the styleComponentStyle
content for an affiliate.
Here's an example of how to use the query to fetch an array of ComponentStyle
objects filtered by a given affiliate using Apollo client:
import { getComponentStylesQuery } from 'contentful-helper'
import { ApolloClient, HttpLink, InMemoryCache } from "@apollo/client";
const space = 'your-space-id'
const accessToken = 'your-access-token'
const environment = 'master'
const link = new HttpLink({
fetch,
uri: `https://graphql.contentful.com/content/v1/spaces/${space}/environments/${environment}r`,
headers: {
authorization: `Bearer ${accessToken}`,
'Content-Language': 'en-us',
}
})
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
});
async function getClassesGraphQL() {
const { data, loading, error } = await client.query({
query: getComponentStylesQuery('some-affiliate')
});
return data.styleComponentStyleCollection.items
}
const componentStyle = await getClassesGraphQL()
console.log(componentStyles)