Skip to content

Commit b8b7fcd

Browse files
authoredOct 1, 2020
Merge pull request #5305 from Kilometers42/gql-data-include-introspection
ra-data-graphql: Update include/exclude introspection logic
2 parents 00e651d + 199012f commit b8b7fcd

File tree

2 files changed

+61
-39
lines changed

2 files changed

+61
-39
lines changed
 

‎packages/ra-data-graphql/src/introspection.js

+23-16
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ import { GET_LIST, GET_ONE } from 'ra-core';
44

55
import { ALL_TYPES } from './constants';
66

7-
export const filterTypesByIncludeExclude = ({ include, exclude }) => {
7+
export const isResourceIncluded = ({ include, type }) => {
88
if (Array.isArray(include)) {
9-
return type => include.includes(type.name);
9+
return include.includes(type.name);
1010
}
1111

1212
if (typeof include === 'function') {
13-
return type => include(type);
13+
return include(type);
1414
}
1515

16+
return false;
17+
};
18+
19+
export const isResourceExcluded = ({ exclude, type }) => {
1620
if (Array.isArray(exclude)) {
17-
return type => !exclude.includes(type.name);
21+
return exclude.includes(type.name);
1822
}
1923

2024
if (typeof exclude === 'function') {
21-
return type => !exclude(type);
25+
return exclude(type);
2226
}
2327

24-
return () => true;
28+
return false;
2529
};
2630

2731
/**
@@ -56,13 +60,19 @@ export default async (client, options) => {
5660
type.name !== (schema.mutationType && schema.mutationType.name)
5761
);
5862

59-
const isResource = type =>
60-
queries.some(
61-
query => query.name === options.operationNames[GET_LIST](type)
62-
) &&
63-
queries.some(
64-
query => query.name === options.operationNames[GET_ONE](type)
63+
const isResource = type => {
64+
if (isResourceIncluded({ type, ...options })) return true;
65+
if (isResourceExcluded({ type, ...options })) return false;
66+
67+
return (
68+
queries.some(
69+
query => query.name === options.operationNames[GET_LIST](type)
70+
) &&
71+
queries.some(
72+
query => query.name === options.operationNames[GET_ONE](type)
73+
)
6574
);
75+
};
6676

6777
const buildResource = type =>
6878
ALL_TYPES.reduce(
@@ -78,10 +88,7 @@ export default async (client, options) => {
7888
{ type }
7989
);
8090

81-
const potentialResources = types.filter(isResource);
82-
const filteredResources = potentialResources.filter(
83-
filterTypesByIncludeExclude(options)
84-
);
91+
const filteredResources = types.filter(isResource);
8592
const resources = filteredResources.map(buildResource);
8693

8794
return {

‎packages/ra-data-graphql/src/introspection.test.js

+38-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import resolveIntrospection, {
2-
filterTypesByIncludeExclude,
2+
isResourceExcluded,
3+
isResourceIncluded,
34
} from './introspection';
45
import {
56
GET_LIST,
@@ -12,64 +13,78 @@ import {
1213
} from 'ra-core';
1314

1415
describe('introspection', () => {
15-
describe('filterTypesByIncludeExclude', () => {
16+
describe('isResourceIncluded', () => {
1617
it('return false with an include option containing an array and tested type is not in it', () => {
1718
expect(
18-
filterTypesByIncludeExclude({ include: ['Post', 'Comment'] })({
19-
name: 'NotMe',
19+
isResourceIncluded({
20+
include: ['Post', 'Comment'],
21+
type: {
22+
name: 'NotMe',
23+
},
2024
})
2125
).toBe(false);
2226
});
2327

2428
it('return true with an include option containing an array and tested type is in it', () => {
2529
expect(
26-
filterTypesByIncludeExclude({ include: ['Post', 'Comment'] })({
27-
name: 'Post',
30+
isResourceIncluded({
31+
include: ['Post', 'Comment'],
32+
type: {
33+
name: 'Post',
34+
},
2835
})
2936
).toBe(true);
3037
});
3138

32-
it('return false with an exclude option containing an array and tested type is in it', () => {
39+
it('return false with an include option containing an array and tested type is not in it', () => {
3340
expect(
34-
filterTypesByIncludeExclude({ exclude: ['NotMe'] })({
35-
name: 'NotMe',
41+
isResourceIncluded({
42+
include: ['NotMe'],
43+
type: {
44+
name: 'Post',
45+
},
3646
})
3747
).toBe(false);
3848
});
3949

40-
it('return true with an include option containing an array and tested type is not in it', () => {
41-
expect(
42-
filterTypesByIncludeExclude({ exclude: ['NotMe'] })({
43-
name: 'Post',
44-
})
45-
).toBe(true);
46-
});
47-
4850
it('return true with an include option being a function returning true', () => {
4951
const include = jest.fn(() => true);
5052
const type = { name: 'Post' };
51-
expect(filterTypesByIncludeExclude({ include })(type)).toBe(true);
53+
expect(isResourceIncluded({ include, type })).toBe(true);
5254
expect(include).toHaveBeenCalledWith(type);
5355
});
5456

5557
it('return false with an include option being a function returning false', () => {
5658
const include = jest.fn(() => false);
5759
const type = { name: 'Post' };
58-
expect(filterTypesByIncludeExclude({ include })(type)).toBe(false);
60+
expect(isResourceIncluded({ include, type })).toBe(false);
5961
expect(include).toHaveBeenCalledWith(type);
6062
});
63+
});
64+
65+
describe('isResourceExcluded', () => {
66+
it('return true with an exclude option containing an array and tested type is in it', () => {
67+
expect(
68+
isResourceExcluded({
69+
exclude: ['NotMe'],
70+
type: {
71+
name: 'NotMe',
72+
},
73+
})
74+
).toBe(true);
75+
});
6176

62-
it('return false with an exclude option being a function returning true', () => {
77+
it('return true with an exclude option being a function returning true', () => {
6378
const exclude = jest.fn(() => true);
6479
const type = { name: 'Post' };
65-
expect(filterTypesByIncludeExclude({ exclude })(type)).toBe(false);
80+
expect(isResourceExcluded({ exclude, type })).toBe(true);
6681
expect(exclude).toHaveBeenCalledWith(type);
6782
});
6883

69-
it('return true with an exclude option being a function returning false', () => {
84+
it('return false with an exclude option being a function returning false', () => {
7085
const exclude = jest.fn(() => false);
7186
const type = { name: 'Post' };
72-
expect(filterTypesByIncludeExclude({ exclude })(type)).toBe(true);
87+
expect(isResourceExcluded({ exclude, type })).toBe(false);
7388
expect(exclude).toHaveBeenCalledWith(type);
7489
});
7590
});

0 commit comments

Comments
 (0)