Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
localAmount: {
type: new GraphQLNonNull(GraphQLString),
description: 'Amount of coin held in local wallet'
},
exchangeAmount: {
type: new GraphQLNonNull(GraphQLString),
description: 'Amount of coin held in exchange wallet'
},
purchaseAmount: {
type: new GraphQLNonNull(GraphQLString),
description: 'Amount of coin waiting to be bought on exchange'
},
localWallet: {
type: Wallet,
description: 'Local wallet associated with coin',
resolve: resolver(Coin.LocalWallet)
},
exchangeWallet: {
type: Wallet,
description: 'Wallet on exchange associated with coin',
resolve: resolver(Coin.ExchangeWallet)
},
feeTolerance: {
type: new GraphQLNonNull(GraphQLString),
description: 'Maximum percentage of a transaction allowed to be lost in fees'
},
// transactions: {
// type: new GraphQLList(Transaction),
// description: 'Transactions associated with this coin'
// }
}
}
return function resolver() {
let _wrapper;
try {
const model = callback(modelName, arguments[1], arguments[2]);
const key = `${model.sequelize.options.identifier}_${model.name}`;
// avoid wrapping too much!
if (!_cache[key]) {
_wrapper = _cache[key] = graphqlSequelize.resolver(model);
} else {
_wrapper = _cache[key];
}
} catch (e) {
throw new Error(`Unable to resolve(${modelName}). ${e.message}`);
}
return _wrapper.apply(null, arguments);
};
};
? new GraphQLList(existingTypes[relation.target.name])
: existingTypes[relation.target.name];
// Remove belongs to associations for input types to avoide foreign key constraint errors.
if (!(isInput && relation.associationType === 'BelongsTo') && !(isInput && !nestedMutations)) {
fields[associationName] = { type };
}
// Add through table, this doesn't need resolver since this is already included when quering n:m relations.
if (relation.associationType === 'BelongsToMany' ) {
fields[relation.through.model.name] = { type: existingTypes[relation.through.model.name] };
}
if (!isInput && !relation.isRemote) {
const throughArguments = relation.associationType === 'BelongsToMany' ? { throughWhere: defaultListArgs().where } : {};
// GraphQLInputObjectType do not accept fields with resolve
fields[associationName].args = Object.assign(defaultArgs(relation), defaultListArgs(), throughArguments);
fields[associationName].resolve = (source, args, context, info) => {
return queryResolver(options)(relation, relation.target.name, source, args, context, info, true);
};
}
}
}
return fields;
}
// Remove belongs to associations for input types to avoide foreign key constraint errors.
if (!(isInput && relation.associationType === 'BelongsTo') && !(isInput && !nestedMutations)) {
fields[associationName] = { type };
}
// Add through table, this doesn't need resolver since this is already included when quering n:m relations.
if (relation.associationType === 'BelongsToMany' ) {
fields[relation.through.model.name] = { type: existingTypes[relation.through.model.name] };
}
if (!isInput && !relation.isRemote) {
const throughArguments = relation.associationType === 'BelongsToMany' ? { throughWhere: defaultListArgs().where } : {};
// GraphQLInputObjectType do not accept fields with resolve
fields[associationName].args = Object.assign(defaultArgs(relation), defaultListArgs(), throughArguments);
fields[associationName].resolve = (source, args, context, info) => {
return queryResolver(options)(relation, relation.target.name, source, args, context, info, true);
};
}
}
}
return fields;
}
const modelType = outputTypes[modelTypeName];
const queries = {
[modelType.name + 'Default']: {
type: GraphQLInt,
description: 'An empty default Query.',
resolve: () => 1
}
};
const paranoidType = models[modelType.name].options.paranoid ? { paranoid: { type: GraphQLBoolean } } : {};
const aliases = models[modelType.name].graphql.alias;
if (models[modelType.name].graphql.excludeQueries.indexOf('query') === -1) {
queries[camelCase(aliases.fetch || (modelType.name + 'Get'))] = {
type: new GraphQLList(modelType),
args: Object.assign(defaultArgs(models[modelType.name]), defaultListArgs(), includeArguments(), paranoidType),
resolve: (source, args, context, info) => {
return query(models[modelType.name], modelType.name, source, args, context, info);
}
}
}
if (models[modelTypeName].graphql && models[modelTypeName].graphql.queries) {
for (const query in models[modelTypeName].graphql.queries) {
if (models[modelTypeName].graphql.queries[query]) {
let isArray = false;
// eslint-disable-next-line no-unused-vars
let isRequired = false;
let outPutType = GraphQLInt;
let inPutType = GraphQLInt;
if (!isInput && !relation.isRemote) {
// GraphQLInputObjectType do not accept fields with resolve
fields[associationName].args = Object.assign(defaultArgs(relation), defaultListArgs(), includeArguments())
fields[associationName].resolve = async (source, args, context, info) => {
await execBefore(relation.target, source, args, context, info, 'fetch')
const data = await resolver(relation, { [EXPECTED_OPTIONS_KEY]: dataloaderContext })(source, args, context, info)
if (relation.target.graphql.extend.fetch && data.length) {
const item = await relation.target.graphql.extend.fetch(data, source, args, context, info)
return [].concat(item)
}
return data
}
} else if (!isInput && relation.isRemote) {
fields[associationName].args = Object.assign({}, relation.query.args, defaultListArgs())
fields[associationName].resolve = (source, args, context, info) => {
return remoteResolver(source, args, context, info, relation.query, fields[associationName].args, types[relation.target.name])
}
}
}
return fields
}
return (models, outputTypes = {}, inputTypes = {}) => {
const includeArguments = generateIncludeArguments(options.includeArguments, outputTypes);
const defaultListArguments = defaultListArgs();
const createQueriesFor = {};
const allCustomQueries = Object.assign({}, options.queries);
for (const modelName in models) {
const model = models[modelName];
const outputTypeName = modelName;
const customQueryNames = Object.keys(model.graphql.queries || {});
const modelQueryName = generateName(model.graphql.alias.fetch || options.naming.queries, { type: naming.type.get, name: outputTypeName }, { pascalCase });
const toBeGenerated = [].concat(customQueryNames).concat(
model.graphql.excludeQueries.includes('fetch') ? [] : modelQueryName
);
// model must have atleast one query to implement.
if (model && (!model.graphql.excludeQueries.includes('fetch') || customQueryNames.length)) {
if (isAvailable(exposeOnly.queries, toBeGenerated)) {
if (!types[relation.target.name]) {
return fields
}
// BelongsToMany is represented as a list, just like HasMany
const type = relation.associationType === 'BelongsToMany' ||
relation.associationType === 'HasMany'
? new GraphQLList(types[relation.target.name])
: types[relation.target.name]
fields[associationName] = { type }
if (!isInput && !relation.isRemote) {
// GraphQLInputObjectType do not accept fields with resolve
fields[associationName].args = Object.assign(defaultArgs(relation), defaultListArgs(), includeArguments())
fields[associationName].resolve = async (source, args, context, info) => {
await execBefore(relation.target, source, args, context, info, 'fetch')
const data = await resolver(relation, { [EXPECTED_OPTIONS_KEY]: dataloaderContext })(source, args, context, info)
if (relation.target.graphql.extend.fetch && data.length) {
const item = await relation.target.graphql.extend.fetch(data, source, args, context, info)
return [].concat(item)
}
return data
}
} else if (!isInput && relation.isRemote) {
fields[associationName].args = Object.assign({}, relation.query.args, defaultListArgs())
fields[associationName].resolve = (source, args, context, info) => {
return remoteResolver(source, args, context, info, relation.query, fields[associationName].args, types[relation.target.name])
}
const before = (findOptions, args) => {
if (isAssociation && model.through) {
findOptions.through = {
attributes: Object.keys(model.through.model.rawAttributes)
};
}
if (args.throughWhere) {
findOptions.where = argsToFindOptions.default({ where: args.throughWhere }, Object.keys(model.through.model.rawAttributes));
}
const order = getOrderBy(args.order);
findOptions.order = order.length ? order : undefined;
// if paranoid option from sequelize is set, this switch can be used to fetch archived, non-archived or all items.
findOptions.paranoid = ((args.where && args.where.deletedAt && args.where.deletedAt.ne === null) || args.paranoid === false) ? false : model.options.paranoid;
return findOptions;
};
return async (model, inputTypeName, source, args, context, info, isAssociation = false) => {
const realModel = isAssociation ? model.target : model;
const graphql = realModel.graphql;
// setup dataloader for resolver.
resolver.contextToOptions = { [EXPECTED_OPTIONS_KEY]: EXPECTED_OPTIONS_KEY };
context[EXPECTED_OPTIONS_KEY] = dataloaderContext;
if (!isAssociation) {
args.limit = args.limit || limits.default;
args.limit = args.limit > limits.max ? limits.max : args.limit;
}
// No need to call authorizer again on associations
if (!isAssociation) await options.authorizer(source, args, context, info);
// query being overwritten at graphql.overwrite.fetch, run it and skip the rest
if (_.has(graphql.overwrite, QUERY_TYPE)) {
return graphql.overwrite[QUERY_TYPE](source, args, context, info);
}
// hook coming from graphql.before.fetch