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 b209df2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
13 changes: 11 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 @@ -164,6 +164,15 @@ export default introspectionResults => (
queryType,
variables
) => {
console.log(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}`
);
}

const { sortField, sortOrder, ...metaVariables } = variables;
const apolloArgs = buildApolloArgs(queryType, variables);
const args = buildArgs(queryType, variables);
Expand All @@ -186,7 +195,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
5 changes: 4 additions & 1 deletion packages/ra-data-graphql/src/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getIntrospectionQuery } from 'graphql';
import gql from 'graphql-tag';
import { GET_LIST, GET_ONE } from 'ra-core';

import { ALL_TYPES } from './constants';
import { ALL_TYPES, GET_COUNT } from './constants';

export const isResourceIncluded = ({ include, type }) => {
if (Array.isArray(include)) {
Expand Down Expand Up @@ -70,6 +70,9 @@ export default async (client, options) => {
) &&
queries.some(
query => query.name === options.operationNames[GET_ONE](type)
) &&
queries.some(
query => query.name === options.operationNames[GET_COUNT](type)
)
);
};
Expand Down
8 changes: 8 additions & 0 deletions packages/ra-data-graphql/src/introspection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
UPDATE,
DELETE,
} from 'ra-core';
import { GET_COUNT } from './constants';

describe('introspection', () => {
describe('isResourceIncluded', () => {
Expand Down Expand Up @@ -102,8 +103,10 @@ describe('introspection', () => {
name: 'Query',
fields: [
{ name: 'allPost' },
{ name: '_allPostMeta' },
{ name: 'Post' },
{ name: 'allComment' },
{ name: '_allCommentMeta' },
{ name: 'Comment' },
],
},
Expand Down Expand Up @@ -134,6 +137,7 @@ describe('introspection', () => {
[GET_LIST]: resource => `all${resource.name}`,
[GET_ONE]: resource => `${resource.name}`,
[GET_MANY]: resource => `all${resource.name}`,
[GET_COUNT]: resource => `_all${resource.name}Meta`,
[GET_MANY_REFERENCE]: resource => `all${resource.name}`,
[CREATE]: resource => `create${resource.name}`,
[UPDATE]: resource => `update${resource.name}`,
Expand All @@ -151,8 +155,10 @@ describe('introspection', () => {
const introspectionResults = await introspectionResultsPromise;
expect(introspectionResults.queries).toEqual([
{ name: 'allPost' },
{ name: '_allPostMeta' },
{ name: 'Post' },
{ name: 'allComment' },
{ name: '_allCommentMeta' },
{ name: 'Comment' },
{ name: 'createPost' },
{ name: 'updatePost' },
Expand All @@ -171,6 +177,7 @@ describe('introspection', () => {
[GET_LIST]: { name: 'allPost' },
[GET_ONE]: { name: 'Post' },
[GET_MANY]: { name: 'allPost' },
[GET_COUNT]: { name: '_allPostMeta' },
[GET_MANY_REFERENCE]: { name: 'allPost' },
[CREATE]: { name: 'createPost' },
[UPDATE]: { name: 'updatePost' },
Expand All @@ -181,6 +188,7 @@ describe('introspection', () => {
[GET_LIST]: { name: 'allComment' },
[GET_ONE]: { name: 'Comment' },
[GET_MANY]: { name: 'allComment' },
[GET_COUNT]: { name: '_allCommentMeta' },
[GET_MANY_REFERENCE]: { name: 'allComment' },
},
]);
Expand Down

0 comments on commit b209df2

Please sign in to comment.