Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
};
let mocks = {
// This is where you tell graphql-tools how to generate a mock for a custom
// scalar type:
//
// Time() {
// return moment().utc().format('YYYY-MM-DDTHH:mm:ss.SSSZ');
// }
Date() {
return '2019-09-28';
},
};
let schema = makeExecutableSchema({ typeDefs: schemaString, resolvers });
addResolveFunctionsToSchema({
schema,
resolvers: typeResolvers,
inheritResolversFromInterfaces: true,
});
addMockFunctionsToSchema({ schema, mocks, preserveResolvers: true });
let pretender = new Pretender();
pretender.post('https://test.example/graphql', function(request) {
let body = JSON.parse(request.requestBody);
return graphql(schema, body.query, {}, {}, body.variables).then(result => {
return [200, { 'Content-Type': 'application/json' }, result];
});
});
});
let value;
onAfter.forEach((hook) => {
promise = promise.then((newValue) => {
value = newValue || value;
return hook({ keys, source, value, variables, context, resolverAST, ast });
});
});
return promise.then(newValue => newValue || value);
};
} else {
store[key] = obj[key];
} return store;
}, {});
};
addResolveFunctionsToSchema(builtSchema, recurse(resolvers));
// console.log(resolvers);
return builtSchema;
};
reduxMiddlewares = [],
apolloClientOptions = {},
typeResolvers,
context = {},
rootValue = {},
}) {
const schema = makeExecutableSchema({ typeDefs });
if (!!mocks) {
addMockFunctionsToSchema({
schema,
mocks,
});
}
if (!!typeResolvers) {
addResolveFunctionsToSchema(schema, typeResolvers);
}
const graphqlClient = new ApolloClient({
addTypename: true,
networkInterface: {
query(request) {
return graphql(
schema,
print(request.query),
rootValue,
context,
request.variables,
request.operationName,
);
},
},
directives,
resolvers,
hooks
);
transformedAST.definitions
.filter(x => x.kind === 'ObjectTypeDefinition')
.forEach((definition) => {
if (['Query', 'Mutation'].includes(definition.name.value)) { return; }
const input = transformASTTypeToInput(definition, {
newName: `${capitalize(definition.name.value)}Input`,
ast: transformedAST,
});
addDefinition(transformedAST, input);
});
const builtSchema = buildASTSchema(transformedAST);
addResolveFunctionsToSchema(builtSchema, resolvers);
return { schema: builtSchema, ast: transformedAST };
};
options: IApplyOptions,
middleware: IMiddleware,
): {
schema: GraphQLSchema
fragmentReplacements: FragmentReplacement[]
} {
const validMiddleware = validateMiddleware(schema, middleware)
const resolvers = generateResolverFromSchemaAndMiddleware(
schema,
options,
validMiddleware,
)
const fragmentReplacements = extractFragmentReplacements(resolvers)
addResolveFunctionsToSchema({
schema,
resolvers,
resolverValidationOptions: {
requireResolversForResolveType: false,
},
})
return { schema, fragmentReplacements }
}
}
typeRegistry.addType('Query', query);
typeRegistry.addType('Mutation', mutation);
let mergedSchema = new GraphQLSchema({
query,
mutation,
types: typeRegistry.getAllTypes(),
});
extensions.forEach(extension => {
mergedSchema = extendSchema(mergedSchema, extension);
});
addResolveFunctionsToSchema(mergedSchema, fullResolvers);
return mergedSchema;
}
export function applyExtension(extension, schema, options = {}) {
let outputSchema = schema
if (extension.extendSchema) {
if (typeof extension.extendSchema === 'object') {
debug(
`Extending schema via an object from the “${extension.name}” extension.`
)
const { schemas = [], resolvers } = extension.extendSchema
outputSchema = schemas.reduce((updatedSchema, extensionSchema) => {
if (typeof extensionSchema === 'string') {
extensionSchema = parse(extensionSchema)
}
return extendSchema(updatedSchema, extensionSchema)
}, outputSchema)
if (resolvers) {
addResolveFunctionsToSchema({ schema: outputSchema, resolvers })
}
} else if (typeof extension.extendSchema === 'function') {
debug(
`Extending schema via a function from the “${
extension.name
}” extension.`
)
outputSchema = extension.extendSchema(schema, options)
} else {
throw new Error(
`The “${extension.name}” extension contains an invalid ` +
`\`extendSchema\` value: ${extension.extendSchema}`
)
}
}
}) {
const schema = makeExecutableSchema({ typeDefs, resolverValidationOptions });
let mockOptions = {};
if (!!mocks) {
mockOptions = {
schema,
mocks,
};
addMockFunctionsToSchema(mockOptions);
}
if (!!typeResolvers) {
addResolveFunctionsToSchema({ schema, resolvers: typeResolvers });
}
const cache = new InMemoryCache(cacheOptions);
return new ApolloClient({
addTypename: true,
cache,
link: ApolloLink.from([
...links(cache),
createLink(schema, rootValue, context, apolloLinkOptions),
]),
connectToDevTools: true,
...apolloClientOptions,
});
}
export const addResolvers = (typeName: string, fieldResolvers: Map> ): GraphQLSchema => {
const resolverMap = {};
resolverMap[typeName] = {};
for (const [name, resolve] of fieldResolvers) {
resolverMap[typeName][name] = resolve;
}
addResolveFunctionsToSchema(schema, resolverMap);
return schema;
};