[mocks][m]: setup CKAN api mocks with the new apollo setup.

This commit is contained in:
anuveyatsu
2020-06-23 10:43:53 +06:00
parent 50119bf33f
commit 55d9c68b34
4 changed files with 39 additions and 20 deletions

View File

@@ -7,7 +7,7 @@ import { RestLink } from 'apollo-link-rest';
let apolloClient; let apolloClient;
const restLink = new RestLink({ const restLink = new RestLink({
uri: 'https://demo.ckan.org/api/3/action/', uri: getConfig().publicRuntimeConfig.DMS + '/api/3/action/',
typePatcher: { typePatcher: {
Search: ( Search: (
data: any, data: any,

View File

@@ -66,9 +66,7 @@ module.exports.initMocks = function () {
nock('http://mock.ckan/api/3/action', { encodedQueryParams: true }) nock('http://mock.ckan/api/3/action', { encodedQueryParams: true })
.persist() .persist()
// 1. Call without query. // 1. Call without query.
.get( .get('/package_search?')
'/package_search?facet.field=organization&facet.field=groups&facet.field=tags&facet.field=res_format&facet.field=license_id&facet.limit=5'
)
.reply(200, { .reply(200, {
success: true, success: true,
result: { result: {
@@ -80,9 +78,7 @@ module.exports.initMocks = function () {
}, },
}) })
// 2. Call with `q=gdp` query. // 2. Call with `q=gdp` query.
.get( .get('/package_search?q=gdp')
'/package_search?q=gdp&facet.field=organization&facet.field=groups&facet.field=tags&facet.field=res_format&facet.field=license_id&facet.limit=5'
)
.reply(200, { .reply(200, {
success: true, success: true,
result: { result: {

View File

@@ -1,7 +1,37 @@
module.exports = { const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
module.exports = (phase, { defaultConfig }) => {
const dms = process.env.DMS;
if (phase === PHASE_DEVELOPMENT_SERVER) {
if (dms) {
console.log('\nYou are running the app in dev mode 🌀');
console.log(
'Did you know that you can use mocked CKAN API? Just unset your `DMS` env var.'
);
console.log('Happy coding ☀️\n');
} else {
const mocks = require('./mocks');
mocks.initMocks();
console.log(
'\nYou have not defined any DMS API so we are activating the mocks ⚠️'
);
console.log(
'If you wish to run against your CKAN API, you can set `DMS` env var.'
);
console.log(
'For example, to run against demo ckan site: `DMS=https://demo.ckan.org`\n'
);
}
return {
publicRuntimeConfig: { publicRuntimeConfig: {
graphqlEndpoint: DMS: dms ? dms.replace(/\/?$/, '') : 'http://mock.ckan',
process.env.GRAPHQL_ENDPOINT ||
'https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn',
}, },
}; };
}
return {
publicRuntimeConfig: {
DMS: dms ? dms.replace(/\/?$/, '') : 'https://demo.ckan.org',
},
};
};

View File

@@ -4,13 +4,6 @@ import '../styles/index.css';
import { ApolloProvider } from '@apollo/react-hooks'; import { ApolloProvider } from '@apollo/react-hooks';
import { useApollo } from '../lib/apolloClient'; import { useApollo } from '../lib/apolloClient';
// Setup mocks
// if (process.env.NODE_ENV === 'development') {
// const mocks = require('../mocks');
// mocks.initMocks();
// console.warn('You have activated the mocks.');
// }
export default function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }) {
const apolloClient = useApollo(pageProps.initialApolloState); const apolloClient = useApollo(pageProps.initialApolloState);