fix remote schema permissions with interfaces

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6417
GitOrigin-RevId: c3df729df040ea11ec61cf75536c8dec6b7cdbed
This commit is contained in:
Daniele Cammareri 2022-10-18 23:20:14 +02:00 committed by hasura-bot
parent 666f34d564
commit e13ff5babe

View File

@ -25,6 +25,7 @@ import {
StringValueNode,
BooleanValueNode,
EnumValueNode,
GraphQLInterfaceType,
} from 'graphql';
import {
isJsonString,
@ -233,6 +234,7 @@ export const getType = (
const inputObjectTypes: RemoteSchemaFields[] = [];
const objectTypes: RemoteSchemaFields[] = [];
const unionTypes: RemoteSchemaFields[] = [];
const interfaceTypes: RemoteSchemaFields[] = [];
Object.entries(introspectionSchemaFields).forEach(([key, value]: any) => {
if (
@ -241,7 +243,8 @@ export const getType = (
value instanceof GraphQLInputObjectType ||
value instanceof GraphQLEnumType ||
value instanceof GraphQLScalarType ||
value instanceof GraphQLUnionType
value instanceof GraphQLUnionType ||
value instanceof GraphQLInterfaceType
)
)
return;
@ -295,6 +298,13 @@ export const getType = (
scalarTypes.push(type);
} else if (value instanceof GraphQLObjectType) {
type.name = `type ${name}`;
if (value.getInterfaces().length) {
const implementsString = value
.getInterfaces()
.map((i: any) => i.name)
.join('& ');
type.name = `type ${name} implements ${implementsString}`;
}
} else if (value instanceof GraphQLInputObjectType) {
type.name = `input ${name}`;
}
@ -389,6 +399,43 @@ export const getType = (
type.children = childArray;
unionTypes.push(type);
}
if (value instanceof GraphQLInterfaceType) {
let isFieldPresent = true;
let permissionsFieldVal: GraphQLFieldMap<any, any, any> = {};
// Check if the type is present in the permission schema coming from user.
if (permissionsSchema !== null && permissionsSchemaFields !== null) {
if (key in permissionsSchemaFields) {
permissionsFieldVal = permissionsSchemaFields[key].getFields();
} else {
isFieldPresent = false;
}
}
type.name = `interface ${name}`;
const childArray: CustomFieldType[] = [];
const fieldVal = value.getFields();
Object.entries(fieldVal).forEach(([k, v]) => {
let checked = false;
if (
permissionsSchema !== null &&
isFieldPresent &&
k in permissionsFieldVal
) {
checked = true;
}
const field: CustomFieldType = {
name: v.name,
checked,
return: v.type.toString(),
};
childArray.push(field);
});
type.children = childArray;
interfaceTypes.push(type);
}
});
return [
...objectTypes,
@ -396,6 +443,7 @@ export const getType = (
...unionTypes,
...enumTypes,
...scalarTypes,
...interfaceTypes,
];
};