2019-06-19 17:19:47 -07:00
|
|
|
import { GraphQLNonNull } from 'graphql';
|
|
|
|
|
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';
|
|
|
|
|
import * as objectsQueries from './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) || {};
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-14 21:25:28 +02:00
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
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,
|
|
|
|
|
} = 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-08-15 23:23:41 +02:00
|
|
|
const getGraphQLQueryName =
|
|
|
|
|
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1);
|
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: {
|
|
|
|
|
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
|
|
|
|
|
readPreference: defaultGraphQLTypes.READ_PREFERENCE_ATT,
|
|
|
|
|
includeReadPreference: defaultGraphQLTypes.INCLUDE_READ_PREFERENCE_ATT,
|
|
|
|
|
},
|
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 {
|
2019-08-14 21:25:28 +02:00
|
|
|
return await getQuery(className, _source, args, context, queryInfo);
|
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-08-15 23:23:41 +02:00
|
|
|
const findGraphQLQueryName = pluralize(
|
|
|
|
|
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1)
|
|
|
|
|
);
|
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(
|
|
|
|
|
classGraphQLFindResultType || defaultGraphQLTypes.FIND_RESULT
|
|
|
|
|
),
|
2019-07-25 20:46:25 +01:00
|
|
|
async resolve(_source, args, context, queryInfo) {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
where,
|
|
|
|
|
order,
|
|
|
|
|
skip,
|
|
|
|
|
limit,
|
|
|
|
|
readPreference,
|
|
|
|
|
includeReadPreference,
|
|
|
|
|
subqueryReadPreference,
|
|
|
|
|
} = args;
|
|
|
|
|
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
|
|
|
|
|
.filter(field => field.includes('.'))
|
|
|
|
|
.map(field => field.slice(field.indexOf('.') + 1))
|
|
|
|
|
);
|
|
|
|
|
const parseOrder = order && order.join(',');
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-07-25 20:46:25 +01:00
|
|
|
return await objectsQueries.findObjects(
|
|
|
|
|
className,
|
|
|
|
|
where,
|
|
|
|
|
parseOrder,
|
|
|
|
|
skip,
|
|
|
|
|
limit,
|
|
|
|
|
keys,
|
|
|
|
|
include,
|
|
|
|
|
false,
|
|
|
|
|
readPreference,
|
|
|
|
|
includeReadPreference,
|
|
|
|
|
subqueryReadPreference,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info,
|
|
|
|
|
selectedFields.map(field => field.split('.', 1)[0])
|
|
|
|
|
);
|
|
|
|
|
} 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 };
|