Skip to content

Instantly share code, notes, and snippets.

View Apiko-tutorials's full-sized avatar

Code Tutorials [by Apiko] Apiko-tutorials

View GitHub Profile
import { Tracks, Albums, Artists } from '../../db';
import { tracksQuery } from '../utils';
export const track = (_, { id }) => Tracks.findById(id);
export const tracks = (_, args) => Tracks.find(...tracksQuery(args));
export const album = (_, { id }) => Albums.findById(id);
export const albums = (_, { limit }) => Albums.find({}, {}, { limit, sort: { rating: 1 } });
import mongoose, { Schema } from 'mongoose';
export const artistSchema = Schema({
name: String,
rating: Number,
startActivity: Date,
endActivity: Date,
country: String,
photo: String,
website: String,
type Query {
track(id: ID!): Track
tracks(
limit: Int = 20
sortBy: TrackSortable = RATING
genres: [Genre],
artists: [ID!]
countries: [String!]
): [Track]
album(id: ID!): Album
interface ArtistType {
type: ArtistTypesEnum!
}
type Single implements ArtistType {
type: ArtistTypesEnum!
birthDate: Date
instruments: [MusicianInstrument]
}
import express from 'express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { graphqlExpress, graphiqlExpress } from 'apollo-server-express';
import { makeExecutableSchema } from 'graphql-tools';
import root from './type-defs/root.graphql';
const app = express();
const schema = makeExecutableSchema({
type Track {
_id: ID! # Unique identifier
name: String!
rating: Float!
genres: [Genre]! # List of allowed genres
releasedAt: Date! # Date is a custom Scalar
label: String
logo: URL # URL is a custom Scalar too
url: URL!
artists: [Artist!]! # Non-nullable list of non-nullable Artists
schema {
query: Query
}
type Query {
hello: String
}
// send invitation to new user
sendNotification(NotificationTypes.invitation, 'some_user_id', {
// some custom data to render at notification's text
invitationLink: 'https://myservice.com/invitation/accept/abcdefg1234',
expirationDate: moment().subtract(5, 'day').format('YYYY-MM-DD'),
});
export function sendNotification(notificationType, userId, context) {
const _locale = getLocaleByUserId(userId);
NotificationTypes[notificationType].forEach((sendMethod) => {
// localize notification message for particular locale
const text = i18n.__(`notifications.${notificationType}.${sendMethod}`, { _locale, ...context });
// send message
sendersMap[sendMethod](userId, text);
});
const sendersMap = {
[SendMethod.email](userId, text) {
const email = getEmailByUserId(userId);
sendEmailBase(email, text);
},
// ... other notification types
};