Skip to content
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

refactor(GraphQL): Pointer constraint input type as ID #6020

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 37 additions & 15 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3416,11 +3416,7 @@ describe('ParseGraphQLServer', () => {
_or: [
{
pointerToUser: {
_eq: {
__type: 'Pointer',
className: '_User',
objectId: user5.id,
},
_eq: user5.id,
},
},
{
Expand All @@ -3445,6 +3441,40 @@ describe('ParseGraphQLServer', () => {
).toEqual(['someValue1', 'someValue3']);
});

it('should support _in pointer operator using class specific query', async () => {
await prepareData();

await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();

const result = await apolloClient.query({
query: gql`
query FindSomeObjects($where: GraphQLClassWhereInput) {
graphQLClasses(where: $where) {
results {
someField
}
}
}
`,
variables: {
where: {
pointerToUser: {
_in: [user5.id],
},
},
},
context: {
headers: {
'X-Parse-Master-Key': 'test',
},
},
});

const { results } = result.data.graphQLClasses;
expect(results.length).toBe(1);
expect(results[0].someField).toEqual('someValue3');
});

it('should support _or operation', async () => {
await prepareData();

Expand Down Expand Up @@ -3543,11 +3573,7 @@ describe('ParseGraphQLServer', () => {
_or: [
{
pointerToUser: {
_eq: {
__type: 'Pointer',
className: '_User',
objectId: user5.id,
},
_eq: user5.id,
},
},
{
Expand Down Expand Up @@ -3599,11 +3625,7 @@ describe('ParseGraphQLServer', () => {
_or: [
{
pointerToUser: {
_eq: {
__type: 'Pointer',
className: '_User',
objectId: user5.id,
},
_eq: user5.id,
},
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/GraphQL/helpers/objectsQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ const findObjects = async (
config,
auth,
info,
selectedFields
selectedFields,
fields
) => {
if (!where) {
where = {};
}
transformQueryInputToParse(where);
transformQueryInputToParse(where, fields);

const options = {};

Expand Down
3 changes: 2 additions & 1 deletion src/GraphQL/loaders/parseClassQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ const load = function(
config,
auth,
info,
selectedFields.map(field => field.split('.', 1)[0])
selectedFields.map(field => field.split('.', 1)[0]),
parseClass.fields
);
} catch (e) {
parseGraphQLSchema.handleError(e);
Expand Down
94 changes: 6 additions & 88 deletions src/GraphQL/loaders/parseClassTypes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
Kind,
GraphQLID,
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLInputObjectType,
GraphQLNonNull,
GraphQLScalarType,
GraphQLEnumType,
} from 'graphql';
import getFieldNames from 'graphql-list-fields';
Expand Down Expand Up @@ -138,86 +136,6 @@ const load = (
update: isUpdateEnabled = true,
} = getParseClassMutationConfig(parseClassConfig);

const classGraphQLScalarTypeName = `${graphQLClassName}Pointer`;
const parseScalarValue = value => {
if (typeof value === 'string') {
return {
__type: 'Pointer',
className: className,
objectId: value,
};
} else if (
typeof value === 'object' &&
value.__type === 'Pointer' &&
value.className === className &&
typeof value.objectId === 'string'
) {
return { ...value, className };
}

throw new defaultGraphQLTypes.TypeValidationError(
value,
classGraphQLScalarTypeName
);
};
let classGraphQLScalarType = new GraphQLScalarType({
name: classGraphQLScalarTypeName,
description: `The ${classGraphQLScalarTypeName} is used in operations that involve ${graphQLClassName} pointers.`,
parseValue: parseScalarValue,
serialize(value) {
if (typeof value === 'string') {
return value;
} else if (
typeof value === 'object' &&
value.__type === 'Pointer' &&
value.className === className &&
typeof value.objectId === 'string'
) {
return value.objectId;
}

throw new defaultGraphQLTypes.TypeValidationError(
value,
classGraphQLScalarTypeName
);
},
parseLiteral(ast) {
if (ast.kind === Kind.STRING) {
return parseScalarValue(ast.value);
} else if (ast.kind === Kind.OBJECT) {
const __type = ast.fields.find(field => field.name.value === '__type');
const className = ast.fields.find(
field => field.name.value === 'className'
);
const objectId = ast.fields.find(
field => field.name.value === 'objectId'
);
if (
__type &&
__type.value &&
className &&
className.value &&
objectId &&
objectId.value
) {
return parseScalarValue({
__type: __type.value.value,
className: className.value.value,
objectId: objectId.value.value,
});
}
}

throw new defaultGraphQLTypes.TypeValidationError(
ast.kind,
classGraphQLScalarTypeName
);
},
});
classGraphQLScalarType =
parseGraphQLSchema.addGraphQLType(classGraphQLScalarType) ||
defaultGraphQLTypes.OBJECT;

const classGraphQLCreateTypeName = `Create${graphQLClassName}FieldsInput`;
let classGraphQLCreateType = new GraphQLInputObjectType({
name: classGraphQLCreateTypeName,
Expand Down Expand Up @@ -341,10 +259,10 @@ const load = (
name: classGraphQLConstraintTypeName,
description: `The ${classGraphQLConstraintTypeName} input type is used in operations that involve filtering objects by a pointer field to ${graphQLClassName} class.`,
fields: {
_eq: defaultGraphQLTypes._eq(classGraphQLScalarType),
_ne: defaultGraphQLTypes._ne(classGraphQLScalarType),
_in: defaultGraphQLTypes._in(classGraphQLScalarType),
_nin: defaultGraphQLTypes._nin(classGraphQLScalarType),
_eq: defaultGraphQLTypes._eq(GraphQLID),
_ne: defaultGraphQLTypes._ne(GraphQLID),
_in: defaultGraphQLTypes._in(defaultGraphQLTypes.OBJECT_ID),
_nin: defaultGraphQLTypes._nin(defaultGraphQLTypes.OBJECT_ID),
_exists: defaultGraphQLTypes._exists,
_select: defaultGraphQLTypes._select,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also improve these following 4 operators. But we can address it in a separate PR. I will take a note in the project.

_dontSelect: defaultGraphQLTypes._dontSelect,
Expand Down Expand Up @@ -513,7 +431,8 @@ const load = (
config,
auth,
info,
selectedFields.map(field => field.split('.', 1)[0])
selectedFields.map(field => field.split('.', 1)[0]),
parseClass.fields
);
} catch (e) {
parseGraphQLSchema.handleError(e);
Expand Down Expand Up @@ -609,7 +528,6 @@ const load = (
parseGraphQLSchema.parseClassTypes[className] = {
classGraphQLPointerType,
classGraphQLRelationType,
classGraphQLScalarType,
classGraphQLCreateType,
classGraphQLUpdateType,
classGraphQLConstraintType,
Expand Down
18 changes: 17 additions & 1 deletion src/GraphQL/transformers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const parseMap = {

const transformQueryInputToParse = (
constraints,
fields,
parentFieldName,
parentConstraints
) => {
Expand Down Expand Up @@ -93,6 +94,21 @@ const transformQueryInputToParse = (
delete constraints[fieldName];
fieldName = parseMap[fieldName];
constraints[fieldName] = fieldValue;

// If parent field type is Pointer, changes constraint value to format expected
// by Parse.
if (
fields[parentFieldName] &&
fields[parentFieldName].type === 'Pointer' &&
typeof fieldValue === 'string'
) {
const { targetClass } = fields[parentFieldName];
constraints[fieldName] = {
__type: 'Pointer',
className: targetClass,
objectId: fieldValue,
};
}
}
switch (fieldName) {
case '$point':
Expand Down Expand Up @@ -147,7 +163,7 @@ const transformQueryInputToParse = (
break;
}
if (typeof fieldValue === 'object') {
transformQueryInputToParse(fieldValue, fieldName, constraints);
transformQueryInputToParse(fieldValue, fields, fieldName, constraints);
}
});
};
Expand Down