Skip to content

Commit

Permalink
fix: make sure data from where clause is complete (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekhmet authored Sep 27, 2023
1 parent d10d6ff commit 34f1494
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
GraphQLResolveInfo,
GraphQLScalarType,
isListType,
isObjectType,
isScalarType
} from 'graphql';
import {
Expand Down Expand Up @@ -70,11 +71,21 @@ export async function queryMulti(parent, args, context: ResolverContext, info) {
query = query.whereIn(`${prefix}.${w[0].slice(0, -3)}`, w[1]);
} else if (typeof w[1] === 'object' && w[0].endsWith('_')) {
const fieldName = w[0].slice(0, -1);
const nestedReturnType = returnType.getFields()[fieldName].type as GraphQLObjectType;
const nestedReturnType = getNonNullType(
returnType.getFields()[fieldName].type as GraphQLObjectType
);
const nestedTableName = getTableName(nestedReturnType.name.toLowerCase());

const fields = Object.values(nestedReturnType.getFields())
.filter(field => isScalarType(getNonNullType(field.type)))
.filter(field => {
const baseType = getNonNullType(field.type);

return (
isScalarType(baseType) ||
isObjectType(baseType) ||
(isListType(baseType) && isScalarType(baseType.ofType))
);
})
.map(field => field.name);

nestedEntitiesMappings[fieldName] = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const generateQueryForEntity = (entity: GraphQLObjectType): string => {
);
};

export const getNonNullType = (type: GraphQLOutputType): GraphQLOutputType => {
export const getNonNullType = <T>(type: T): T => {
if (type instanceof GraphQLNonNull) {
return type.ofType;
}
Expand Down

0 comments on commit 34f1494

Please sign in to comment.