Skip to content

Instantly share code, notes, and snippets.

@tomitrescak
Created June 20, 2016 16:17
Show Gist options
  • Save tomitrescak/07450392517a6d3b198ab1fb3eeee2a0 to your computer and use it in GitHub Desktop.
Save tomitrescak/07450392517a6d3b198ab1fb3eeee2a0 to your computer and use it in GitHub Desktop.
// file composer
interface IApolloDefinition {
schema: string;
queries?: Object;
resolvers?: Object;
mutations?: Object;
queryText?: string;
mutationText?: string;
}
let queries = '';
let mutations = '';
export const schema: any[] = [];
export const resolvers = {
RootQuery: {},
RootMutation: {}
};
function process(apolloDefinitions: IApolloDefinition[]) {
for (let apolloDefinition of apolloDefinitions) {
if (apolloDefinition.schema) {
schema.push(apolloDefinition.schema);
}
if (apolloDefinition.queries) {
Object.assign(resolvers.RootQuery, apolloDefinition.queries);
}
if (apolloDefinition.mutations) {
Object.assign(resolvers.RootMutation, apolloDefinition.mutations);
}
if (apolloDefinition.resolvers) {
Object.assign(resolvers, apolloDefinition.resolvers);
}
if (apolloDefinition.queryText) {
queries += apolloDefinition.queryText + '\n';
}
if (apolloDefinition.mutationText) {
mutations += apolloDefinition.mutationText + '\n';
}
}
// add all the queries and mutations
queries = `
type RootQuery {
${queries}
}
`;
schema.push(queries);
mutations = `
type RootMutation {
${queries}
}
`;
schema.push(mutations);
}
// example of processing
import achievements from './schemas/achievement_schema';
process([
achievements,
]);
///////////////////////////////////////////////
// achievemnt_schema
import * as Collections from '../../../lib/collections/collections';
const schema = `
scalar Date
type NotificationParameter {
scheduleName: String,
practicalName: String,
exerciseName: String,
rank: Int,
count: Int,
mark: Float
}
type Notification {
_id: String,
userId: String,
code: String,
date: Float,
parameters: NotificationParameter
}
`;
const queryText = `
notifications: [Notification]
`;
const queries = {
notifications(root: any, params: any, context: any) {
return Collections.Notifications.find({ userId: context.userId }, { sort: { date: -1 }, limit: 20 }).fetch();
}
};
const resolvers = {
Notification: {
date(notification: any) {
return notification.date.getTime();
},
parameters(notification: any) {
return notification.parameters;
}
}
}
const mutations: any = null;
export default {
schema,
queryText,
queries,
resolvers,
mutations,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment