Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
password: String,
dob: String,
createdAt: Date,
updatedAt: Date,
});
import { composeType, composeField, composeResolve, composeInterface, composeStorage } from 'graphql-compose';
import { description, only, remove, restrict, add } from 'graphql-compose/type';
import { composeTypeFromMongoose } from 'graphql-compose-mongoose';
import { GraphQLString } from 'graphql';
//---------- TYPE MODIFICATORS
composeType('User',
composeTypeFromMongoose(UserMongooseModel),
addResolverParam('model', UserMongooseModel), // internally added by `composeTypeFromMongoose`
composeInterface('Timestable'), // internally call composeStorage.Interfaces.get('Timestable')
description('User model description'),
only(['myName', 'surname']), // get only described fields
remove(['stats', 'password']), // ... or leave others, and remove just listed here
rename({
myName: 'name',
surname: 'lastname',
}),
restrict({
hasAccess: (source, args, context, info) => {
return context.isAdmin;
},
fields: ['name', 'dob'],
}),
changeValue({
name: ({ source, args, context, info }) => `${source.name} modified`,
}),
// example of custom type-middleware
next => typeConfig => {
const gqType = next(typeConfig);
return gqType;
},
);
//---------- INHERITANCE
composeType('SuperUser',
cloneType('User'),
add('isSuperUserType',
{
type: GraphQLBoolean,
resolve: () => true,
}
)
);
//---------- FIELD RESOLVERS
composeType('User',
add(
'friends',
composeType('SuperUser',
cloneType('User'),
add('isSuperUserType',
{
type: GraphQLBoolean,
resolve: () => true,
}
)
);
//---------- FIELD RESOLVERS
composeType('User',
add(
'friends',
composeField(
// custom field middleware
next => fieldConfig => {
const gqField = next(fieldConfig);
return gqField;
},
description('List of friends'),
addArg('gender', {}),
composeResolve(
argEval(({ source }) => ({ frendId: source._id })),
resolveList('User'),
// example of custom resolve-middleware
next => resolveParams => {
//---------- MUTATIONS (in progress, not ready)
composeType('UserInput',
cloneType('User'),
makeInputType(),
remove(['id'])
);
composeType('RootMutation',
add('createUser', composeType('User').queryById)
);
//----------- ROOT CONSTRUCTOR (in progress, not ready)
composeType('RootQuery',
add('user', composeType('User').queryById),
add('userList', composeType('User').queryList),
add('userConnection', composeType('User').queryConnection),
add('superUser', composeType('SuperUser').queryById)
);
//----------- INTERFACES
composeInterface('Timestable',
description('Timestable interface for types, which have fields with creation and modification time'),
add('createdAt', {
type: GraphQLDate,
}),
add('updatedAt', {
'User',
'byId',
addIdArg,
addResolverParam('DL', new DataLoader(keys => myBatchGetUsers(keys))),
addResolve(
(pluginParams, loaderParams, { source, args, context, info }) => {
return loaderParams.DL.load(args.id);
}
),
);
//---------- MUTATIONS (in progress, not ready)
composeType('UserInput',
cloneType('User'),
makeInputType(),
remove(['id'])
);
composeType('RootMutation',
add('createUser', composeType('User').queryById)
);
//----------- ROOT CONSTRUCTOR (in progress, not ready)
composeType('RootQuery',
add('user', composeType('User').queryById),
add('userList', composeType('User').queryList),
add('userConnection', composeType('User').queryConnection),
return loaderParams.DL.load(args.id);
}
),
);
//---------- MUTATIONS (in progress, not ready)
composeType('UserInput',
cloneType('User'),
makeInputType(),
remove(['id'])
);
composeType('RootMutation',
add('createUser', composeType('User').queryById)
);
//----------- ROOT CONSTRUCTOR (in progress, not ready)
composeType('RootQuery',
add('user', composeType('User').queryById),
add('userList', composeType('User').queryList),
add('userConnection', composeType('User').queryConnection),
add('superUser', composeType('SuperUser').queryById)
);
//----------- INTERFACES