Skip to content

Commit 5d76b2f

Browse files
Moumoulsdavimacedo
authored andcommitted
GraphQL: DX Relational Where Query (#6255)
* DX Relational Where Query * Remove WherePointer & fix tests * Add have, haveNot, exists on Pointer/Relation where input * Merge branch 'master' into gql-relational-query * Enable inQueryKey * better descrption
1 parent afe49cb commit 5d76b2f

File tree

7 files changed

+519
-160
lines changed

7 files changed

+519
-160
lines changed

spec/ParseGraphQLServer.spec.js

+321-5
Original file line numberDiff line numberDiff line change
@@ -4915,7 +4915,11 @@ describe('ParseGraphQLServer', () => {
49154915
OR: [
49164916
{
49174917
pointerToUser: {
4918-
equalTo: user5.id,
4918+
have: {
4919+
objectId: {
4920+
equalTo: user5.id,
4921+
},
4922+
},
49194923
},
49204924
},
49214925
{
@@ -4960,7 +4964,11 @@ describe('ParseGraphQLServer', () => {
49604964
variables: {
49614965
where: {
49624966
pointerToUser: {
4963-
in: [user5.id],
4967+
have: {
4968+
objectId: {
4969+
in: [user5.id],
4970+
},
4971+
},
49644972
},
49654973
},
49664974
},
@@ -5063,6 +5071,72 @@ describe('ParseGraphQLServer', () => {
50635071
}
50645072
});
50655073

5074+
it('should support in query key', async () => {
5075+
try {
5076+
const country = new Parse.Object('Country');
5077+
country.set('code', 'FR');
5078+
await country.save();
5079+
5080+
const country2 = new Parse.Object('Country');
5081+
country2.set('code', 'US');
5082+
await country2.save();
5083+
5084+
const city = new Parse.Object('City');
5085+
city.set('country', 'FR');
5086+
city.set('name', 'city1');
5087+
await city.save();
5088+
5089+
const city2 = new Parse.Object('City');
5090+
city2.set('country', 'US');
5091+
city2.set('name', 'city2');
5092+
await city2.save();
5093+
5094+
await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();
5095+
5096+
const {
5097+
data: {
5098+
cities: { edges: result },
5099+
},
5100+
} = await apolloClient.query({
5101+
query: gql`
5102+
query inQueryKey($where: CityWhereInput) {
5103+
cities(where: $where) {
5104+
edges {
5105+
node {
5106+
country
5107+
name
5108+
}
5109+
}
5110+
}
5111+
}
5112+
`,
5113+
context: {
5114+
headers: {
5115+
'X-Parse-Master-Key': 'test',
5116+
},
5117+
},
5118+
variables: {
5119+
where: {
5120+
country: {
5121+
inQueryKey: {
5122+
query: {
5123+
className: 'Country',
5124+
where: { code: { equalTo: 'US' } },
5125+
},
5126+
key: 'code',
5127+
},
5128+
},
5129+
},
5130+
},
5131+
});
5132+
5133+
expect(result.length).toEqual(1);
5134+
expect(result[0].node.name).toEqual('city2');
5135+
} catch (e) {
5136+
handleError(e);
5137+
}
5138+
});
5139+
50665140
it('should support order, skip and first arguments', async () => {
50675141
const promises = [];
50685142
for (let i = 0; i < 100; i++) {
@@ -5278,7 +5352,11 @@ describe('ParseGraphQLServer', () => {
52785352
OR: [
52795353
{
52805354
pointerToUser: {
5281-
equalTo: user5.id,
5355+
have: {
5356+
objectId: {
5357+
equalTo: user5.id,
5358+
},
5359+
},
52825360
},
52835361
},
52845362
{
@@ -5332,7 +5410,11 @@ describe('ParseGraphQLServer', () => {
53325410
OR: [
53335411
{
53345412
pointerToUser: {
5335-
equalTo: user5.id,
5413+
have: {
5414+
objectId: {
5415+
equalTo: user5.id,
5416+
},
5417+
},
53365418
},
53375419
},
53385420
{
@@ -5746,7 +5828,11 @@ describe('ParseGraphQLServer', () => {
57465828
variables: {
57475829
where: {
57485830
pointerToUser: {
5749-
inQuery: { where: {}, className: '_User' },
5831+
have: {
5832+
objectId: {
5833+
equalTo: 'xxxx',
5834+
},
5835+
},
57505836
},
57515837
},
57525838
},
@@ -8557,6 +8643,236 @@ describe('ParseGraphQLServer', () => {
85578643
expect(result2.companies.edges[0].node.objectId).toEqual(company1.id);
85588644
});
85598645

8646+
it_only_db('mongo')(
8647+
'should support relational where query',
8648+
async () => {
8649+
const president = new Parse.Object('President');
8650+
president.set('name', 'James');
8651+
await president.save();
8652+
8653+
const employee = new Parse.Object('Employee');
8654+
employee.set('name', 'John');
8655+
await employee.save();
8656+
8657+
const company1 = new Parse.Object('Company');
8658+
company1.set('name', 'imACompany1');
8659+
await company1.save();
8660+
8661+
const company2 = new Parse.Object('Company');
8662+
company2.set('name', 'imACompany2');
8663+
company2.relation('employees').add([employee]);
8664+
await company2.save();
8665+
8666+
const country = new Parse.Object('Country');
8667+
country.set('name', 'imACountry');
8668+
country.relation('companies').add([company1, company2]);
8669+
await country.save();
8670+
8671+
const country2 = new Parse.Object('Country');
8672+
country2.set('name', 'imACountry2');
8673+
country2.relation('companies').add([company1]);
8674+
await country2.save();
8675+
8676+
const country3 = new Parse.Object('Country');
8677+
country3.set('name', 'imACountry3');
8678+
country3.set('president', president);
8679+
await country3.save();
8680+
8681+
await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();
8682+
8683+
let {
8684+
data: {
8685+
countries: { edges: result },
8686+
},
8687+
} = await apolloClient.query({
8688+
query: gql`
8689+
query findCountry($where: CountryWhereInput) {
8690+
countries(where: $where) {
8691+
edges {
8692+
node {
8693+
id
8694+
objectId
8695+
companies {
8696+
edges {
8697+
node {
8698+
id
8699+
objectId
8700+
name
8701+
}
8702+
}
8703+
}
8704+
}
8705+
}
8706+
}
8707+
}
8708+
`,
8709+
variables: {
8710+
where: {
8711+
companies: {
8712+
have: {
8713+
employees: { have: { name: { equalTo: 'John' } } },
8714+
},
8715+
},
8716+
},
8717+
},
8718+
});
8719+
expect(result.length).toEqual(1);
8720+
result = result[0].node;
8721+
expect(result.objectId).toEqual(country.id);
8722+
expect(result.companies.edges.length).toEqual(2);
8723+
8724+
const {
8725+
data: {
8726+
countries: { edges: result2 },
8727+
},
8728+
} = await apolloClient.query({
8729+
query: gql`
8730+
query findCountry($where: CountryWhereInput) {
8731+
countries(where: $where) {
8732+
edges {
8733+
node {
8734+
id
8735+
objectId
8736+
companies {
8737+
edges {
8738+
node {
8739+
id
8740+
objectId
8741+
name
8742+
}
8743+
}
8744+
}
8745+
}
8746+
}
8747+
}
8748+
}
8749+
`,
8750+
variables: {
8751+
where: {
8752+
companies: {
8753+
have: {
8754+
OR: [
8755+
{ name: { equalTo: 'imACompany1' } },
8756+
{ name: { equalTo: 'imACompany2' } },
8757+
],
8758+
},
8759+
},
8760+
},
8761+
},
8762+
});
8763+
expect(result2.length).toEqual(2);
8764+
8765+
const {
8766+
data: {
8767+
countries: { edges: result3 },
8768+
},
8769+
} = await apolloClient.query({
8770+
query: gql`
8771+
query findCountry($where: CountryWhereInput) {
8772+
countries(where: $where) {
8773+
edges {
8774+
node {
8775+
id
8776+
name
8777+
}
8778+
}
8779+
}
8780+
}
8781+
`,
8782+
variables: {
8783+
where: {
8784+
companies: { exists: false },
8785+
},
8786+
},
8787+
});
8788+
expect(result3.length).toEqual(1);
8789+
expect(result3[0].node.name).toEqual('imACountry3');
8790+
8791+
const {
8792+
data: {
8793+
countries: { edges: result4 },
8794+
},
8795+
} = await apolloClient.query({
8796+
query: gql`
8797+
query findCountry($where: CountryWhereInput) {
8798+
countries(where: $where) {
8799+
edges {
8800+
node {
8801+
id
8802+
name
8803+
}
8804+
}
8805+
}
8806+
}
8807+
`,
8808+
variables: {
8809+
where: {
8810+
president: { exists: false },
8811+
},
8812+
},
8813+
});
8814+
expect(result4.length).toEqual(2);
8815+
const {
8816+
data: {
8817+
countries: { edges: result5 },
8818+
},
8819+
} = await apolloClient.query({
8820+
query: gql`
8821+
query findCountry($where: CountryWhereInput) {
8822+
countries(where: $where) {
8823+
edges {
8824+
node {
8825+
id
8826+
name
8827+
}
8828+
}
8829+
}
8830+
}
8831+
`,
8832+
variables: {
8833+
where: {
8834+
president: { exists: true },
8835+
},
8836+
},
8837+
});
8838+
expect(result5.length).toEqual(1);
8839+
const {
8840+
data: {
8841+
countries: { edges: result6 },
8842+
},
8843+
} = await apolloClient.query({
8844+
query: gql`
8845+
query findCountry($where: CountryWhereInput) {
8846+
countries(where: $where) {
8847+
edges {
8848+
node {
8849+
id
8850+
objectId
8851+
name
8852+
}
8853+
}
8854+
}
8855+
}
8856+
`,
8857+
variables: {
8858+
where: {
8859+
companies: {
8860+
haveNot: {
8861+
OR: [
8862+
{ name: { equalTo: 'imACompany1' } },
8863+
{ name: { equalTo: 'imACompany2' } },
8864+
],
8865+
},
8866+
},
8867+
},
8868+
},
8869+
});
8870+
expect(result6.length).toEqual(1);
8871+
expect(result6.length).toEqual(1);
8872+
expect(result6[0].node.name).toEqual('imACountry3');
8873+
}
8874+
);
8875+
85608876
it('should support files', async () => {
85618877
try {
85628878
parseServer = await global.reconfigureServer({

src/GraphQL/helpers/objectsQueries.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ const findObjects = async (
6767
auth,
6868
info,
6969
selectedFields,
70-
fields
70+
parseClasses
7171
) => {
7272
if (!where) {
7373
where = {};
7474
}
75-
transformQueryInputToParse(where, fields, className);
76-
75+
transformQueryInputToParse(where, className, parseClasses);
7776
const skipAndLimitCalculation = calculateSkipAndLimit(
7877
skipInput,
7978
first,

0 commit comments

Comments
 (0)