Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #28 from marmelab/understandable_errors
Browse files Browse the repository at this point in the history
[RFR] Better error messages
  • Loading branch information
fzaninotto authored May 4, 2017
2 parents 3dcecd0 + 32bd290 commit c156991
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/buildApolloParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ import {
* @returns {Object} apolloParams An object passed to either apolloClient.query or apolloClient.mutate
*/
export default (queries, type, resource, params) => {
const resourceQueries = queries[resource];

if (!resourceQueries) {
const knownResources = Object.keys(queries).map(key => `\r\n - ${key}`).join('');
throw new Error(`Unable to find queries and/or mutations for the "${resource}" resource. Known resources are: ${knownResources}`);
}

switch (type) {
case GET_LIST: {
return {
query: queries[resource][GET_LIST],
query: resourceQueries[GET_LIST],
variables: {
filter: JSON.stringify(params.filter),
page: params.pagination.page - 1,
Expand All @@ -31,7 +38,7 @@ export default (queries, type, resource, params) => {

case GET_ONE:
return {
query: queries[resource][GET_ONE],
query: resourceQueries[GET_ONE],
variables: {
id: params.id,
},
Expand All @@ -42,15 +49,15 @@ export default (queries, type, resource, params) => {
filter: JSON.stringify({ ids: params.ids }),
};

if (!queries[resource][GET_MANY]) {
if (!resourceQueries[GET_MANY]) {
variables = {
...variables,
perPage: 1000,
};
}

return {
query: queries[resource][GET_MANY] || queries[resource][GET_LIST],
query: resourceQueries[GET_MANY] || resourceQueries[GET_LIST],
variables,
};
}
Expand All @@ -60,34 +67,34 @@ export default (queries, type, resource, params) => {
filter: JSON.stringify({ [params.target]: params.id }),
};

if (!queries[resource][GET_MANY_REFERENCE]) {
if (!resourceQueries[GET_MANY_REFERENCE]) {
variables = {
...variables,
perPage: 1000,
};
}

return {
query: queries[resource][GET_MANY_REFERENCE] || queries[resource][GET_LIST],
query: resourceQueries[GET_MANY_REFERENCE] || resourceQueries[GET_LIST],
variables,
};
}

case UPDATE:
return {
mutation: queries[resource][UPDATE],
mutation: resourceQueries[UPDATE],
variables: { data: JSON.stringify(params.data) },
};

case CREATE:
return {
mutation: queries[resource][CREATE],
mutation: resourceQueries[CREATE],
variables: { data: JSON.stringify(params.data) },
};

case DELETE:
return {
mutation: queries[resource][DELETE],
mutation: resourceQueries[DELETE],
variables: {
id: params.id,
},
Expand Down

0 comments on commit c156991

Please sign in to comment.