Skip to content

Instantly share code, notes, and snippets.

@Oluwasetemi
Last active August 10, 2022 18:33
Show Gist options
  • Save Oluwasetemi/05dbcd9999212271b300a1b9e316e65f to your computer and use it in GitHub Desktop.
Save Oluwasetemi/05dbcd9999212271b300a1b9e316e65f to your computer and use it in GitHub Desktop.
A list of the REST data source I implemented using Apollo REST Data source
Contentful Apollo Rest Data Source
I wrote a rest data source for one of my client's
project to fetch data from contentful api to
a graphql backend. For more information - https://www.apollographql.com/docs/apollo-server/data/data-sources/
/* eslint-disable camelcase */
/* eslint-disable class-methods-use-this */
const { RESTDataSource } = require('apollo-datasource-rest');
class ContentfulAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = `https://cdn.contentful.com/spaces/${
process.env.CONTENTFUL_SPACE_ID
}`;
}
willSendRequest(request) {
// request.params.set(
// 'select',
// 'sys.id,fields.title,fields.body,fields.createdAt,fields.tags,fields.author,fields.authorRole'
// );
request.headers.set(
'Authorization',
`Bearer ${process.env.CONTENTFUL_ACCESS_TOKEN}`
);
}
async entry(id) {
const data = await this.get(
`/entries/${id}?select=sys.id,fields.title,fields.body,fields.createdAt,fields.tags,fields.author,fields.authorRole`
);
const entries = JSON.parse(data);
return entries;
}
async entriesByContentType(content_type = 'fitness', limit = 10, skip = 0) {
const data = await this.get(
`/entries?content_type=${content_type}&limit=${limit}&skip=${skip}&select=sys.id,fields.title,fields.body,fields.createdAt,fields.tags,fields.author,fields.authorRole,fields.image`
);
const entries = JSON.parse(data);
return entries;
}
async entries(limit = 10, skip = 0) {
const data = await this.get(`/entries?limit=${limit}&skip=${skip}`);
const entries = JSON.parse(data);
return entries;
}
async singleAsset(id) {
const data = await this.get(`/assets/${id}`);
const entries = JSON.parse(data);
return entries;
}
}
module.exports = ContentfulAPI;
/* eslint-disable camelcase */
/* eslint-disable class-methods-use-this */
const { RESTDataSource } = require('apollo-datasource-rest');
class WgerdotdeAPI extends RESTDataSource {
constructor() {
super();
this.baseURL = `https://wger.de/api/v2`;
}
willSendRequest(request) {
// request.headers.set(
// 'Authorization',
// `Bearer ${process.env.CONTENTFUL_ACCESS_TOKEN}`
// );
// request.params.set('format', 'json');
// request.params.set('language', '2');
// request.params.set('status', 2);
}
async test() {
try {
const data = await this.get(`/`);
// const allApi = JSON.parse(data);
return data;
} catch (error) {
throw new Error(error.message);
}
}
async exerciseList(limit = 10, skip = 0) {
const data = await this.get(
`/exercise?format=json&limit=${limit}&offset=${skip}&language=2&status=2`
);
return data;
}
async exerciseImageList(limit = 10, skip = 0) {
const data = await this.get(
`/exerciseimage/?format=json&limit=${limit}&offset=${skip}&language=2&status=2`
);
return data;
}
async exerciseInfo(limit = 10, skip = 0) {
const data = await this.get(
`/exerciseinfo/?format=json&limit=${limit}&offset=${skip}&language=2&status=2`
);
return data;
}
async exerciseImage(id) {
const data = await this.get(
`/exerciseimage/${id}?format=json&language=2&status=2`
);
return data;
}
async exercise(id) {
const data = await this.get(
`/exercise/${id}?format=json&language=2&status=2`
);
return data;
}
async exerciseCategory(id) {
const data = await this.get(
`/exercisecategory/${id}?format=json&language=2&status=2`
);
return data;
}
}
module.exports = WgerdotdeAPI;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment