Enable cy.mockGraphqlOps to handle async methods
This commit is contained in:
parent
e4046de318
commit
66b9f8ac6a
|
|
@ -60,43 +60,48 @@ const mockGraphql = (options?: any) => {
|
|||
// rebuilding what was in cypress-graphql-mock package here, because now we can just intercept the graphql call instead of messing with fetch
|
||||
cy.intercept('POST', '/api/graphql', (req) => {
|
||||
const { operationName, query, variables } = req.body;
|
||||
const rootValue = getRootValue(currentOperations, operationName, variables);
|
||||
return getRootValue(currentOperations, operationName, variables)
|
||||
.then((rootValue) => {
|
||||
if (!rootValue) {
|
||||
return req;
|
||||
}
|
||||
|
||||
if (!rootValue) {
|
||||
return req;
|
||||
}
|
||||
if (
|
||||
// Additional checks here because of transpilation.
|
||||
// We will lose instanceof if we are not using specific babel plugin, or using pure TS to compile front-end
|
||||
rootValue instanceof GraphQLError ||
|
||||
rootValue.constructor === GraphQLError ||
|
||||
rootValue.constructor.name === 'GraphQLError'
|
||||
) {
|
||||
return req.reply({
|
||||
body: {
|
||||
data: {},
|
||||
errors: [rootValue],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
// Additional checks here because of transpilation.
|
||||
// We will lose instanceof if we are not using specific babel plugin, or using pure TS to compile front-end
|
||||
rootValue instanceof GraphQLError ||
|
||||
rootValue.constructor === GraphQLError ||
|
||||
rootValue.constructor.name === 'GraphQLError'
|
||||
) {
|
||||
return req.reply({
|
||||
body: {
|
||||
data: {},
|
||||
errors: [rootValue],
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
graphql({
|
||||
schema: schemaWithMocks,
|
||||
source: query,
|
||||
variableValues: variables,
|
||||
operationName,
|
||||
rootValue,
|
||||
}).then(
|
||||
(result) => {
|
||||
req.reply({
|
||||
...result,
|
||||
});
|
||||
},
|
||||
(e) => {
|
||||
graphql({
|
||||
schema: schemaWithMocks,
|
||||
source: query,
|
||||
variableValues: variables,
|
||||
operationName,
|
||||
rootValue,
|
||||
}).then(
|
||||
(result) => {
|
||||
req.reply({
|
||||
...result,
|
||||
});
|
||||
},
|
||||
(e) => {
|
||||
console.error(e);
|
||||
}
|
||||
);
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
}
|
||||
);
|
||||
req.reply({ statusCode: 500, body: { errors: ['Internal server error'] } });
|
||||
});
|
||||
}).as('graphqlRequest');
|
||||
|
||||
cy.wrap({
|
||||
|
|
@ -235,10 +240,10 @@ Cypress.Commands.add('mockGraphql', mockGraphql);
|
|||
|
||||
Cypress.Commands.add('mockGraphqlOps', mockGraphqlOps);
|
||||
|
||||
const getRootValue = (allOperations: any, operationName: string, variables: any) => {
|
||||
const getRootValue = async (allOperations: any, operationName: string, variables: any) => {
|
||||
const operation = allOperations[operationName];
|
||||
if (typeof operation === 'function') {
|
||||
return operation(variables);
|
||||
return Promise.resolve().then(() => operation(variables));
|
||||
}
|
||||
return operation;
|
||||
return Promise.resolve(operation);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue