2019-06-19 17:19:47 -07:00
|
|
|
import { GraphQLNonNull } from 'graphql';
|
2019-12-01 21:43:08 -08:00
|
|
|
import { fromGlobalId } from 'graphql-relay';
|
2019-06-19 17:19:47 -07:00
|
|
|
import getFieldNames from 'graphql-list-fields';
|
2019-08-15 23:23:41 +02:00
|
|
|
import pluralize from 'pluralize';
|
2019-06-19 17:19:47 -07:00
|
|
|
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
2019-09-01 22:11:03 -07:00
|
|
|
import * as objectsQueries from '../helpers/objectsQueries';
|
2019-07-25 20:46:25 +01:00
|
|
|
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
|
2019-08-15 23:23:41 +02:00
|
|
|
import { transformClassNameToGraphQL } from '../transformers/className';
|
2019-08-14 21:25:28 +02:00
|
|
|
import { extractKeysAndInclude } from '../parseGraphQLUtils';
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
const getParseClassQueryConfig = function(
|
|
|
|
|
parseClassConfig: ?ParseGraphQLClassConfig
|
|
|
|
|
) {
|
|
|
|
|
return (parseClassConfig && parseClassConfig.query) || {};
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-23 09:40:04 +01:00
|
|
|
const getQuery = async (
|
|
|
|
|
parseClass,
|
|
|
|
|
_source,
|
|
|
|
|
args,
|
|
|
|
|
context,
|
|
|
|
|
queryInfo,
|
|
|
|
|
parseClasses
|
|
|
|
|
) => {
|
2019-12-01 21:43:08 -08:00
|
|
|
let { id } = args;
|
|
|
|
|
const { options } = args;
|
2019-09-12 13:43:49 -07:00
|
|
|
const { readPreference, includeReadPreference } = options || {};
|
2019-08-14 21:25:28 +02:00
|
|
|
const { config, auth, info } = context;
|
|
|
|
|
const selectedFields = getFieldNames(queryInfo);
|
|
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
const globalIdObject = fromGlobalId(id);
|
|
|
|
|
|
2020-02-22 00:12:49 +01:00
|
|
|
if (globalIdObject.type === parseClass.className) {
|
2019-12-01 21:43:08 -08:00
|
|
|
id = globalIdObject.id;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 21:25:28 +02:00
|
|
|
const { keys, include } = extractKeysAndInclude(selectedFields);
|
|
|
|
|
|
|
|
|
|
return await objectsQueries.getObject(
|
2020-02-22 00:12:49 +01:00
|
|
|
parseClass.className,
|
2019-08-30 20:23:46 -03:00
|
|
|
id,
|
2019-08-14 21:25:28 +02:00
|
|
|
keys,
|
|
|
|
|
include,
|
|
|
|
|
readPreference,
|
|
|
|
|
includeReadPreference,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
2020-02-22 00:12:49 +01:00
|
|
|
info,
|
2020-03-23 09:40:04 +01:00
|
|
|
parseClasses
|
2019-08-14 21:25:28 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
const load = function(
|
|
|
|
|
parseGraphQLSchema,
|
|
|
|
|
parseClass,
|
|
|
|
|
parseClassConfig: ?ParseGraphQLClassConfig
|
|
|
|
|
) {
|
2019-08-15 23:23:41 +02:00
|
|
|
const className = parseClass.className;
|
|
|
|
|
const graphQLClassName = transformClassNameToGraphQL(className);
|
2019-07-25 20:46:25 +01:00
|
|
|
const {
|
|
|
|
|
get: isGetEnabled = true,
|
|
|
|
|
find: isFindEnabled = true,
|
2019-12-04 03:14:48 +03:00
|
|
|
getAlias: getAlias = '',
|
|
|
|
|
findAlias: findAlias = '',
|
2019-07-25 20:46:25 +01:00
|
|
|
} = getParseClassQueryConfig(parseClassConfig);
|
2019-06-19 17:19:47 -07:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
classGraphQLOutputType,
|
|
|
|
|
classGraphQLFindArgs,
|
|
|
|
|
classGraphQLFindResultType,
|
|
|
|
|
} = parseGraphQLSchema.parseClassTypes[className];
|
|
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
if (isGetEnabled) {
|
2019-12-04 03:14:48 +03:00
|
|
|
const lowerCaseClassName =
|
2019-08-15 23:23:41 +02:00
|
|
|
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1);
|
2019-12-04 03:14:48 +03:00
|
|
|
|
|
|
|
|
const getGraphQLQueryName = getAlias || lowerCaseClassName;
|
|
|
|
|
|
2019-08-17 11:02:19 -07:00
|
|
|
parseGraphQLSchema.addGraphQLQuery(getGraphQLQueryName, {
|
2019-08-15 23:23:41 +02:00
|
|
|
description: `The ${getGraphQLQueryName} query can be used to get an object of the ${graphQLClassName} class by its id.`,
|
2019-07-25 20:46:25 +01:00
|
|
|
args: {
|
2019-12-01 21:43:08 -08:00
|
|
|
id: defaultGraphQLTypes.GLOBAL_OR_OBJECT_ID_ATT,
|
2019-09-12 13:43:49 -07:00
|
|
|
options: defaultGraphQLTypes.READ_OPTIONS_ATT,
|
2019-07-25 20:46:25 +01:00
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
type: new GraphQLNonNull(
|
|
|
|
|
classGraphQLOutputType || defaultGraphQLTypes.OBJECT
|
|
|
|
|
),
|
2019-07-25 20:46:25 +01:00
|
|
|
async resolve(_source, args, context, queryInfo) {
|
|
|
|
|
try {
|
2020-03-23 09:40:04 +01:00
|
|
|
return await getQuery(
|
|
|
|
|
parseClass,
|
|
|
|
|
_source,
|
|
|
|
|
args,
|
|
|
|
|
context,
|
|
|
|
|
queryInfo,
|
|
|
|
|
parseGraphQLSchema.parseClasses
|
|
|
|
|
);
|
2019-07-25 20:46:25 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
});
|
2019-07-25 20:46:25 +01:00
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
if (isFindEnabled) {
|
2019-12-04 03:14:48 +03:00
|
|
|
const lowerCaseClassName =
|
|
|
|
|
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1);
|
|
|
|
|
|
|
|
|
|
const findGraphQLQueryName = findAlias || pluralize(lowerCaseClassName);
|
|
|
|
|
|
2019-08-17 11:02:19 -07:00
|
|
|
parseGraphQLSchema.addGraphQLQuery(findGraphQLQueryName, {
|
2019-08-15 23:23:41 +02:00
|
|
|
description: `The ${findGraphQLQueryName} query can be used to find objects of the ${graphQLClassName} class.`,
|
2019-07-25 20:46:25 +01:00
|
|
|
args: classGraphQLFindArgs,
|
2019-08-15 23:23:41 +02:00
|
|
|
type: new GraphQLNonNull(
|
2019-12-01 21:43:08 -08:00
|
|
|
classGraphQLFindResultType || defaultGraphQLTypes.OBJECT
|
2019-08-15 23:23:41 +02:00
|
|
|
),
|
2019-07-25 20:46:25 +01:00
|
|
|
async resolve(_source, args, context, queryInfo) {
|
|
|
|
|
try {
|
2019-12-01 21:43:08 -08:00
|
|
|
const {
|
|
|
|
|
where,
|
|
|
|
|
order,
|
|
|
|
|
skip,
|
|
|
|
|
first,
|
|
|
|
|
after,
|
|
|
|
|
last,
|
|
|
|
|
before,
|
|
|
|
|
options,
|
|
|
|
|
} = args;
|
2019-07-25 20:46:25 +01:00
|
|
|
const {
|
|
|
|
|
readPreference,
|
|
|
|
|
includeReadPreference,
|
|
|
|
|
subqueryReadPreference,
|
2019-09-12 13:43:49 -07:00
|
|
|
} = options || {};
|
2019-07-25 20:46:25 +01:00
|
|
|
const { config, auth, info } = context;
|
|
|
|
|
const selectedFields = getFieldNames(queryInfo);
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-08-14 21:25:28 +02:00
|
|
|
const { keys, include } = extractKeysAndInclude(
|
2019-07-25 20:46:25 +01:00
|
|
|
selectedFields
|
2019-12-01 21:43:08 -08:00
|
|
|
.filter(field => field.startsWith('edges.node.'))
|
|
|
|
|
.map(field => field.replace('edges.node.', ''))
|
2019-07-25 20:46:25 +01:00
|
|
|
);
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
return await objectsQueries.findObjects(
|
|
|
|
|
className,
|
|
|
|
|
where,
|
2020-03-23 09:02:52 +01:00
|
|
|
order,
|
2019-07-25 20:46:25 +01:00
|
|
|
skip,
|
2019-12-01 21:43:08 -08:00
|
|
|
first,
|
|
|
|
|
after,
|
|
|
|
|
last,
|
|
|
|
|
before,
|
2019-07-25 20:46:25 +01:00
|
|
|
keys,
|
|
|
|
|
include,
|
|
|
|
|
false,
|
|
|
|
|
readPreference,
|
|
|
|
|
includeReadPreference,
|
|
|
|
|
subqueryReadPreference,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info,
|
2019-12-01 21:43:08 -08:00
|
|
|
selectedFields,
|
2019-12-05 19:14:16 +01:00
|
|
|
parseGraphQLSchema.parseClasses
|
2019-07-25 20:46:25 +01:00
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-08-15 23:23:41 +02:00
|
|
|
});
|
2019-07-25 20:46:25 +01:00
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { load };
|