-
Notifications
You must be signed in to change notification settings - Fork 70
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
Gql preprocess #20
Conversation
This pull request introduces 6 alerts when merging c2961fa into 12e0be7 - view on LGTM.com new alerts:
|
This pull request introduces 2 alerts when merging bcb29b9 into a9bc64c - view on LGTM.com new alerts:
|
This pull request introduces 2 alerts when merging fd1ada7 into 0b795c9 - view on LGTM.com new alerts:
|
packages/amplify-graphql-transformer-core/src/transformation/transform.ts
Outdated
Show resolved
Hide resolved
packages/amplify-graphql-transformer-core/src/transformation/transform.ts
Outdated
Show resolved
Hide resolved
packages/amplify-graphql-transformer-core/src/transformation/transform.ts
Outdated
Show resolved
Hide resolved
packages/amplify-graphql-transformer-core/src/transformation/transform.ts
Outdated
Show resolved
Hide resolved
This pull request introduces 3 alerts when merging d64c9c2 into b1ceecd - view on LGTM.com new alerts:
|
This pull request introduces 4 alerts when merging 201eacc into 5a1b0b6 - view on LGTM.com new alerts:
|
This pull request introduces 3 alerts when merging 6ed42b9 into 5a1b0b6 - view on LGTM.com new alerts:
|
This pull request introduces 3 alerts when merging 09255fa into 5a1b0b6 - view on LGTM.com new alerts:
|
This pull request introduces 3 alerts when merging 0e017e2 into 390bfd0 - view on LGTM.com new alerts:
|
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; | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
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))
.
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>, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); |
There was a problem hiding this comment.
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.
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>); | ||
} | ||
}); | ||
}); | ||
} | ||
}); |
There was a problem hiding this comment.
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?
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>, | ||
); | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
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.
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); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider use of filter.
There was a problem hiding this 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.
This pull request introduces 1 alert when merging 37fe785 into eee67ed - view on LGTM.com new alerts:
|
There was a problem hiding this 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", |
There was a problem hiding this comment.
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
This pull request introduces 1 alert when merging 544eb3e into 5a8c75a - view on LGTM.com new alerts:
|
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
yarn test
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.