Error: @default only accepts scalar types, which might be why the array lints badly

Error: @default only accepts scalar types, which might be why the array lints badly


0

I’m trying to amplify push my updated amplify/backend/api/<myAPIName>/schema.graphql file. I am getting the following error:

An error occurred during the push operation: /
Schema validation failed.

Expected type String, found ["Admins", "Users"].

canRead: [String] @default(value: ["Admins", "Users"])

Expected type String, found ["Admins", "Users"].

The offending code in schema.graphql:

type Card {
  ...
  canRead: [String] @default(value: ["Admins", "Users"])
}

As you can see, I’m trying to set a field "canRead" on my Card object. The field should be an array of strings. I want the default value to be ["Admins", "Users"], but Amplify/graphql only accepts Scalar types for default values.

The GraphQL documentation lists things such as integers and strings as Scalar types, but then confusingly says that "appearsIn" will resolve to a scalar in the provided example:

{
   "data": {
    "hero": {
      "name": "R2-D2",
      "appearsIn": [
        "NEWHOPE",
        "EMPIRE",
        "JEDI"
      ]
    }
}

Which I took to mean that an array of strings might also be considered a scalar, but apparently not or else my code would presumably work?

The documentation goes on to tease with the following:

In most GraphQL service implementations, there is also a way to
specify custom scalar types. For example, we could define a Date type:

scalar Date

Then it’s up to our implementation to define how
that type should be serialized, deserialized, and validated. For
example, you could specify that the Date type should always be
serialized into an integer timestamp, and your client should know to
expect that format for any date fields.

But it doesn’t describe how to do this, and what the limitations are. Can anyone advise if/how defining a default array of strings can be achieved?

1 Answer
1


0

Had something similar come up recently also.

I don’t think you can specify a default for an Array, only scalar types as mentioned in the docs here: https://docs.amplify.aws/cli/graphql/data-modeling/#assign-default-values-for-fields I think the tease "and more" refers to the AWS specific types referenced here: https://docs.aws.amazon.com/appsync/latest/devguide/scalars.html which covers off ID, Int, String, Float, Boolean and some exotic types from AWS like AWSDateTime

My option was to set on creation – Not the answer you’re looking for but hope it helps.



Leave a Reply

Your email address will not be published. Required fields are marked *