graphql-dataloader-boilerplate
graphql-dataloader-boilerplate copied to clipboard
Add tags on each release
Add tags on each release so it reduces the bugs that it may cause on create-graphql when refactoring the structure.
loader
// @flow
import DataLoader from 'dataloader';
-import ConnectionFromMongoCursor from '../connection/ConnectionFromMongoCursor';
-import ExampleModel from '../../model/Example';
+import { Example as ExampleModel } from '../../model';
+import connectionFromMongoCursor from './ConnectionFromMongoCursor';
import mongooseLoader from './mongooseLoader';
+import type { ConnectionArguments } from 'graphql-relay';
+import type { GraphQLContext } from '../TypeDefinition';
+
type ExampleType = {
id: string,
_id: string,
type ExampleType = {
avaible: boolean,
createdAt: string,
updatedAt: string,
-}
+};
export default class Example {
id: string;
export default class Example {
createdAt: string;
updatedAt: string;
- static getLoader = () => new DataLoader(ids => mongooseLoader(ExampleModel, ids));
-
- constructor(data: ExampleType) {
+ constructor(data: ExampleType, { example }: GraphQLContext) {
this.id = data.id;
this._id = data._id;
this.name = data.name;
export default class Example {
this.createdAt = data.createdAt;
this.updatedAt = data.updatedAt;
}
+}
- static viewerCanSee(viewer, data) {
- // TODO: handle security
-
- return true;
- }
+export const getLoader = () => new DataLoader(ids => mongooseLoader(ExampleModel, ids));
- static async load({ user: viewer, dataloaders }, id) {
- if (!id) {
- return null;
- }
+const viewerCanSee = (viewer, data) => {
+ // Anyone can se another user
+ return true;
+};
- const data = await dataloaders.ExampleLoader.load(id.toString());
+export const clearCache = ({ dataloaders }: GraphQLContext, id: string) => {
+ return dataloaders.ExampleLoader.clear(id.toString());
+};
- return Example.viewerCanSee(viewer, data) ? new Example(data) : null;
+export const load = async (context: GraphQLContext, id: string): Promise<?User> => {
+ if (!id) {
+ return null;
}
- static clearCache({ dataloaders }, id) {
- return dataloaders.ExampleLoader.clear(id.toString());
+ let data;
+ try {
+ data = await context.dataloaders.ExampleLoader.load(id);
+ } catch (err) {
+ return null;
}
+ return viewerCanSee(context, data) ? new Example(data, context) : null;
+};
- static async loadExamples(context, args) {
- // TODO: specify conditions
- const examples = ExampleModel.find({});
+export const loadExamples = async (context: GraphQLContext) => {
+ const examples = ExampleModel.find({}).sort({ createdAt: -1 });
- return ConnectionFromMongoCursor.connectionFromMongoCursor(
- context, examples, args, Example.load,
- );
- }
-}
+ return connectionFromMongoCursor({
+ cursor: examples,
+ context,
+ loader: load,
+ });
+};
+// add here custom resolver
type
// @flow
-import {
- GraphQLObjectType,
- GraphQLString,
- GraphQLInt,
- GraphQLBoolean,
-} from 'graphql';
+import { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLBoolean } from 'graphql';
import { globalIdField } from 'graphql-relay';
-import { NodeInterface } from '../../interface/NodeInterface';
-import UserType from './UserType';
-import UserLoader from '../loader/UserLoader';
+import { NodeInterface } from '../interface/NodeInterface';
export default new GraphQLObjectType({
name: 'Tattoo',
export default new GraphQLObjectType({
resolve: obj => obj.styles,
},
user: {
- type: UserType,
+ type: GraphQLID,
description: 'User that created this example',
- resolve: async (obj, args, context) => await UserLoader.load(context, obj.author),
+ resolve: obj => obj.author,
},
body: {
type: GraphQLString,
viewerType
import { globalIdField, connectionArgs, fromGlobalId } from 'graphql-relay';
import { NodeInterface } from '../interface/NodeInterface';
+import { UserLoader, ContactLoader, ExampleLoader } from '../loader';
+
import UserType from './UserType';
-import { UserLoader } from '../loader';
import UserConnection from '../connection/UserConnection';
import ContactType from './ContactType';
-import { ContactLoader } from '../loader';
import ContactConnection from '../connection/ContactConnection';
+import ExampleType from './ExampleType';
+import ExampleConnection from '../connection/ExampleConnection';
+
export default new GraphQLObjectType({
name: 'Viewer',
description: '...',
This refers to the generated models