Files
kami-parse-server/src/GraphQL/loaders/objectsQueries.js

406 lines
9.8 KiB
JavaScript
Raw Normal View History

GraphQL Support (#5674) * GraphQL boilerplate * Create GraphQL schema without using gql * Introducing loaders * Generic create mutation * create mutation is now working for any data type * Create mutation for each parse class - partial * Adding more data types to the class * Get parse class query * Generic get query * Generic delete mutation * Parse class delete mutation * Parse class find mutation * Generic update mutation * Parse class update mutation * Fixing initialization problems * Installing node-fetch again * Basic implementation for Pointer * Constructor tests * API tests boilerplate * _getGraphQLOptions * applyGraphQL tests * GraphQL API initial tests * applyPlayground tests * createSubscriptions tests * ParseGrapjQLSchema tests file * ParseGraphQLSchema tests * TypeValidationError * TypeValidationError * parseStringValue test * parseIntValue tests * parseBooleanValue tests * parseDateValue tests * parseValue tests * parseListValues tests * parseObjectFields tests * Default types tests * Get tests * First permission test at generic Get operation * Fixing prepare data * ApolloClient does not work well with different queries runnning in paralell with different headers * ApolloClient does not work well with different queries runnning in paralell with different headers * User 3 tests * User 3 tests * Get level permission tests * Get User specific tests * Get now support keys argument * Get now supports include argument * Get now supports read preferences * Adding tests for read preference enum type * Find basic test * Find permissions test * Find where argument test * Order, skip and limit tests * Error handler * Find now supports count * Test for FindResult type * Improving find count * Find max limit test * Find now supports keys, include and includeAll * Find now supports read preferences * Basic Create test * Generic create mutation tests * Basic update test * UpdateResult object type test * Update level permissions tests * Error handler for default mutations * Delete mutation basic test * Delete mutation level permission tests * Test for string * String test * Date test * Pointer test * Relation tests * Changing objects mutations location * Changing objects queries location * Create file mutation * Test for file fields * Test for null values * Changing parse classes operations location * Objects mutations refactoring * Class specific create object mutation now working * Update class specific mutation now working * Specific class delete mutation now working * Get class specific mutation now working * Find class specific query now working without where and sort * Find query for custom classes working with where partially * Almost all data types working for specfic class find where * Now only missing relation, geopoint, file and ACL * Additional tests with Parse classes queries and mutations * Now only missing relation, geopoint, file and ACL * Files * Fiels are now working * Excluding missing order test temporarly * Refactoring dates * Refactoring files * Default types review * Refeactoring object queries * Refactoring class scalar type * Refactoring class types * Geo queries are now working * Fixing centerSphere * Allow sort on class specific queries * Supporting bytes * ACL constraint * Temporarly removing xit tests * Fixing some tests because of schema cache * Removing session token from users * Parse.User queries and mutations * Remove test using fit * Fixing include test that was failing because of schema cache * Fixing count test for postgres. Postgres does not count with where={} (legacy problem). We should solve it later * Fix null values test for postgres. It is evaluating null as undefined (legacy problem) and we should fix is later. * Fixing schema change test that was failing because of schema cache * Add GraphQL File type parseLiteral tests * Refeactoring users * Including sign up mutation * Fix failing test * Improve default GraphQL types tests coverage * Including some tests for data types * Including additional pointer test: * Fixing some tests * more data type tests * Include Bytes and Polygon data types tests * Polygons test * Merging other tests * Fixing some postgres tests
2019-06-19 17:19:47 -07:00
import {
GraphQLNonNull,
GraphQLBoolean,
GraphQLString,
GraphQLObjectType,
} from 'graphql';
import getFieldNames from 'graphql-list-fields';
import Parse from 'parse/node';
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
import rest from '../../rest';
const getObject = async (
className,
objectId,
keys,
include,
readPreference,
includeReadPreference,
config,
auth,
info
) => {
const options = {};
if (keys) {
options.keys = keys;
}
if (include) {
options.include = include;
if (includeReadPreference) {
options.includeReadPreference = includeReadPreference;
}
}
if (readPreference) {
options.readPreference = readPreference;
}
const response = await rest.get(
config,
auth,
className,
objectId,
options,
info.clientSDK
);
if (!response.results || response.results.length == 0) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
}
if (className === '_User') {
delete response.results[0].sessionToken;
}
return response.results[0];
};
const parseMap = {
_or: '$or',
_and: '$and',
_nor: '$nor',
_relatedTo: '$relatedTo',
_eq: '$eq',
_ne: '$ne',
_lt: '$lt',
_lte: '$lte',
_gt: '$gt',
_gte: '$gte',
_in: '$in',
_nin: '$nin',
_exists: '$exists',
_select: '$select',
_dontSelect: '$dontSelect',
_inQuery: '$inQuery',
_notInQuery: '$notInQuery',
_containedBy: '$containedBy',
_all: '$all',
_regex: '$regex',
_options: '$options',
_text: '$text',
_search: '$search',
_term: '$term',
_language: '$language',
_caseSensitive: '$caseSensitive',
_diacriticSensitive: '$diacriticSensitive',
_nearSphere: '$nearSphere',
_maxDistance: '$maxDistance',
_maxDistanceInRadians: '$maxDistanceInRadians',
_maxDistanceInMiles: '$maxDistanceInMiles',
_maxDistanceInKilometers: '$maxDistanceInKilometers',
_within: '$within',
_box: '$box',
_geoWithin: '$geoWithin',
_polygon: '$polygon',
_centerSphere: '$centerSphere',
_geoIntersects: '$geoIntersects',
_point: '$point',
};
const transformToParse = (constraints, parentFieldName, parentConstraints) => {
GraphQL Support (#5674) * GraphQL boilerplate * Create GraphQL schema without using gql * Introducing loaders * Generic create mutation * create mutation is now working for any data type * Create mutation for each parse class - partial * Adding more data types to the class * Get parse class query * Generic get query * Generic delete mutation * Parse class delete mutation * Parse class find mutation * Generic update mutation * Parse class update mutation * Fixing initialization problems * Installing node-fetch again * Basic implementation for Pointer * Constructor tests * API tests boilerplate * _getGraphQLOptions * applyGraphQL tests * GraphQL API initial tests * applyPlayground tests * createSubscriptions tests * ParseGrapjQLSchema tests file * ParseGraphQLSchema tests * TypeValidationError * TypeValidationError * parseStringValue test * parseIntValue tests * parseBooleanValue tests * parseDateValue tests * parseValue tests * parseListValues tests * parseObjectFields tests * Default types tests * Get tests * First permission test at generic Get operation * Fixing prepare data * ApolloClient does not work well with different queries runnning in paralell with different headers * ApolloClient does not work well with different queries runnning in paralell with different headers * User 3 tests * User 3 tests * Get level permission tests * Get User specific tests * Get now support keys argument * Get now supports include argument * Get now supports read preferences * Adding tests for read preference enum type * Find basic test * Find permissions test * Find where argument test * Order, skip and limit tests * Error handler * Find now supports count * Test for FindResult type * Improving find count * Find max limit test * Find now supports keys, include and includeAll * Find now supports read preferences * Basic Create test * Generic create mutation tests * Basic update test * UpdateResult object type test * Update level permissions tests * Error handler for default mutations * Delete mutation basic test * Delete mutation level permission tests * Test for string * String test * Date test * Pointer test * Relation tests * Changing objects mutations location * Changing objects queries location * Create file mutation * Test for file fields * Test for null values * Changing parse classes operations location * Objects mutations refactoring * Class specific create object mutation now working * Update class specific mutation now working * Specific class delete mutation now working * Get class specific mutation now working * Find class specific query now working without where and sort * Find query for custom classes working with where partially * Almost all data types working for specfic class find where * Now only missing relation, geopoint, file and ACL * Additional tests with Parse classes queries and mutations * Now only missing relation, geopoint, file and ACL * Files * Fiels are now working * Excluding missing order test temporarly * Refactoring dates * Refactoring files * Default types review * Refeactoring object queries * Refactoring class scalar type * Refactoring class types * Geo queries are now working * Fixing centerSphere * Allow sort on class specific queries * Supporting bytes * ACL constraint * Temporarly removing xit tests * Fixing some tests because of schema cache * Removing session token from users * Parse.User queries and mutations * Remove test using fit * Fixing include test that was failing because of schema cache * Fixing count test for postgres. Postgres does not count with where={} (legacy problem). We should solve it later * Fix null values test for postgres. It is evaluating null as undefined (legacy problem) and we should fix is later. * Fixing schema change test that was failing because of schema cache * Add GraphQL File type parseLiteral tests * Refeactoring users * Including sign up mutation * Fix failing test * Improve default GraphQL types tests coverage * Including some tests for data types * Including additional pointer test: * Fixing some tests * more data type tests * Include Bytes and Polygon data types tests * Polygons test * Merging other tests * Fixing some postgres tests
2019-06-19 17:19:47 -07:00
if (!constraints || typeof constraints !== 'object') {
return;
}
Object.keys(constraints).forEach(fieldName => {
let fieldValue = constraints[fieldName];
/**
* If we have a key-value pair, we need to change the way the constraint is structured.
*
* Example:
* From:
* {
* "someField": {
* "_lt": {
* "_key":"foo.bar",
* "_value": 100
* },
* "_gt": {
* "_key":"foo.bar",
* "_value": 10
* }
* }
* }
*
* To:
* {
* "someField.foo.bar": {
* "$lt": 100,
* "$gt": 10
* }
* }
*/
if (
fieldValue._key &&
fieldValue._value &&
parentConstraints &&
parentFieldName
) {
delete parentConstraints[parentFieldName];
parentConstraints[`${parentFieldName}.${fieldValue._key}`] = {
...parentConstraints[`${parentFieldName}.${fieldValue._key}`],
[parseMap[fieldName]]: fieldValue._value,
};
} else if (parseMap[fieldName]) {
GraphQL Support (#5674) * GraphQL boilerplate * Create GraphQL schema without using gql * Introducing loaders * Generic create mutation * create mutation is now working for any data type * Create mutation for each parse class - partial * Adding more data types to the class * Get parse class query * Generic get query * Generic delete mutation * Parse class delete mutation * Parse class find mutation * Generic update mutation * Parse class update mutation * Fixing initialization problems * Installing node-fetch again * Basic implementation for Pointer * Constructor tests * API tests boilerplate * _getGraphQLOptions * applyGraphQL tests * GraphQL API initial tests * applyPlayground tests * createSubscriptions tests * ParseGrapjQLSchema tests file * ParseGraphQLSchema tests * TypeValidationError * TypeValidationError * parseStringValue test * parseIntValue tests * parseBooleanValue tests * parseDateValue tests * parseValue tests * parseListValues tests * parseObjectFields tests * Default types tests * Get tests * First permission test at generic Get operation * Fixing prepare data * ApolloClient does not work well with different queries runnning in paralell with different headers * ApolloClient does not work well with different queries runnning in paralell with different headers * User 3 tests * User 3 tests * Get level permission tests * Get User specific tests * Get now support keys argument * Get now supports include argument * Get now supports read preferences * Adding tests for read preference enum type * Find basic test * Find permissions test * Find where argument test * Order, skip and limit tests * Error handler * Find now supports count * Test for FindResult type * Improving find count * Find max limit test * Find now supports keys, include and includeAll * Find now supports read preferences * Basic Create test * Generic create mutation tests * Basic update test * UpdateResult object type test * Update level permissions tests * Error handler for default mutations * Delete mutation basic test * Delete mutation level permission tests * Test for string * String test * Date test * Pointer test * Relation tests * Changing objects mutations location * Changing objects queries location * Create file mutation * Test for file fields * Test for null values * Changing parse classes operations location * Objects mutations refactoring * Class specific create object mutation now working * Update class specific mutation now working * Specific class delete mutation now working * Get class specific mutation now working * Find class specific query now working without where and sort * Find query for custom classes working with where partially * Almost all data types working for specfic class find where * Now only missing relation, geopoint, file and ACL * Additional tests with Parse classes queries and mutations * Now only missing relation, geopoint, file and ACL * Files * Fiels are now working * Excluding missing order test temporarly * Refactoring dates * Refactoring files * Default types review * Refeactoring object queries * Refactoring class scalar type * Refactoring class types * Geo queries are now working * Fixing centerSphere * Allow sort on class specific queries * Supporting bytes * ACL constraint * Temporarly removing xit tests * Fixing some tests because of schema cache * Removing session token from users * Parse.User queries and mutations * Remove test using fit * Fixing include test that was failing because of schema cache * Fixing count test for postgres. Postgres does not count with where={} (legacy problem). We should solve it later * Fix null values test for postgres. It is evaluating null as undefined (legacy problem) and we should fix is later. * Fixing schema change test that was failing because of schema cache * Add GraphQL File type parseLiteral tests * Refeactoring users * Including sign up mutation * Fix failing test * Improve default GraphQL types tests coverage * Including some tests for data types * Including additional pointer test: * Fixing some tests * more data type tests * Include Bytes and Polygon data types tests * Polygons test * Merging other tests * Fixing some postgres tests
2019-06-19 17:19:47 -07:00
delete constraints[fieldName];
fieldName = parseMap[fieldName];
constraints[fieldName] = fieldValue;
}
switch (fieldName) {
case '$point':
case '$nearSphere':
if (typeof fieldValue === 'object' && !fieldValue.__type) {
fieldValue.__type = 'GeoPoint';
}
break;
case '$box':
if (
typeof fieldValue === 'object' &&
fieldValue.bottomLeft &&
fieldValue.upperRight
) {
fieldValue = [
{
__type: 'GeoPoint',
...fieldValue.bottomLeft,
},
{
__type: 'GeoPoint',
...fieldValue.upperRight,
},
];
constraints[fieldName] = fieldValue;
}
break;
case '$polygon':
if (fieldValue instanceof Array) {
fieldValue.forEach(geoPoint => {
if (typeof geoPoint === 'object' && !geoPoint.__type) {
geoPoint.__type = 'GeoPoint';
}
});
}
break;
case '$centerSphere':
if (
typeof fieldValue === 'object' &&
fieldValue.center &&
fieldValue.distance
) {
fieldValue = [
{
__type: 'GeoPoint',
...fieldValue.center,
},
fieldValue.distance,
];
constraints[fieldName] = fieldValue;
}
break;
}
if (typeof fieldValue === 'object') {
transformToParse(fieldValue, fieldName, constraints);
GraphQL Support (#5674) * GraphQL boilerplate * Create GraphQL schema without using gql * Introducing loaders * Generic create mutation * create mutation is now working for any data type * Create mutation for each parse class - partial * Adding more data types to the class * Get parse class query * Generic get query * Generic delete mutation * Parse class delete mutation * Parse class find mutation * Generic update mutation * Parse class update mutation * Fixing initialization problems * Installing node-fetch again * Basic implementation for Pointer * Constructor tests * API tests boilerplate * _getGraphQLOptions * applyGraphQL tests * GraphQL API initial tests * applyPlayground tests * createSubscriptions tests * ParseGrapjQLSchema tests file * ParseGraphQLSchema tests * TypeValidationError * TypeValidationError * parseStringValue test * parseIntValue tests * parseBooleanValue tests * parseDateValue tests * parseValue tests * parseListValues tests * parseObjectFields tests * Default types tests * Get tests * First permission test at generic Get operation * Fixing prepare data * ApolloClient does not work well with different queries runnning in paralell with different headers * ApolloClient does not work well with different queries runnning in paralell with different headers * User 3 tests * User 3 tests * Get level permission tests * Get User specific tests * Get now support keys argument * Get now supports include argument * Get now supports read preferences * Adding tests for read preference enum type * Find basic test * Find permissions test * Find where argument test * Order, skip and limit tests * Error handler * Find now supports count * Test for FindResult type * Improving find count * Find max limit test * Find now supports keys, include and includeAll * Find now supports read preferences * Basic Create test * Generic create mutation tests * Basic update test * UpdateResult object type test * Update level permissions tests * Error handler for default mutations * Delete mutation basic test * Delete mutation level permission tests * Test for string * String test * Date test * Pointer test * Relation tests * Changing objects mutations location * Changing objects queries location * Create file mutation * Test for file fields * Test for null values * Changing parse classes operations location * Objects mutations refactoring * Class specific create object mutation now working * Update class specific mutation now working * Specific class delete mutation now working * Get class specific mutation now working * Find class specific query now working without where and sort * Find query for custom classes working with where partially * Almost all data types working for specfic class find where * Now only missing relation, geopoint, file and ACL * Additional tests with Parse classes queries and mutations * Now only missing relation, geopoint, file and ACL * Files * Fiels are now working * Excluding missing order test temporarly * Refactoring dates * Refactoring files * Default types review * Refeactoring object queries * Refactoring class scalar type * Refactoring class types * Geo queries are now working * Fixing centerSphere * Allow sort on class specific queries * Supporting bytes * ACL constraint * Temporarly removing xit tests * Fixing some tests because of schema cache * Removing session token from users * Parse.User queries and mutations * Remove test using fit * Fixing include test that was failing because of schema cache * Fixing count test for postgres. Postgres does not count with where={} (legacy problem). We should solve it later * Fix null values test for postgres. It is evaluating null as undefined (legacy problem) and we should fix is later. * Fixing schema change test that was failing because of schema cache * Add GraphQL File type parseLiteral tests * Refeactoring users * Including sign up mutation * Fix failing test * Improve default GraphQL types tests coverage * Including some tests for data types * Including additional pointer test: * Fixing some tests * more data type tests * Include Bytes and Polygon data types tests * Polygons test * Merging other tests * Fixing some postgres tests
2019-06-19 17:19:47 -07:00
}
});
};
const findObjects = async (
className,
where,
order,
skip,
limit,
keys,
include,
includeAll,
readPreference,
includeReadPreference,
subqueryReadPreference,
config,
auth,
info,
selectedFields
) => {
if (!where) {
where = {};
}
transformToParse(where);
const options = {};
if (selectedFields.includes('results')) {
if (limit || limit === 0) {
options.limit = limit;
}
if (options.limit !== 0) {
if (order) {
options.order = order;
}
if (skip) {
options.skip = skip;
}
if (config.maxLimit && options.limit > config.maxLimit) {
// Silently replace the limit on the query with the max configured
options.limit = config.maxLimit;
}
if (keys) {
options.keys = keys;
}
if (includeAll === true) {
options.includeAll = includeAll;
}
if (!options.includeAll && include) {
options.include = include;
}
if ((options.includeAll || options.include) && includeReadPreference) {
options.includeReadPreference = includeReadPreference;
}
}
} else {
options.limit = 0;
}
if (selectedFields.includes('count')) {
options.count = true;
}
if (readPreference) {
options.readPreference = readPreference;
}
if (Object.keys(where).length > 0 && subqueryReadPreference) {
options.subqueryReadPreference = subqueryReadPreference;
}
return await rest.find(
config,
auth,
className,
where,
options,
info.clientSDK
);
};
const load = parseGraphQLSchema => {
parseGraphQLSchema.graphQLObjectsQueries.get = {
description:
'The get query can be used to get an object of a certain class by its objectId.',
args: {
className: defaultGraphQLTypes.CLASS_NAME_ATT,
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
keys: defaultGraphQLTypes.KEYS_ATT,
include: defaultGraphQLTypes.INCLUDE_ATT,
readPreference: defaultGraphQLTypes.READ_PREFERENCE_ATT,
includeReadPreference: defaultGraphQLTypes.INCLUDE_READ_PREFERENCE_ATT,
},
type: new GraphQLNonNull(defaultGraphQLTypes.OBJECT),
async resolve(_source, args, context) {
try {
const {
className,
objectId,
keys,
include,
readPreference,
includeReadPreference,
} = args;
const { config, auth, info } = context;
return await getObject(
className,
objectId,
keys,
include,
readPreference,
includeReadPreference,
config,
auth,
info
);
} catch (e) {
parseGraphQLSchema.handleError(e);
}
},
};
parseGraphQLSchema.graphQLObjectsQueries.find = {
description:
'The find query can be used to find objects of a certain class.',
args: {
className: defaultGraphQLTypes.CLASS_NAME_ATT,
where: defaultGraphQLTypes.WHERE_ATT,
order: {
description:
'This is the order in which the objects should be returned',
type: GraphQLString,
},
skip: defaultGraphQLTypes.SKIP_ATT,
limit: defaultGraphQLTypes.LIMIT_ATT,
keys: defaultGraphQLTypes.KEYS_ATT,
include: defaultGraphQLTypes.INCLUDE_ATT,
includeAll: {
description: 'All pointers will be returned',
type: GraphQLBoolean,
},
readPreference: defaultGraphQLTypes.READ_PREFERENCE_ATT,
includeReadPreference: defaultGraphQLTypes.INCLUDE_READ_PREFERENCE_ATT,
subqueryReadPreference: defaultGraphQLTypes.SUBQUERY_READ_PREFERENCE_ATT,
},
type: new GraphQLNonNull(defaultGraphQLTypes.FIND_RESULT),
async resolve(_source, args, context, queryInfo) {
try {
const {
className,
where,
order,
skip,
limit,
keys,
include,
includeAll,
readPreference,
includeReadPreference,
subqueryReadPreference,
} = args;
const { config, auth, info } = context;
const selectedFields = getFieldNames(queryInfo);
return await findObjects(
className,
where,
order,
skip,
limit,
keys,
include,
includeAll,
readPreference,
includeReadPreference,
subqueryReadPreference,
config,
auth,
info,
selectedFields
);
} catch (e) {
parseGraphQLSchema.handleError(e);
}
},
};
const objectsQuery = new GraphQLObjectType({
name: 'ObjectsQuery',
description: 'ObjectsQuery is the top level type for objects queries.',
fields: parseGraphQLSchema.graphQLObjectsQueries,
});
parseGraphQLSchema.graphQLTypes.push(objectsQuery);
parseGraphQLSchema.graphQLQueries.objects = {
description: 'This is the top level for objects queries.',
type: objectsQuery,
resolve: () => new Object(),
};
};
export { getObject, findObjects, load };