Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
? 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;
}
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)) {
const model = models[modelType.name];
const paranoidType = model.graphql.paranoid && model.options.paranoid ? { paranoid: { type: GraphQLBoolean } } : {};
const aliases = model.graphql.alias;
const modelQueryName = generateName(aliases.fetch || options.naming.queries, { type: naming.type.get, name: modelTypeName }, { pascalCase });
queries[generateName(model.graphql.alias.default || options.naming.queries, { type: naming.type.default, name: modelTypeName }, { pascalCase })] = {
type: GraphQLString,
description: 'An empty default Query. Can be overwritten for your needs (for example metadata).',
resolve: () => '1'
};
if (models[modelType.name].graphql.excludeQueries.indexOf('count') === -1) {
queries[generateName(model.graphql.alias.count || options.naming.queries, { type: naming.type.count, name: modelTypeName }, { pascalCase })] = {
type: GraphQLInt,
args: {
where: defaultListArgs().where
},
resolve: (source, { where }, context, info) => {
const args = argsToFindOptions.default({ where });
if (args.where) whereQueryVarsToValues(args.where, info.variableValues);
return models[modelTypeName].count({
where: args.where
});
},
description: 'A count of the total number of objects in this connection, ignoring pagination.'
};
}
if (!model.graphql.excludeQueries.includes('fetch') && isAvailable(exposeOnly.queries, [modelQueryName])) {
queries[generateName(aliases.fetch || options.naming.queries, { type: naming.type.get, name: modelTypeName }, { pascalCase })] = {
fieldAssociations.hasMany.forEach(associatedModel => {
fields[formatFieldName(associatedModel.name)] = {
type: new GraphQLList(types[associatedModel.name]),
args: defaultListArgs(model[associatedModel.name]),
resolve: resolver(model[associatedModel.name]),
};
});