Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gql preprocess #20

Merged
merged 16 commits into from
Jun 13, 2022
Merged

Gql preprocess #20

merged 16 commits into from
Jun 13, 2022

Conversation

marcvberg
Copy link
Contributor

Description of changes

This begins to add support for transformer plugins to modify schemas outside the transformation process. Enables codegen to hook into these changes directly and use them

This is only for the purpose of adding support in right now and does not hook into any active workflows

Issue #, if available

Description of how you validated changes

Unit testing

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@lgtm-com
Copy link

lgtm-com bot commented May 6, 2022

This pull request introduces 6 alerts when merging c2961fa into 12e0be7 - view on LGTM.com

new alerts:

  • 6 for Unused variable, import, function or class

@marcvberg marcvberg marked this pull request as ready for review May 25, 2022 14:41
@marcvberg marcvberg requested a review from a team May 25, 2022 14:41
@marcvberg marcvberg requested a review from a team as a code owner May 25, 2022 14:41
@lgtm-com
Copy link

lgtm-com bot commented May 25, 2022

This pull request introduces 2 alerts when merging bcb29b9 into a9bc64c - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented May 25, 2022

This pull request introduces 2 alerts when merging fd1ada7 into 0b795c9 - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented May 26, 2022

This pull request introduces 3 alerts when merging d64c9c2 into b1ceecd - view on LGTM.com

new alerts:

  • 3 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented May 27, 2022

This pull request introduces 4 alerts when merging 201eacc into 5a1b0b6 - view on LGTM.com

new alerts:

  • 4 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented May 27, 2022

This pull request introduces 3 alerts when merging 6ed42b9 into 5a1b0b6 - view on LGTM.com

new alerts:

  • 3 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented May 27, 2022

This pull request introduces 3 alerts when merging 09255fa into 5a1b0b6 - view on LGTM.com

new alerts:

  • 3 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented Jun 2, 2022

This pull request introduces 3 alerts when merging 0e017e2 into 390bfd0 - view on LGTM.com

new alerts:

  • 3 for Unused variable, import, function or class

alharris-at
alharris-at previously approved these changes Jun 2, 2022
Comment on lines +488 to +506
doc?.definitions?.forEach(def => {
if ((def.kind === 'ObjectTypeDefinition' || def.kind === 'ObjectTypeExtension') && def.name.value === objectType) {
def?.fields?.forEach(field => {
if (field.name.value === fieldName) {
field?.directives?.forEach(dir => {
if (dir.name.value === 'hasOne') {
dir?.arguments?.forEach(arg => {
if (arg.name.value === 'fields') {
if (arg.value.kind === 'ListValue' && arg.value.values[0].kind === 'StringValue' && arg.value.values[0].value === generatedFieldName) {
hasFieldArgument = true;
}
else if (arg.value.kind === 'StringValue' && arg.value.value === generatedFieldName) {
hasFieldArgument = true;
}
}
});
}
});
}
Copy link
Contributor

@sundersc sundersc Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is it possible to reduce the nesting here using filter and some?
May be like doc.definitions.filter(def => (def.kind === '' and so on) && def.fields.some(f => f.name.value === fieldName)).

Comment on lines 77 to 106
if (def.kind === 'ObjectTypeExtension' || def.kind === 'ObjectTypeDefinition') {
def?.fields?.forEach(field => {
field?.directives?.forEach(dir => {
if (dir.name.value === directiveName) {
const relatedType = objectTypeMap.get(getBaseType(field.type));
if (relatedType) { // Validation is done in a different segment of the life cycle
const relationTypeField = relatedType?.fields?.find(relatedField => {
if (getBaseType(relatedField.type) === def.name.value && relatedField?.directives?.some(
relatedDir => relatedDir.name.value === 'hasOne' || relatedDir.name.value === 'hasMany',
)) {
return true;
}
return false;
});
const relationTypeName = relationTypeField?.directives?.find(relationDir => relationDir.name.value === 'hasOne' || relationDir.name.value === 'hasMany')?.name?.value;
if (relationTypeName === 'hasOne') {
const connectionAttributeName = getConnectionAttributeName(def.name.value, field.name.value);
if (!def?.fields?.some(defField => defField.name.value === connectionAttributeName)) {
def?.fields?.push(
makeField(
connectionAttributeName, [], isNonNullType(field.type) ?
makeNonNullType(makeNamedType('ID')) : makeNamedType('ID'), [],
) as WritableDraft<FieldDefinitionNode>,
);
}
}
}
}
});
});
Copy link
Contributor

@sundersc sundersc Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the previous comment. Please see if nesting can be reduced using filter and some.

Comment on lines 73 to 86
draftDoc?.definitions?.forEach(def => {
if (def.kind === 'ObjectTypeExtension' || def.kind === 'ObjectTypeDefinition') {
def?.fields?.forEach(field => {
field?.directives?.forEach(dir => {
if (dir.name.value === directiveName) {
const baseFieldType = getBaseType(field.type);
const connectionAttributeName = getConnectionAttributeName(def.name.value, field.name.value);
const newField = makeField(connectionAttributeName, [], isNonNullType(field.type) ? makeNonNullType(makeNamedType('ID')) : makeNamedType('ID'), []);
connectingFieldsMap.set(baseFieldType, newField as WritableDraft<FieldDefinitionNode>);
}
});
});
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use filter instead of foreach and if?

Comment on lines 77 to 108
draftDoc.definitions.forEach(def => {
if (def.kind === 'ObjectTypeDefinition' || def.kind === 'ObjectTypeExtension') {
def?.fields?.forEach(field => {
field?.directives?.forEach(dir => {
if (dir.name.value === directiveName) {
const connectionAttributeName = getConnectionAttributeName(def.name.value, field.name.value);
let hasFieldsDefined = false;
let removalIndex = -1;
dir?.arguments?.forEach((arg, idx) => {
if (arg.name.value === 'fields') {
if ((arg.value.kind === 'StringValue' && arg.value.value) || (arg.value.kind === 'ListValue' && arg.value.values && arg.value.values.length > 0)) {
hasFieldsDefined = true;
} else {
removalIndex = idx;
}
}
});
if (removalIndex !== -1) {
dir?.arguments?.splice(removalIndex, 1);
}
if (!hasFieldsDefined) {
// eslint-disable-next-line no-param-reassign
dir.arguments = [makeArgument('fields', makeValueNode(connectionAttributeName)) as WritableDraft<ArgumentNode>];
def?.fields?.push(
makeField(
connectionAttributeName, [], isNonNullType(field.type) ?
makeNonNullType(makeNamedType('ID')) : makeNamedType('ID'), [],
) as WritableDraft<FieldDefinitionNode>,
);
}
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the previous comment. Please see if we can use filter to simply the logic.

Comment on lines 115 to 140
draftDoc?.definitions?.forEach(def => {
if (def.kind === 'ObjectTypeDefinition' || def.kind === 'ObjectTypeExtension') {
def?.fields?.forEach(field => {
field?.directives?.forEach(dir => {
if (dir.name.value === directiveName) {
const relationArg = dir?.arguments?.find(arg => arg.name.value === 'relationName');
if (relationArg?.value?.kind === 'StringValue') {
const relationName = relationArg.value.value;
const manyToManyContext: ManyToManyPreProcessContext = {
model: def,
field: field,
directive: dir,
modelAuthDirectives: def?.directives?.filter(authDir => authDir.name.value === 'auth') ?? [],
fieldAuthDirectives: def?.directives?.filter(authDir => authDir.name.value === 'auth') ?? [],
relationName: relationName,
};
if (!manyToManyMap.has(relationName)) {
manyToManyMap.set(relationName, []);
}
manyToManyMap.get(relationName)?.push(manyToManyContext);
}
}
});
});
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider use of filter.

Copy link
Contributor

@sundersc sundersc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left few comments to check whether the nested foreach and if statements can be avoided using filter method. Otherwise the changes look good.

@lgtm-com
Copy link

lgtm-com bot commented Jun 10, 2022

This pull request introduces 1 alert when merging 37fe785 into eee67ed - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

sundersc
sundersc previously approved these changes Jun 11, 2022
Copy link
Contributor

@sundersc sundersc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -36,7 +36,10 @@
"@aws-cdk/core": "~1.124.0",
"graphql": "^14.5.8",
"graphql-mapping-template": "4.20.5",
"graphql-transformer-common": "4.23.2"
"graphql-transformer-common": "4.23.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is a dup of 41

@marcvberg marcvberg merged commit 15860fc into aws-amplify:main Jun 13, 2022
@lgtm-com
Copy link

lgtm-com bot commented Jun 13, 2022

This pull request introduces 1 alert when merging 544eb3e into 5a8c75a - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants