This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { mongooseLoader } from '@entria/graphql-mongoose-loader'; | |
import DataLoader from 'dataloader'; | |
import { ConnectionArguments } from 'graphql-relay'; | |
import { Model, Types } from 'mongoose'; | |
import { buildMongoConditionsFromFilters } from '@entria/graphql-mongo-helpers'; | |
import { withConnectionCursor } from './withConnectionCursor'; | |
const defaulViewerCanSee = (context, data) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const UserType = new GraphQLObjectType<IUser, GraphQLContext>({ | |
name: 'User', | |
description: 'User data', | |
fields: () => ({ | |
id: globalIdField('User'), | |
...mongooseIDResolver, | |
name: { | |
type: GraphQLString, | |
resolve: user => user.name, | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to return an object given its globalId | |
// Is used to take the ID argument of the node field and use it to resolve an object | |
export const idFetcher = async (globalId, context: GraphQLContext) => { | |
const { type, id } = fromGlobalId(globalId); | |
const data = await typesLoaders[type].load(context, id); | |
return data; | |
}; | |
// How to return a type given an object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type GetLoader = () => DataLoader<string, any>; | |
type Dataloaders = { | |
[loader: string]: GetLoader; | |
}; | |
const dataloaders: Dataloaders = {}; | |
export const registerDataLoader = (name: string, getLoader: GetLoader) => { | |
dataloaders[name] = getLoader; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type DataLoaderKey = string | Types.ObjectId | Object; | |
export type Load = (context: GraphQLContext, id: DataLoaderKey) => any; | |
type TypeLoaders = { | |
[key: string]: { | |
type: GraphQLObjectType, | |
load: Load, | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { toGlobalId } from 'graphql-relay'; | |
import { Types } from 'mongoose'; | |
import { getObjectId } from './getObjectId'; | |
import { | |
clearDbAndRestartCounters, | |
connectMongoose, | |
createUser, | |
disconnectMongoose | |
} from '../../../test/helpers'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mutation = createEditSingleFieldMutation({ | |
name: 'UserEditName', | |
model: User, | |
fieldName: 'title', | |
fieldType: GraphQLString, | |
clearCache: UserLoader.clearCache, | |
notFoundMessage: ({ t }) => t('User not found'), | |
successMessage: ({ t }) => t('User edited successfully'), | |
outputFields: { | |
user: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { graphql, subscribe, parse } from 'graphql'; | |
import { | |
connectMongoose, | |
disconnectMongoose, | |
clearDbAndRestartCounters, | |
getContext, | |
} from '../../../../../test/helpers'; | |
import { schema } from '../../../../schema/schema'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { subscriptionWithClientId } from 'graphql-relay-subscription'; | |
import UserType from '../UserType'; | |
import * as UserLoader from '../UserLoader'; | |
import pubSub, { EVENTS } from '../../../channels/pubSub'; | |
const UserAddedSubscription = subscriptionWithClientId({ | |
name: 'UserAdded', | |
inputFields: {}, | |
subscribe: () => pubSub.asyncIterator(EVENTS.USER.ADDED), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Class | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.count !== this.props.count) { | |
console.log('count changed', nextProps.count); | |
} | |
} | |
// Hooks | |
// Skipping first iteration exactly like componentWillReceiveProps: |