Skip to content

Instantly share code, notes, and snippets.

@pikanji
Last active June 13, 2024 01:19
Show Gist options
  • Save pikanji/dcb6749293ddb4c68332c56ee910d1ba to your computer and use it in GitHub Desktop.
Save pikanji/dcb6749293ddb4c68332c56ee910d1ba to your computer and use it in GitHub Desktop.
Amplify GraphQL Directive Definitions
import gql from 'graphql-tag';
// https://docs.amplify.aws/cli-legacy/graphql-transformer/directives/
const clientSchemaExtensions = gql`
# https://docs.amplify.aws/cli/graphql/data-modeling/#how-it-works
directive @model(
queries: ModelQueryMap
mutations: ModelMutationMap
subscriptions: ModelSubscriptionMap
timestamps: TimestampConfiguration
) on OBJECT
input ModelMutationMap {
create: String
update: String
delete: String
}
input ModelQueryMap {
get: String
list: String
}
input ModelSubscriptionMap {
onCreate: [String]
onUpdate: [String]
onDelete: [String]
level: ModelSubscriptionLevel
}
enum ModelSubscriptionLevel {
off
public
on
}
input TimestampConfiguration {
createdAt: String
updatedAt: String
}
# https://docs.amplify.aws/cli-legacy/graphql-transformer/key/#definition
directive @key(
name: String
fields: [String!]!
queryField: String
) repeatable on OBJECT
# https://docs.amplify.aws/cli-legacy/graphql-transformer/auth/#definition
directive @auth(rules: [AuthRule!]!) on OBJECT | FIELD_DEFINITION
input AuthRule {
allow: AuthStrategy!
provider: AuthProvider
ownerField: String # defaults to "owner" when using owner auth
identityClaim: String # defaults to "username" when using owner auth
groupClaim: String # defaults to "cognito:groups" when using Group auth
groups: [String] # Required when using Static Group auth
groupsField: String # defaults to "groups" when using Dynamic Group auth
operations: [ModelOperation] # Required for finer control
# The following arguments are deprecated. It is encouraged to use the 'operations' argument.
queries: [ModelQuery]
mutations: [ModelMutation]
}
enum AuthStrategy { owner groups private public }
enum AuthProvider { apiKey iam oidc userPools }
enum ModelOperation { create update delete read }
# The following objects are deprecated. It is encouraged to use ModelOperations.
enum ModelQuery { get list }
enum ModelMutation { create update delete }
# https://docs.amplify.aws/cli-legacy/graphql-transformer/connection/#definition
directive @connection(keyName: String, fields: [ String! ]) on FIELD_DEFINITION
# https://docs.amplify.aws/cli-legacy/graphql-transformer/function/#definition
directive @function(name: String!, region: String) on FIELD_DEFINITION
# https://docs.amplify.aws/cli-legacy/graphql-transformer/http/#definition
directive @http(method: HttpMethod, url: String!, headers: [HttpHeader]) on FIELD_DEFINITION
enum HttpMethod { PUT POST GET DELETE PATCH }
input HttpHeader {
key: String
value: String
}
# https://docs.amplify.aws/cli-legacy/graphql-transformer/predictions/#definition
directive @predictions(actions: [PredictionsActions!]!) on FIELD_DEFINITION
enum PredictionsActions {
identifyText # uses Amazon Rekognition to detect text
identifyLabels # uses Amazon Rekognition to detect labels
convertTextToSpeech # uses Amazon Polly in a lambda to output a presigned url to synthesized speech
translateText # uses Amazon Translate to translate text from source to target language
}
# https://docs.amplify.aws/cli-legacy/graphql-transformer/searchable/#definition
# Streams data from DynamoDB to OpenSearch and exposes search capabilities.
directive @searchable(queries: SearchableQueryMap) on OBJECT
input SearchableQueryMap { search: String }
# https://docs.amplify.aws/cli-legacy/graphql-transformer/versioned/#definition
directive @versioned(
versionField: String = "version"
versionInput: String = "expectedVersion"
) on OBJECT
directive @aws_api_key on OBJECT | FIELD_DEFINITION
directive @aws_iam on OBJECT | FIELD_DEFINITION
directive @aws_oidc on OBJECT | FIELD_DEFINITION
directive @aws_cognito_user_pools on OBJECT | FIELD_DEFINITION
directive @aws_auth on FIELD_DEFINITION
directive @aws_subscribe(
mutations: [ String ]
) on FIELD_DEFINITION
directive @hasOne on FIELD_DEFINITION
directive @hasMany on FIELD_DEFINITION
directive @belongsTo on FIELD_DEFINITION
directive @manyToMany(relationName: String) on FIELD_DEFINITION
scalar AWSDateTime
`;
@rweintraub-cycleon
Copy link

@pikanji thanks for publishing this gist !

@Jupdi
Copy link

Jupdi commented Feb 4, 2022

Thanks for this!
Could you add all the new directives?
https://docs.amplify.aws/cli/migration/transformer-migration/
primaryKey and index are missing so far

@0afcode
Copy link

0afcode commented Feb 25, 2022

Thanks for publishing!

@ikushlianski
Copy link

Big thanks!

@pelly-ryu
Copy link

i added @index and AWSPhone in https://gist.github.com/pelly-ryu/b1c2f08457f96fc7c51a33b15ae62521
//
yet did only what i needed for now

@prashant-joshi-25
Copy link

Missing directives definition:

directive @primaryKey(sortKeyFields: [String!], queryField: String) on FIELD_DEFINITION
directive @index(name: String!, sortKeyFields: [String], queryField: String) on FIELD_DEFINITION

Source:
https://github.com/aws-amplify/amplify-codegen/blob/21bbff8f35b99c613dfd501a9141dfbfea12f260/packages/appsync-modelgen-plugin/src/scalars/supported-directives.ts#L139

@Jupdi
Copy link

Jupdi commented Jun 17, 2023

I just forked and updated to the version I'm using. No errors in the editor so far.
https://gist.github.com/Jupdi/57d8bc09bffacbc086bf6f4d972bc253

@jessethouin
Copy link

jessethouin commented Jun 13, 2024

Amazing work, everyone! I added indexName to @hasmany, and scalar AWSEmail.

https://gist.github.com/jessethouin/e6e1fa3fcaad1137eda807f4e9f7e14f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment