Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getType(field, model) {
if (field.type === 'enum') {
addUsedType('GraphQLEnumType');
return getEnum(field, model);
}
var type = typeMap[field.type];
var identifier = b.identifier(type);
addUsedType(type);
if (!field.isNullable) {
addUsedType('GraphQLNonNull');
return b.newExpression(b.identifier('GraphQLNonNull'), [identifier]);
}
return identifier;
}
);
var typeDeclaration = b.objectExpression([
b.property('init', b.identifier('name'), b.literal(name)),
generateDescription(model.description),
b.property(
'init',
b.identifier('fields'),
buildFieldWrapperFunction(name, b.objectExpression(fields), opts)
)
].concat(interfaces || []));
return {
ast: buildVar(
name + 'Type',
b.newExpression(
b.identifier('GraphQLObjectType'),
[typeDeclaration]
),
opts.es6
)
};
}
module.exports = function buildQuery(type, data, opts) {
var model = data.models[type.name];
var primaryKey = getPrimaryKey(model) || {};
var keyName = primaryKey.name;
var keyType = typeMap[primaryKey.type];
return b.objectExpression([
b.property('init', b.identifier('type'), b.identifier(type.varName)),
b.property('init', b.identifier('args'), b.objectExpression(keyName ? [
b.property('init', b.identifier('id'), b.objectExpression([
b.property('init', b.identifier('name'), b.literal(keyName)),
b.property('init', b.identifier('type'), b.newExpression(
b.identifier('GraphQLNonNull'),
[b.identifier(keyType)]
))
]))
] : []))
].concat(opts.outputDir ? [b.property(
'init',
b.identifier('resolve'),
buildResolver(type)
)] : []));
};
function wrapMissingModule(modulePath: string): Promise {
return Promise.resolve(b.program([
b.throwStatement(
b.newExpression(
b.identifier('Error'),
[
b.literal(`Module '${modulePath}' not found`)
]
)
)
]));
}
return b.property(
'init',
b.identifier(camelCase(type.name)),
buildQuery(type, data, opts)
);
});
}
return buildVar('schema',
b.newExpression(
b.identifier('GraphQLSchema'),
[b.objectExpression([
b.property(
'init',
b.identifier('query'),
b.newExpression(
b.identifier('GraphQLObjectType'),
[b.objectExpression([
b.property('init', b.identifier('name'), b.literal('RootQueryType')),
b.property('init', b.identifier('fields'), buildFieldWrapperFunction(
'RootQuery',
b.objectExpression(queryFields),
opts
))
])]
)
)
])]
)
);
};
'init',
b.identifier('node'),
b.identifier('nodeField')
));
} else {
queryFields = map(data.types, function(type) {
return b.property(
'init',
b.identifier(camelCase(type.name)),
buildQuery(type, data, opts)
);
});
}
return buildVar('schema',
b.newExpression(
b.identifier('GraphQLSchema'),
[b.objectExpression([
b.property(
'init',
b.identifier('query'),
b.newExpression(
b.identifier('GraphQLObjectType'),
[b.objectExpression([
b.property('init', b.identifier('name'), b.literal('RootQueryType')),
b.property('init', b.identifier('fields'), buildFieldWrapperFunction(
'RootQuery',
b.objectExpression(queryFields),
opts
))
])]
)
function generateListReferenceField(reference) {
addUsedType('GraphQLList');
if (opts.relay) {
return generateRelayReferenceField(reference);
}
return generateField({
name: reference.field,
description: reference.description || opts.defaultDescription + ' (reference)',
resolve: buildResolver(reference.model, find(reference.model.fields, { isPrimaryKey: true }).originalName),
args: generateLimitOffsetArgs()
}, b.newExpression(
b.identifier('GraphQLList'),
[b.callExpression(b.identifier('getType'), [b.literal(reference.model.name)])]
));
}
'init',
b.literal(enumKey),
b.objectExpression([
b.property('init', b.identifier('value'), b.literal(enumValue.value)),
generateDescription(enumValue.description)
])
));
}
var typeDeclaration = b.objectExpression([
b.property('init', b.identifier('name'), b.literal(model.name + capitalize(field.name))),
generateDescription(field.description),
b.property('init', b.identifier('values'), b.objectExpression(values))
]);
return b.newExpression(
b.identifier('GraphQLEnumType'),
[typeDeclaration]
);
}
}