-
Notifications
You must be signed in to change notification settings - Fork 71
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
Gql preprocess #20
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8f9fc88
WIP: Adding preProcess support to V2 transformer
marcvberg c2961fa
Resolve merge conflict: two added tests
marcvberg 9bcb494
Feature: Add preprocessing to belongsTo plugin
marcvberg c1bea31
Feat: adding preprocessing support manyToMany
marcvberg 6df2b71
Remove DataAPI, we will use a different single source read
marcvberg bcb29b9
Merge branch 'main' into gql-preprocess
marcvberg fd1ada7
Tests: add manyToMany preprocess tests
marcvberg d64c9c2
Fix: Add test and corrct field type renaming manyToMany
marcvberg 201eacc
Fix: make schema mutate more functional
marcvberg 6ed42b9
Fix: remove use of let
marcvberg 09255fa
Docs: update docstring for preprocessing with more detail
marcvberg 0e017e2
Merge branch 'main' into gql-preprocess
marcvberg 2646fc1
Cut down on nesting in pre-processing
marcvberg b623e0a
Add some semicolons in that should've been there
marcvberg 37fe785
Merge branch 'main' into gql-preprocess
marcvberg 544eb3e
Fix extra package dependencies from merge
marcvberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { PrimaryKeyTransformer } from '@aws-amplify/graphql-index-transformer'; | ||
import { ModelTransformer } from '@aws-amplify/graphql-model-transformer'; | ||
import { ConflictHandlerType, GraphQLTransform, validateModelSchema } from '@aws-amplify/graphql-transformer-core'; | ||
import { Kind, parse } from 'graphql'; | ||
import { DocumentNode, Kind, parse } from 'graphql'; | ||
import { HasManyTransformer, HasOneTransformer } from '..'; | ||
|
||
test('fails if @hasOne was used on an object that is not a model type', () => { | ||
|
@@ -466,3 +466,123 @@ test('recursive @hasOne relationships are supported if DataStore is enabled', () | |
const schema = parse(out.schema); | ||
validateModelSchema(schema); | ||
}); | ||
|
||
describe('Pre Processing Has One Tests', () => { | ||
let transformer: GraphQLTransform; | ||
const hasGeneratedField = (doc: DocumentNode, objectType: string, fieldName: string): boolean => { | ||
let hasField = false; | ||
doc?.definitions?.forEach(def => { | ||
if ((def.kind === 'ObjectTypeDefinition' || def.kind === 'ObjectTypeExtension') && def.name.value === objectType) { | ||
def?.fields?.forEach(field => { | ||
if (field.name.value === fieldName) { | ||
hasField = true; | ||
} | ||
}); | ||
} | ||
}); | ||
return hasField; | ||
}; | ||
|
||
const hasGeneratedFieldArgument = (doc: DocumentNode, objectType: string, fieldName: string, generatedFieldName: string): boolean => { | ||
let hasFieldArgument = false; | ||
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; | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
Comment on lines
+488
to
+506
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Is it possible to reduce the nesting here using |
||
}); | ||
} | ||
}); | ||
return hasFieldArgument; | ||
}; | ||
|
||
beforeEach(() => { | ||
transformer = new GraphQLTransform({ | ||
transformers: [new ModelTransformer(), new HasOneTransformer()], | ||
}); | ||
}); | ||
|
||
test('Should generate connecting field when one is not provided', () => { | ||
const schema = ` | ||
type Blog @model { | ||
id: ID! | ||
blogName: BlogName @hasOne | ||
} | ||
|
||
type BlogName @model { | ||
id: ID! | ||
} | ||
`; | ||
|
||
const updatedSchemaDoc = transformer.preProcessSchema(parse(schema)); | ||
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeTruthy(); | ||
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeTruthy(); | ||
}); | ||
|
||
test('Should generate connecting field when empty fields are provided', () => { | ||
const schema = ` | ||
type Blog @model { | ||
id: ID! | ||
blogName: BlogName @hasOne(fields: []) | ||
} | ||
|
||
type BlogName @model { | ||
id: ID! | ||
} | ||
`; | ||
|
||
const updatedSchemaDoc = transformer.preProcessSchema(parse(schema)); | ||
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeTruthy(); | ||
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeTruthy(); | ||
}); | ||
|
||
test('Should not generate connecting field when one is provided', () => { | ||
const schema = ` | ||
type Blog @model { | ||
id: ID! | ||
connectionField: ID | ||
blogName: BlogName @hasOne(fields: "connectionField") | ||
} | ||
|
||
type BlogName @model { | ||
id: ID! | ||
} | ||
`; | ||
|
||
const updatedSchemaDoc = transformer.preProcessSchema(parse(schema)); | ||
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeFalsy(); | ||
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeFalsy(); | ||
}); | ||
|
||
test('Should not generate connecting field when a list one is provided', () => { | ||
const schema = ` | ||
type Blog @model { | ||
id: ID! | ||
connectionField: ID | ||
blogName: BlogName @hasOne(fields: ["connectionField"]) | ||
} | ||
|
||
type BlogName @model { | ||
id: ID! | ||
} | ||
`; | ||
|
||
const updatedSchemaDoc = transformer.preProcessSchema(parse(schema)); | ||
expect(hasGeneratedField(updatedSchemaDoc, 'Blog', 'blogBlogNameId')).toBeFalsy(); | ||
expect(hasGeneratedFieldArgument(updatedSchemaDoc, 'Blog', 'blogName', 'blogBlogNameId')).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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