Skip to content

Instantly share code, notes, and snippets.

@deeja
Last active November 29, 2023 00:07
Show Gist options
  • Save deeja/50ff900a0780ba8c5afa0785e008be0f to your computer and use it in GitHub Desktop.
Save deeja/50ff900a0780ba8c5afa0785e008be0f to your computer and use it in GitHub Desktop.
Amplify GraphQL -> C# Code Generation using `graphql-codegen`
# Taken from https://github.com/dotansimha/graphql-code-generator/discussions/4311
scalar AWSTime
scalar AWSDate
scalar AWSDateTime
scalar AWSTimestamp
scalar AWSEmail
scalar AWSJSON
scalar AWSURL
scalar AWSPhone
scalar AWSIPAddress
scalar BigInt
scalar Double
directive @aws_subscribe(mutations: [String!]!) on FIELD_DEFINITION
# Allows transformer libraries to deprecate directive arguments.
directive @deprecated(reason: String!) on INPUT_FIELD_DEFINITION | ENUM
directive @aws_auth(cognito_groups: [String!]!) on FIELD_DEFINITION
directive @aws_api_key on FIELD_DEFINITION | OBJECT
directive @aws_iam on FIELD_DEFINITION | OBJECT
directive @aws_oidc on FIELD_DEFINITION | OBJECT
directive @aws_cognito_user_pools(
cognito_groups: [String!]
) on FIELD_DEFINITION | OBJECT
schema:
- amplify\backend\api\my_project\build\schema.graphql
- amplify_shim.graphql
generates:
AmplifyGraphQL.cs:
config:
scalars:
AWSDateTime: DateTimeOffset
AWSDate: DateOnly
AWSEmail: string
AWSJSON: string
AWSURL: string
AWSTimestamp: long
plugins:
- c-sharp
// Run using `npm install` the first time, then `npm run graphql-codegen` after that.
{
"scripts":{
"graphql-codegen": "graphql-codegen"
},
"devDependencies": {
"@graphql-codegen/c-sharp": "5.0.0",
"@graphql-codegen/cli": "5.0.0"
}
}
@deeja
Copy link
Author

deeja commented Nov 28, 2023

Hopefully this helps people. Discussion around Amplify GraphQL dotansimha/graphql-code-generator#4311

Issues I had:

Unknown type: "AWSDateTime".
Reason: AWS scalars are not defined in the generated amplify graphql schema. The extra amplify_shim.graphql schema helps with this.

✖ Cannot use GraphQLScalarType "AWSDate" from another module or realm
Reason: The globally installed graphql-js was different from the locally installed one. Adding an npm run command fixed it.

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