Skip to content

GraphQL: Inline Fragment on Array Fields #5908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 115 additions & 1 deletion spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -5601,7 +5711,11 @@ describe('ParseGraphQLServer', () => {
findSomeClass(where: { someField: { _exists: true } }) {
results {
objectId
someField
someField {
... on Element {
value
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ParseGraphQLSchema {
parseClassMutations.load(this, parseClass, parseClassConfig);
}
);

defaultGraphQLTypes.loadArrayResult(this, parseClasses);
defaultGraphQLQueries.load(this);
defaultGraphQLMutations.load(this);

Expand Down
54 changes: 54 additions & 0 deletions src/GraphQL/loaders/defaultGraphQLTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
GraphQLList,
GraphQLInputObjectType,
GraphQLBoolean,
GraphQLUnionType,
} from 'graphql';
import { GraphQLUpload } from 'graphql-upload';

Expand Down Expand Up @@ -1020,6 +1021,55 @@ const SIGN_UP_RESULT = new GraphQLObjectType({
},
});

const ELEMENT = new GraphQLObjectType({
name: 'Element',
description:
'The SignUpResult object type is used in the users sign up mutation to return the data of the recent created user.',
fields: {
value: {
description: 'Return the value of the element in the array',
type: new GraphQLNonNull(ANY),
},
},
});

// Default static union type, we update types and resolveType function later
let ARRAY_RESULT;

const loadArrayResult = (parseGraphQLSchema, parseClasses) => {
const classTypes = parseClasses
.filter(parseClass =>
parseGraphQLSchema.parseClassTypes[parseClass.className]
.classGraphQLOutputType
? true
: false
)
.map(
parseClass =>
parseGraphQLSchema.parseClassTypes[parseClass.className]
.classGraphQLOutputType
);
ARRAY_RESULT = new GraphQLUnionType({
name: 'ArrayResult',
description:
'Use Inline Fragment on Array to get results: https://graphql.org/learn/queries/#inline-fragments',
types: () => [ELEMENT, ...classTypes],
resolveType: value => {
if (value.__type === 'Object' && value.className && value.objectId) {
if (parseGraphQLSchema.parseClassTypes[value.className]) {
return parseGraphQLSchema.parseClassTypes[value.className]
.classGraphQLOutputType;
} else {
return ELEMENT;
}
} else {
return ELEMENT;
}
},
});
parseGraphQLSchema.graphQLTypes.push(ARRAY_RESULT);
};

const load = parseGraphQLSchema => {
parseGraphQLSchema.graphQLTypes.push(GraphQLUpload);
parseGraphQLSchema.graphQLTypes.push(ANY);
Expand Down Expand Up @@ -1056,6 +1106,7 @@ const load = parseGraphQLSchema => {
parseGraphQLSchema.graphQLTypes.push(POLYGON_CONSTRAINT);
parseGraphQLSchema.graphQLTypes.push(FIND_RESULT);
parseGraphQLSchema.graphQLTypes.push(SIGN_UP_RESULT);
parseGraphQLSchema.graphQLTypes.push(ELEMENT);
};

export {
Expand Down Expand Up @@ -1140,5 +1191,8 @@ export {
POLYGON_CONSTRAINT,
FIND_RESULT,
SIGN_UP_RESULT,
ARRAY_RESULT,
ELEMENT,
load,
loadArrayResult,
};
14 changes: 4 additions & 10 deletions src/GraphQL/loaders/parseClassMutations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GraphQLNonNull } from 'graphql';
import getFieldNames from 'graphql-list-fields';
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
import * as parseClassTypes from './parseClassTypes';
import { extractKeysAndInclude } from '../parseGraphQLUtils';
import * as objectsMutations from './objectsMutations';
import * as objectsQueries from './objectsQueries';
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
Expand Down Expand Up @@ -119,9 +119,7 @@ const load = function(
info
);
const selectedFields = getFieldNames(mutationInfo);
const { keys, include } = parseClassTypes.extractKeysAndInclude(
selectedFields
);
const { keys, include } = extractKeysAndInclude(selectedFields);
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
fields,
keys,
Expand Down Expand Up @@ -180,9 +178,7 @@ const load = function(
info
);
const selectedFields = getFieldNames(mutationInfo);
const { keys, include } = parseClassTypes.extractKeysAndInclude(
selectedFields
);
const { keys, include } = extractKeysAndInclude(selectedFields);

const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
fields,
Expand Down Expand Up @@ -225,9 +221,7 @@ const load = function(
const { objectId } = args;
const { config, auth, info } = context;
const selectedFields = getFieldNames(mutationInfo);
const { keys, include } = parseClassTypes.extractKeysAndInclude(
selectedFields
);
const { keys, include } = extractKeysAndInclude(selectedFields);

let optimizedObject = {};
const splitedKeys = keys.split(',');
Expand Down
44 changes: 23 additions & 21 deletions src/GraphQL/loaders/parseClassQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@ import { GraphQLNonNull } from 'graphql';
import getFieldNames from 'graphql-list-fields';
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
import * as objectsQueries from './objectsQueries';
import * as parseClassTypes from './parseClassTypes';
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
import { extractKeysAndInclude } from '../parseGraphQLUtils';

const getParseClassQueryConfig = function(
parseClassConfig: ?ParseGraphQLClassConfig
) {
return (parseClassConfig && parseClassConfig.query) || {};
};

const getQuery = async (className, _source, args, context, queryInfo) => {
const { objectId, readPreference, includeReadPreference } = args;
const { config, auth, info } = context;
const selectedFields = getFieldNames(queryInfo);

const { keys, include } = extractKeysAndInclude(selectedFields);

return await objectsQueries.getObject(
className,
objectId,
keys,
include,
readPreference,
includeReadPreference,
config,
auth,
info
);
};

const load = function(
parseGraphQLSchema,
parseClass,
Expand Down Expand Up @@ -40,25 +60,7 @@ const load = function(
type: new GraphQLNonNull(classGraphQLOutputType),
async resolve(_source, args, context, queryInfo) {
try {
const { objectId, readPreference, includeReadPreference } = args;
const { config, auth, info } = context;
const selectedFields = getFieldNames(queryInfo);

const { keys, include } = parseClassTypes.extractKeysAndInclude(
selectedFields
);

return await objectsQueries.getObject(
className,
objectId,
keys,
include,
readPreference,
includeReadPreference,
config,
auth,
info
);
return await getQuery(className, _source, args, context, queryInfo);
} catch (e) {
parseGraphQLSchema.handleError(e);
}
Expand Down Expand Up @@ -86,7 +88,7 @@ const load = function(
const { config, auth, info } = context;
const selectedFields = getFieldNames(queryInfo);

const { keys, include } = parseClassTypes.extractKeysAndInclude(
const { keys, include } = extractKeysAndInclude(
selectedFields
.filter(field => field.includes('.'))
.map(field => field.slice(field.indexOf('.') + 1))
Expand Down
Loading