Add first draft of a GraphQL mock intercept

This commit is contained in:
Ramon Wenger 2022-09-26 16:02:19 +02:00
parent bb4c22bee8
commit 965f7517f8
4 changed files with 105 additions and 25 deletions

View File

@ -4,21 +4,21 @@ describe('Instruments Page', () => {
});
it('opens the instruments page', () => {
cy.mockGraphqlOps({
operations: {
MeQuery: {},
InstrumentQuery: {
instrument: {
title: 'A Guitar',
intro: 'Money for Nothing',
contents: ''
// id: ID!
// bookmarks: [InstrumentBookmarkNode]
// type: InstrumentTypeNode
}
},
},
});
// cy.mockGraphqlOps({
// operations: {
// MeQuery: {},
// InstrumentQuery: {
// instrument: {
// title: 'A Guitar',
// intro: 'Money for Nothing',
// contents: ''
// // id: ID!
// // bookmarks: [InstrumentBookmarkNode]
// // type: InstrumentTypeNode
// }
// },
// },
// });
cy.visit('instrument/blabliblablub');
cy.getByDataCy('instrument-title').should('exist').should('contain', 'A Guitar');

View File

@ -27,8 +27,11 @@
// https://github.com/tgriesser/cypress-graphql-mock/issues/23
// todo: once above issue is fixed, go back to the original repo -> npm install cypress-graphql-mock
// import 'cypress-graphql-mock';
import '@iam4x/cypress-graphql-mock';
import mocks from '../fixtures/mocks';
// import mocks from '../fixtures/mocks';
import {makeExecutableSchema} from '@graphql-tools/schema';
import {addMocksToSchema} from '@graphql-tools/mock';
import {graphql} from 'graphql/graphql';
Cypress.Commands.add('apolloLogin', (username, password) => {
const payload = {
@ -135,17 +138,60 @@ Cypress.Commands.add('isSubmissionReadOnly', (myText) => {
});
Cypress.Commands.add('openSidebar', () => {
cy.getByDataCy('user-widget-avatar').click();
cy.getByDataCy('user-widget-avatar').click();
});
Cypress.Commands.add('setup', () => {
cy.fakeLogin('nino.teacher', 'test');
cy.server();
cy.viewport('macbook-15');
cy.task('getSchema').then(schema => {
cy.mockGraphql({
schema,
mocks,
});
cy.viewport('macbook-15');
// cy.task('getSchema').then(schemaString => {
const schemaString = `
schema {
query: Query
}
type Query {
me: PrivateUserNode
}
interface Node {
id: ID!
}
type PrivateUserNode implements Node {
firstName: String!
lastName: String!
avatarUrl: String!
}
`;
const schema = makeExecutableSchema({typeDefs: schemaString});
const schemaWithMocks = addMocksToSchema({schema});
// cy.intercept('POST', '/api/graphql', (req) => {
const query = `
query MeQuery {
me {
firstName
lastName
avatarUrl
}
}
`;
// const {query, variables} = req.body;
// console.log(query, variables);
graphql({
schema: schemaWithMocks,
source: query,
}).then(result => {
console.log(result);
// req.reply({
// data: {},
// });
}, e => {
console.log(e.message);
console.error(e);
});
// });
// });
});

29
client/mock.js Normal file
View File

@ -0,0 +1,29 @@
const {readFileSync} = require('fs');
const {resolve} = require('path');
const { addMocksToSchema} = require('@graphql-tools/mock');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { graphql } = require('graphql');
const schemaString = readFileSync(
resolve(__dirname,'../server/schema.graphql'),
'utf8'
);
// Make a GraphQL schema with no resolvers
const schema = makeExecutableSchema({ typeDefs: schemaString })
// Create a new schema with mocks
const schemaWithMocks = addMocksToSchema({ schema })
const query = /* GraphQL */ `
query MeQuery {
me {
firstName
}
}
`
graphql({
schema: schemaWithMocks,
source: query,
}).then(result => console.log('Got result', result))

5
next-steps.md Normal file
View File

@ -0,0 +1,5 @@
- install via yarn
- try resolving the packages with `"**/graphql"` and `"graphql"` separately
- install cypress 10 and upgrade
- try to build the project and run statically, then load cypress, might be a webpack issue
- try to make a separate package.json inside of the cypress folder