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

* refactor(GraphQL): Pointer constraint input type as ID

Redefines the Pointer constraint input type from a custom scalar to
a simple ID.

* fix: PR review requested changes
This commit is contained in:
Douglas Muraoka
2019-09-04 19:46:18 -03:00
committed by Antonio Davi Macedo Coelho de Castro
parent 34f1bf384d
commit f9b77c1bc7
5 changed files with 68 additions and 109 deletions

View File

@@ -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);

View File

@@ -1,12 +1,10 @@
import {
Kind,
GraphQLID,
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLInputObjectType,
GraphQLNonNull,
GraphQLScalarType,
GraphQLEnumType,
} from 'graphql';
import getFieldNames from 'graphql-list-fields';
@@ -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,
@@ -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: {
equalTo: defaultGraphQLTypes.equalTo(classGraphQLScalarType),
notEqualTo: defaultGraphQLTypes.notEqualTo(classGraphQLScalarType),
in: defaultGraphQLTypes.inOp(classGraphQLScalarType),
notIn: defaultGraphQLTypes.notIn(classGraphQLScalarType),
equalTo: defaultGraphQLTypes.equalTo(GraphQLID),
notEqualTo: defaultGraphQLTypes.notEqualTo(GraphQLID),
in: defaultGraphQLTypes.inOp(defaultGraphQLTypes.OBJECT_ID),
notIn: defaultGraphQLTypes.notIn(defaultGraphQLTypes.OBJECT_ID),
exists: defaultGraphQLTypes.exists,
inQueryKey: defaultGraphQLTypes.inQueryKey,
notInQueryKey: defaultGraphQLTypes.notInQueryKey,
@@ -519,7 +437,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);
@@ -615,7 +534,6 @@ const load = (
parseGraphQLSchema.parseClassTypes[className] = {
classGraphQLPointerType,
classGraphQLRelationType,
classGraphQLScalarType,
classGraphQLCreateType,
classGraphQLUpdateType,
classGraphQLConstraintType,