Skip to content

Commit

Permalink
Add a GET_COUNT fetch action for graphql dataProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin committed Oct 15, 2020
1 parent aad6285 commit 954a5f1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
17 changes: 15 additions & 2 deletions packages/ra-data-graphql-simple/src/buildGqlQuery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GET_LIST, GET_MANY, GET_MANY_REFERENCE, DELETE } from 'ra-core';
import { QUERY_TYPES } from 'ra-data-graphql';
import { QUERY_TYPES, GET_COUNT } from 'ra-data-graphql';
import { TypeKind } from 'graphql';
import * as gqlTypes from 'graphql-ast-types-browser';

Expand Down Expand Up @@ -158,12 +158,25 @@ export const buildApolloArgs = (query, variables) => {
return args;
};

function getCountType(resource) {
const countType = resource[GET_COUNT];

if (!countType) {
throw new Error(
`No query or mutation matching fetch type ${GET_COUNT} could be found for resource ${resource.type.name}`
);
}

return countType;
}

export default introspectionResults => (
resource,
aorFetchType,
queryType,
variables
) => {
const countType = getCountType(resource);
const { sortField, sortOrder, ...metaVariables } = variables;
const apolloArgs = buildApolloArgs(queryType, variables);
const args = buildArgs(queryType, variables);
Expand All @@ -186,7 +199,7 @@ export default introspectionResults => (
gqlTypes.selectionSet(fields)
),
gqlTypes.field(
gqlTypes.name(`_${queryType.name}Meta`),
gqlTypes.name(countType.name),
gqlTypes.name('total'),
metaArgs,
null,
Expand Down
2 changes: 2 additions & 0 deletions packages/ra-data-graphql-simple/src/buildGqlQuery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
CREATE,
DELETE,
} from 'ra-core';
import { GET_COUNT } from 'ra-data-graphql';
import buildGqlQuery, {
buildApolloArgs,
buildArgs,
Expand Down Expand Up @@ -293,6 +294,7 @@ describe('buildGqlQuery', () => {
};

const resource = {
[GET_COUNT]: { name: '_allCommandMeta' },
type: {
fields: [
{ type: { kind: TypeKind.SCALAR, name: '' }, name: 'foo' },
Expand Down
6 changes: 5 additions & 1 deletion packages/ra-data-graphql-simple/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import merge from 'lodash/merge';
import buildDataProvider from 'ra-data-graphql';
import buildDataProvider, {
GET_COUNT as INNER_GET_COUNT,
} from 'ra-data-graphql';
import { DELETE, DELETE_MANY, UPDATE, UPDATE_MANY } from 'ra-core';

import defaultBuildQuery from './buildQuery';
const defaultOptions = {
buildQuery: defaultBuildQuery,
};

export const GET_COUNT = INNER_GET_COUNT;

export const buildQuery = defaultBuildQuery;

export default options => {
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-data-graphql/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import {
DELETE_MANY,
} from 'ra-core';

export const QUERY_TYPES = [GET_LIST, GET_MANY, GET_MANY_REFERENCE, GET_ONE];
export const GET_COUNT = 'GET_COUNT';
export const QUERY_TYPES = [
GET_LIST,
GET_MANY,
GET_MANY_REFERENCE,
GET_ONE,
GET_COUNT,
];
export const MUTATION_TYPES = [
CREATE,
UPDATE,
Expand Down
3 changes: 3 additions & 0 deletions packages/ra-data-graphql/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ import {
QUERY_TYPES as INNER_QUERY_TYPES,
MUTATION_TYPES as INNER_MUTATION_TYPES,
ALL_TYPES as INNER_ALL_TYPES,
GET_COUNT as INNER_GET_COUNT,
} from './constants';
import defaultResolveIntrospection from './introspection';
export const QUERY_TYPES = INNER_QUERY_TYPES;
export const MUTATION_TYPES = INNER_MUTATION_TYPES;
export const ALL_TYPES = INNER_ALL_TYPES;
export const GET_COUNT = INNER_GET_COUNT;

const defaultOptions = {
resolveIntrospection: defaultResolveIntrospection,
introspection: {
operationNames: {
[GET_LIST]: resource => `all${pluralize(resource.name)}`,
[GET_COUNT]: resource => `_all${pluralize(resource.name)}Meta`,
[GET_ONE]: resource => `${resource.name}`,
[GET_MANY]: resource => `all${pluralize(resource.name)}`,
[GET_MANY_REFERENCE]: resource => `all${pluralize(resource.name)}`,
Expand Down

0 comments on commit 954a5f1

Please sign in to comment.