Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 53 additions & 30 deletions packages/data-schema/__tests__/CustomOperations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,15 +907,15 @@ describe('CustomOperation transform', () => {
.query()
.arguments({})
.handler(a.handler.function(fn1).async())
.authorization((allow) => allow.authenticated())
.authorization((allow) => allow.authenticated()),
});

const { schema, lambdaFunctions } = s.transform();
expect(schema).toMatchSnapshot();
expect(lambdaFunctions).toMatchObject({
FnGetPostDetails: fn1,
});
})
});

test('defineFunction sync - async', () => {
const fn1 = defineFunctionStub({});
Expand All @@ -927,15 +927,15 @@ describe('CustomOperation transform', () => {
a.handler.function(fn1),
a.handler.function(fn1).async(),
])
.authorization((allow) => allow.authenticated())
.authorization((allow) => allow.authenticated()),
});

const { schema, lambdaFunctions } = s.transform();
expect(schema).toMatchSnapshot();
expect(lambdaFunctions).toMatchObject({
FnGetPostDetails: fn1,
});
})
});

test('defineFunction sync - async with returns generates type errors', () => {
const fn1 = defineFunctionStub({});
Expand All @@ -949,9 +949,9 @@ describe('CustomOperation transform', () => {
])
.authorization((allow) => allow.authenticated())
// @ts-expect-error
.returns({ })
.returns({}),
});
})
});

test('defineFunction async - async', () => {
const fn1 = defineFunctionStub({});
Expand All @@ -965,7 +965,7 @@ describe('CustomOperation transform', () => {
a.handler.function(fn1).async(),
a.handler.function(fn2).async(),
])
.authorization((allow) => allow.authenticated())
.authorization((allow) => allow.authenticated()),
});

const { schema, lambdaFunctions } = s.transform();
Expand All @@ -974,7 +974,7 @@ describe('CustomOperation transform', () => {
FnGetPostDetails: fn1,
FnGetPostDetails2: fn2,
});
})
});

test('defineFunction async - sync', () => {
const fn1 = defineFunctionStub({});
Expand All @@ -987,12 +987,12 @@ describe('CustomOperation transform', () => {
a.handler.function(fn1),
])
.returns(a.customType({}))
.authorization((allow) => allow.authenticated())
.authorization((allow) => allow.authenticated()),
});

const { schema, lambdaFunctions } = s.transform();
expect(schema).toMatchSnapshot();
})
});

test('pipeline / mix', () => {
const fn1 = defineFunctionStub({});
Expand Down Expand Up @@ -1341,15 +1341,14 @@ describe('custom operations + custom type auth inheritance', () => {

test('implicit custom type inherits auth rules from referencing op', () => {
const s = a.schema({
MyQueryReturnType: a.customType({
fieldA: a.string(),
fieldB: a.integer(),
}),
myQuery: a
.query()
.handler(a.handler.function('myFn'))
.returns(
a.customType({
fieldA: a.string(),
fieldB: a.integer(),
}),
)
.returns(a.ref('MyQueryReturnType'))
.authorization((allow) => allow.publicApiKey()),
});

Expand All @@ -1363,23 +1362,22 @@ describe('custom operations + custom type auth inheritance', () => {

test('nested custom types inherit auth rules from top-level referencing op', () => {
const s = a.schema({
MyQueryReturnType: a.customType({
fieldA: a.string(),
fieldB: a.integer(),
nestedCustomType: a.customType({
nestedA: a.string(),
nestedB: a.string(),
grandChild: a.customType({
grandA: a.string(),
grandB: a.string(),
}),
}),
}),
myQuery: a
.query()
.handler(a.handler.function('myFn'))
.returns(
a.customType({
fieldA: a.string(),
fieldB: a.integer(),
nestedCustomType: a.customType({
nestedA: a.string(),
nestedB: a.string(),
grandChild: a.customType({
grandA: a.string(),
grandB: a.string(),
}),
}),
}),
)
Comment on lines 1364 to -1382

This comment was marked as outdated.

.returns(a.ref('MyQueryReturnType'))
.authorization((allow) => allow.publicApiKey()),
});

Expand All @@ -1401,6 +1399,31 @@ describe('custom operations + custom type auth inheritance', () => {
);
});

test('inline custom type inherits auth rules from referencing op', () => {
const s = a.schema({
myQuery: a
.query()
.handler(a.handler.function('myFn'))
.returns(
a.customType({
fieldA: a.string(),
fieldB: a.integer(),
}),
)
.authorization((allow) => allow.publicApiKey()),
});

const result = s.transform().schema;

expect(result).toMatchSnapshot();
expect(result).toEqual(expect.stringContaining('type MyQueryReturnType {'));
expect(result).toEqual(
expect.stringContaining(
'myQuery: MyQueryReturnType @function(name: "myFn") @auth(rules: [{allow: public, provider: apiKey}])',
),
);
});

test('top-level custom type with nested top-level custom types inherits combined auth rules from referencing ops', () => {
const s = a.schema({
myQuery: a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,20 @@ type Query {
`;

exports[`custom operations Add entities to SQL schema add custom type, enum, and custom query to generated SQL schema 1`] = `
"type post @model(timestamps: null) @auth(rules: [{allow: private}])
{
title: String!
description: String
author: String
}

enum PostStatus {
"enum PostStatus {
draft
pending
approved
published
}

type post @model(timestamps: null) @auth(rules: [{allow: private}])
{
title: String!
description: String
author: String
}

type PostMeta @aws_cognito_user_pools
{
viewCount: Int
Expand Down Expand Up @@ -595,7 +595,11 @@ exports[`schema auth rules global public auth - multiple models 1`] = `
"functionSlots": [],
"jsFunctions": [],
"lambdaFunctions": {},
"schema": "type A @model @auth(rules: [{allow: public, provider: apiKey}])
"schema": "enum DTired {
?
}

type A @model @auth(rules: [{allow: public, provider: apiKey}])
{
field: String
}
Expand All @@ -621,10 +625,6 @@ type D @model @auth(rules: [{allow: public, provider: apiKey}])
tired: DTired
cId: ID
c: C @belongsTo(references: ["cId"])
}

enum DTired {
?
}",
}
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CustomOperation transform dynamo schema Custom Mutation w required arg and enum 1`] = `
"type Post @model @auth(rules: [{allow: private}])
{
title: String
}

enum LikePostReactionType {
"enum LikePostReactionType {
:shipit:
:risitas:
}

type Post @model @auth(rules: [{allow: private}])
{
title: String
}

type Mutation {
likePost(postId: String!, reactionType: LikePostReactionType): Post
}"
Expand Down Expand Up @@ -45,8 +45,7 @@ exports[`CustomOperation transform dynamo schema Custom mutation w inline boolea
`;

exports[`CustomOperation transform dynamo schema Custom mutation w inline custom return type 1`] = `
"type LikePostReturnType
{
"type LikePostReturnType {
stringField: String
intField: Int
floatField: Float
Expand Down Expand Up @@ -109,8 +108,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema Custom query w inline custom return type 1`] = `
"type GetPostDetailsReturnType
{
"type GetPostDetailsReturnType {
stringField: String
intField: Int
floatField: Float
Expand Down Expand Up @@ -258,8 +256,7 @@ type Subscription {
`;

exports[`CustomOperation transform dynamo schema custom subscriptions Custom subscription where .for() resource has a CustomType return type 1`] = `
"type CreateCustomTypePostReturnType @aws_api_key
{
"type CreateCustomTypePostReturnType {
Copy link
Member

Choose a reason for hiding this comment

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

Here and below, I'm seeing that the auth graphql directives (@aws_api_key, @aws_cognito_user_pools, etc.) are now getting dropped from the generated types.

Does this PR actually need to change anything about how return types are handled relative to main? I could be wrong, but AFAIK, we already support custom types and refs to custom types as the return types for custom queries and mutations.

title: String!
}

Expand Down Expand Up @@ -340,8 +337,7 @@ exports[`CustomOperation transform dynamo schema handlers a.handler.custom a.han
title: String
}

type GetPostDetailsReturnType @aws_api_key
{
type GetPostDetailsReturnType {

}

Expand All @@ -351,8 +347,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.custom a.handler.custom with auth works 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools(cognito_groups: ["groupA", "groupB"])
{
"type GetPostDetailsReturnType {

}

Expand All @@ -373,8 +368,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.function defineFunction 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools
{
"type GetPostDetailsReturnType {

}

Expand All @@ -395,8 +389,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.function defineFunction async - sync 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools
{
"type GetPostDetailsReturnType {

}

Expand Down Expand Up @@ -428,8 +421,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.function pipeline / mix 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools
{
"type GetPostDetailsReturnType {

}

Expand All @@ -439,8 +431,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.function string 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools
{
"type GetPostDetailsReturnType {

}

Expand All @@ -450,8 +441,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.inlineSql escapes quotes 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools
{
"type GetPostDetailsReturnType {

}

Expand All @@ -472,8 +462,7 @@ exports[`CustomOperation transform dynamo schema handlers a.handler.inlineSql wo
"functionSlots": [],
"jsFunctions": [],
"lambdaFunctions": {},
"schema": "type GetPostDetailsReturnType @aws_cognito_user_pools
{
"schema": "type GetPostDetailsReturnType {

}

Expand All @@ -484,8 +473,7 @@ type Query {
`;

exports[`CustomOperation transform dynamo schema handlers a.handler.sqlReference works 1`] = `
"type GetPostDetailsReturnType @aws_cognito_user_pools
{
"type GetPostDetailsReturnType {

}

Expand Down Expand Up @@ -545,6 +533,17 @@ type Query {
}"
`;

exports[`custom operations + custom type auth inheritance inline custom type inherits auth rules from referencing op 1`] = `
"type MyQueryReturnType {
fieldA: String
fieldB: Int
}

type Query {
myQuery: MyQueryReturnType @function(name: "myFn") @auth(rules: [{allow: public, provider: apiKey}])
}"
`;

exports[`custom operations + custom type auth inheritance nested custom types inherit auth rules from top-level referencing op 1`] = `
"type MyQueryReturnType @aws_api_key
{
Expand Down
Loading
Loading