Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.makeExecutableSchema = ({ typeDefs, resolvers = {} }) => {
const parsed = graphql.parse(typeDefs);
const astSchema = graphql.buildASTSchema(parsed, { commentDescriptions: true });
for (const resolverName of Object.keys(resolvers)) {
const type = astSchema.getType(resolverName);
Assert(type || resolverName === '__schema', `Missing schema definition for resolver: ${resolverName}`);
const typeResolver = resolvers[resolverName];
// go through field resolvers for the parent resolver type
for (const fieldName of Object.keys(typeResolver)) {
const fieldResolver = typeResolver[fieldName];
Assert(typeof fieldResolver === 'function', `${resolverName}.${fieldName} resolver must be a function`);
if (type instanceof graphql.GraphQLScalarType) {
type[fieldName] = fieldResolver;
continue;
const type = astSchema.getType(resolverName);
Assert(type || resolverName === '__schema', `Missing schema definition for resolver: ${resolverName}`);
const typeResolver = resolvers[resolverName];
// go through field resolvers for the parent resolver type
for (const fieldName of Object.keys(typeResolver)) {
const fieldResolver = typeResolver[fieldName];
Assert(typeof fieldResolver === 'function', `${resolverName}.${fieldName} resolver must be a function`);
if (type instanceof graphql.GraphQLScalarType) {
type[fieldName] = fieldResolver;
continue;
}
if (type instanceof graphql.GraphQLEnumType) {
const fieldType = type.getValue(fieldName);
Assert(fieldType, `${resolverName}.${fieldName} enum definition missing from schema`);
fieldType.value = fieldResolver;
continue;
}
// no need to set resolvers unless we are dealing with a type that needs resolvers
if (!(type instanceof graphql.GraphQLObjectType) && !(type instanceof graphql.GraphQLInterfaceType)) {
continue;
}
const fields = type.getFields();
if (!fields) {
continue;
}
Assert(typeof fieldResolver === 'function', `${resolverName}.${fieldName} resolver must be a function`);
if (type instanceof graphql.GraphQLScalarType) {
type[fieldName] = fieldResolver;
continue;
}
if (type instanceof graphql.GraphQLEnumType) {
const fieldType = type.getValue(fieldName);
Assert(fieldType, `${resolverName}.${fieldName} enum definition missing from schema`);
fieldType.value = fieldResolver;
continue;
}
// no need to set resolvers unless we are dealing with a type that needs resolvers
if (!(type instanceof graphql.GraphQLObjectType) && !(type instanceof graphql.GraphQLInterfaceType)) {
continue;
}
const fields = type.getFields();
if (!fields) {
continue;
}
fields[fieldName].resolve = fieldResolver;
}
}
return astSchema;
};
exports.makeExecutableSchema = ({ typeDefs, resolvers = {} }) => {
const parsed = graphql.parse(typeDefs);
const astSchema = graphql.buildASTSchema(parsed, { commentDescriptions: true });
for (const resolverName of Object.keys(resolvers)) {
const type = astSchema.getType(resolverName);
Assert(type || resolverName === '__schema', `Missing schema definition for resolver: ${resolverName}`);
const typeResolver = resolvers[resolverName];
// go through field resolvers for the parent resolver type
for (const fieldName of Object.keys(typeResolver)) {
const fieldResolver = typeResolver[fieldName];
Assert(typeof fieldResolver === 'function', `${resolverName}.${fieldName} resolver must be a function`);
if (type instanceof graphql.GraphQLScalarType) {
type[fieldName] = fieldResolver;
continue;
}
if (type instanceof graphql.GraphQLEnumType) {
const fieldType = type.getValue(fieldName);
Assert(fieldType, `${resolverName}.${fieldName} enum definition missing from schema`);
fieldType.value = fieldResolver;
continue;
}
// no need to set resolvers unless we are dealing with a type that needs resolvers
if (!(type instanceof graphql.GraphQLObjectType) && !(type instanceof graphql.GraphQLInterfaceType)) {
continue;
}
exports.makeExecutableSchema = ({ typeDefs, resolvers = {} }) => {
const parsed = graphql.parse(typeDefs);
const astSchema = graphql.buildASTSchema(parsed, { commentDescriptions: true });
for (const resolverName of Object.keys(resolvers)) {
const type = astSchema.getType(resolverName);
Assert(type || resolverName === '__schema', `Missing schema definition for resolver: ${resolverName}`);
const typeResolver = resolvers[resolverName];
// go through field resolvers for the parent resolver type
for (const fieldName of Object.keys(typeResolver)) {
const fieldResolver = typeResolver[fieldName];
Assert(typeof fieldResolver === 'function', `${resolverName}.${fieldName} resolver must be a function`);
if (type instanceof graphql.GraphQLScalarType) {
type[fieldName] = fieldResolver;
continue;
}
it('has a resolver for every query and mutation in the schema', async () => {
const fields = [];
const parsed = graphql.parse(schema.toString());
for (const def of parsed.definitions) {
if (def.kind !== 'ObjectTypeDefinition' || (def.name.value !== 'Query' && def.name.value !== 'Mutation')) {
continue;
}
for (const field of def.fields) {
fields.push(field.name.value);
}
}
const server = await ServerHelper.getServer();
const paths = server.table().map((route) => {
return route.path.substr(1);
});
for (const field of fields) {