GraphQL: Inline Fragment on Array Fields (#5908)

* Inline Fragment Spec

* Inline Fragment on Arrays

* Fix Test

* Only select the root field

* Requested Changes

* Lazy Loaded ArrayResult
This commit is contained in:
Antoine Cormouls
2019-08-14 21:25:28 +02:00
committed by Antonio Davi Macedo Coelho de Castro
parent 45dabbbcda
commit 4bffdce047
7 changed files with 247 additions and 61 deletions

View File

@@ -539,6 +539,19 @@ describe('ParseGraphQLServer', () => {
expect(dateType.kind).toEqual('SCALAR');
});
it('should have ArrayResult type', async () => {
const arrayResultType = (await apolloClient.query({
query: gql`
query ArrayResultType {
__type(name: "ArrayResult") {
kind
}
}
`,
})).data['__type'];
expect(arrayResultType.kind).toEqual('UNION');
});
it('should have File object type', async () => {
const fileType = (await apolloClient.query({
query: gql`
@@ -746,6 +759,25 @@ describe('ParseGraphQLServer', () => {
).toBeTruthy(JSON.stringify(schemaTypes));
});
it('should ArrayResult contains all types', async () => {
const objectType = (await apolloClient.query({
query: gql`
query ObjectType {
__type(name: "ArrayResult") {
kind
possibleTypes {
name
}
}
}
`,
})).data['__type'];
const possibleTypes = objectType.possibleTypes.map(o => o.name);
expect(possibleTypes).toContain('_UserClass');
expect(possibleTypes).toContain('_RoleClass');
expect(possibleTypes).toContain('Element');
});
it('should update schema when it changes', async () => {
const schemaController = await parseServer.config.databaseController.loadSchema();
await schemaController.updateClass('_User', {
@@ -1661,6 +1693,84 @@ describe('ParseGraphQLServer', () => {
expect(new Date(result.updatedAt)).toEqual(obj.updatedAt);
});
it_only_db('mongo')(
'should return child objects in array fields',
async () => {
const obj1 = new Parse.Object('Customer');
const obj2 = new Parse.Object('SomeClass');
const obj3 = new Parse.Object('Customer');
obj1.set('someCustomerField', 'imCustomerOne');
const arrayField = [42.42, 42, 'string', true];
obj1.set('arrayField', arrayField);
await obj1.save();
obj2.set('someClassField', 'imSomeClassTwo');
await obj2.save();
//const obj3Relation = obj3.relation('manyRelations')
obj3.set('manyRelations', [obj1, obj2]);
await obj3.save();
await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();
const result = (await apolloClient.query({
query: gql`
query GetCustomer($objectId: ID!) {
objects {
getCustomer(objectId: $objectId) {
objectId
manyRelations {
... on CustomerClass {
objectId
someCustomerField
arrayField {
... on Element {
value
}
}
}
... on SomeClassClass {
objectId
someClassField
}
}
createdAt
updatedAt
}
}
}
`,
variables: {
objectId: obj3.id,
},
})).data.objects.getCustomer;
expect(result.objectId).toEqual(obj3.id);
expect(result.manyRelations.length).toEqual(2);
const customerSubObject = result.manyRelations.find(
o => o.objectId === obj1.id
);
const someClassSubObject = result.manyRelations.find(
o => o.objectId === obj2.id
);
expect(customerSubObject).toBeDefined();
expect(someClassSubObject).toBeDefined();
expect(customerSubObject.someCustomerField).toEqual(
'imCustomerOne'
);
const formatedArrayField = customerSubObject.arrayField.map(
elem => elem.value
);
expect(formatedArrayField).toEqual(arrayField);
expect(someClassSubObject.someClassField).toEqual(
'imSomeClassTwo'
);
}
);
it('should respect level permissions', async () => {
await prepareData();
@@ -5609,7 +5719,11 @@ describe('ParseGraphQLServer', () => {
findSomeClass(where: { someField: { _exists: true } }) {
results {
objectId
someField
someField {
... on Element {
value
}
}
}
}
}