-
Notifications
You must be signed in to change notification settings - Fork 77
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
fix: enable IAM auth for custom types #2961
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ import { ObjectTypeDefinitionNode, FieldDefinitionNode, DirectiveNode, NamedType | |||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||
blankObjectExtension, | ||||||||||||||||||||||||||||||||||||||||||
extendFieldWithDirectives, | ||||||||||||||||||||||||||||||||||||||||||
extendObjectWithDirectives, | ||||||||||||||||||||||||||||||||||||||||||
extensionWithDirectives, | ||||||||||||||||||||||||||||||||||||||||||
graphqlName, | ||||||||||||||||||||||||||||||||||||||||||
isListType, | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -213,7 +214,7 @@ export const addDirectivesToField = ( | |||||||||||||||||||||||||||||||||||||||||
typeName: string, | ||||||||||||||||||||||||||||||||||||||||||
fieldName: string, | ||||||||||||||||||||||||||||||||||||||||||
directives: Array<DirectiveNode>, | ||||||||||||||||||||||||||||||||||||||||||
) => { | ||||||||||||||||||||||||||||||||||||||||||
): void => { | ||||||||||||||||||||||||||||||||||||||||||
const type = ctx.output.getType(typeName) as ObjectTypeDefinitionNode; | ||||||||||||||||||||||||||||||||||||||||||
if (type) { | ||||||||||||||||||||||||||||||||||||||||||
const field = type.fields?.find((f) => f.name.value === fieldName); | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -230,6 +231,17 @@ export const addDirectivesToField = ( | |||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
export const addDirectivesToObject = ( | ||||||||||||||||||||||||||||||||||||||||||
ctx: TransformerTransformSchemaStepContextProvider, | ||||||||||||||||||||||||||||||||||||||||||
typeName: string, | ||||||||||||||||||||||||||||||||||||||||||
directives: Array<DirectiveNode>, | ||||||||||||||||||||||||||||||||||||||||||
): void => { | ||||||||||||||||||||||||||||||||||||||||||
const type = ctx.output.getType(typeName) as ObjectTypeDefinitionNode; | ||||||||||||||||||||||||||||||||||||||||||
if (type) { | ||||||||||||||||||||||||||||||||||||||||||
ctx.output.putType(extendObjectWithDirectives(type, directives)); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+234
to
+243
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: This would break convention in this file...
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||
* addSubscriptionArguments | ||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,13 @@ export const getField = (obj: ObjectTypeDefinitionNode, fieldName: string): Fiel | |
|
||
export const getType = (schema: DocumentNode, typeName: string): ObjectTypeDefinitionNode | undefined => | ||
schema.definitions.find((def) => isObjectTypeDefinitionNode(def) && def.name.value === typeName) as ObjectTypeDefinitionNode | undefined; | ||
|
||
/** | ||
* Returns true if the node has a directive named `name` | ||
*/ | ||
export const hasDirectiveWithName = ( | ||
node: FieldDefinitionNode | InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, | ||
name: string, | ||
): boolean => { | ||
return node.directives?.some((d) => d.name.value === name) ?? false; | ||
}; | ||
Comment on lines
+55
to
+60
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. Possible duplicate/extension of code here: export const directiveExists = (definition: ObjectTypeDefinitionNode, name: string) => {
return definition?.directives?.find((directive) => directive?.name?.value === name);
}; 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. Oh, thanks for finding that. I'll fix in a followup |
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. Renamed this file from "defnition.test.ts", added the entire |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { isObjectTypeDefinitionNode } from '@aws-amplify/graphql-transformer-core'; | ||
import { extendObjectWithDirectives, getNonModelTypes, makeDirective } from '../definition'; | ||
import { Kind, ObjectTypeDefinitionNode, parse } from 'graphql'; | ||
|
||
const testModel = ` | ||
type Post @model { | ||
id: ID! | ||
title: String | ||
} | ||
`; | ||
|
||
describe('gets Non-Model Types', () => { | ||
it('should return empty list if no non-model types', () => { | ||
const document = parse(testModel); | ||
const nonModelTypes = getNonModelTypes(document); | ||
expect(nonModelTypes).toEqual([]); | ||
}); | ||
|
||
it('should return non-model types', () => { | ||
const testDocument = parse(` | ||
type Post @model { | ||
id: ID! | ||
nonModel: NonModel | ||
} | ||
|
||
type NonModel { | ||
id: ID! | ||
} | ||
`); | ||
const nonModelTypes = getNonModelTypes(testDocument); | ||
expect(nonModelTypes).toHaveLength(1); | ||
expect((nonModelTypes[0] as ObjectTypeDefinitionNode).name?.value).toEqual('NonModel'); | ||
}); | ||
}); | ||
|
||
describe('extendObjectWithDirectives', () => { | ||
it('should extend an object with one directive if the object does not already have directives', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(0); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, [makeDirective('dir0', [])]); | ||
|
||
expect(updatedObject.directives?.length).toEqual(1); | ||
expect(updatedObject?.directives?.[0].name.value).toEqual('dir0'); | ||
}); | ||
|
||
it('should extend an object with one directive if the object already has directives', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo @existingDir0 @existingDir1 { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(2); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, [makeDirective('dir0', [])]); | ||
|
||
expect(updatedObject.directives?.length).toEqual(3); | ||
expect(updatedObject?.directives?.[2].name.value).toEqual('dir0'); | ||
}); | ||
|
||
it('should extend an object with multiple directives if the object does not already have directives', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(0); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, [makeDirective('dir0', []), makeDirective('dir1', [])]); | ||
|
||
expect(updatedObject.directives?.length).toEqual(2); | ||
expect(updatedObject?.directives?.[0].name.value).toEqual('dir0'); | ||
expect(updatedObject?.directives?.[1].name.value).toEqual('dir1'); | ||
}); | ||
|
||
it('should extend an object with multiple directives if the object already has directives', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo @existingDir0 @existingDir1 { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(2); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, [makeDirective('dir0', []), makeDirective('dir1', [])]); | ||
|
||
expect(updatedObject.directives?.length).toEqual(4); | ||
expect(updatedObject?.directives?.[2].name.value).toEqual('dir0'); | ||
expect(updatedObject?.directives?.[3].name.value).toEqual('dir1'); | ||
}); | ||
|
||
it('should not add duplicate directives', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo @existingDir0 @existingDir1 { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(2); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, [makeDirective('existingDir0', []), makeDirective('dir1', [])]); | ||
|
||
expect(updatedObject.directives?.length).toEqual(3); | ||
expect(updatedObject?.directives?.[0].name.value).toEqual('existingDir0'); | ||
expect(updatedObject?.directives?.[1].name.value).toEqual('existingDir1'); | ||
expect(updatedObject?.directives?.[2].name.value).toEqual('dir1'); | ||
}); | ||
|
||
it('should return the object unchanged if directives is empty', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo @existingDir0 @existingDir1 { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(2); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, []); | ||
|
||
expect(updatedObject).toBe(object); | ||
}); | ||
|
||
it('should return the object unchanged if multiple directives are specified but all already exist', () => { | ||
const doc = parse(/* GraphQL */ ` | ||
type Foo @existingDir0 @existingDir1 { | ||
id: ID! | ||
} | ||
`); | ||
|
||
const object = doc.definitions.find(isObjectTypeDefinitionNode)!; | ||
expect(object?.directives?.length).toEqual(2); | ||
|
||
const updatedObject = extendObjectWithDirectives(object, [makeDirective('existingDir0', []), makeDirective('existingDir1', [])]); | ||
|
||
expect(updatedObject).toBe(object); | ||
}); | ||
}); |
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 was an opportunistic test addition that I found while I was investigating how a customer-provided GraphQL schema is being mutated in other contexts.