Amplify-cli: Expected scalar and got X

Created on 19 Jun 2019  路  6Comments  路  Source: aws-amplify/amplify-cli

Is there a way I can group my results based on a custom scalar?

type Group
  @model
 {
  id: ID!
  owner: String
  groupname: String!
  groupcode: String!
  swipes: [Swipe] @connection(name: "GroupSwipes")
}

type Schedule
  @model
  @key(name: "ByGroup", fields: ["group"], queryField: "schedulesByGroup")
{
  id: ID!
  recipe: Recipe! @connection
  group: Group!
  cookdate: AWSDateTime
  timestamp: AWSDateTime
}

What I like to query is "Give me all the schedules for group X"
But when I setup like the schema above I get an error saying: Expected scalar and got Group

graphql-transformer pending-response question

Most helpful comment

@sprucify The @key directive can only create indexes on top of scalar fields. If you want to index such that you can lookup resources by the logical then you can add a field groupId: ID! and then index on that.

Is there a specific reason for this? Why, for example, can't we use @key(name: "byProductByStatus", fields: ["id", "status"]) where the status attribute is an enum instead of a scalar type?

All 6 comments

@sprucify The @key directive can only create indexes on top of scalar fields. If you want to index such that you can lookup resources by the logical then you can add a field groupId: ID! and then index on that.

Thanks Mike. How can @key be applied to a '@connection' field. Example:

type Content @model
@key(name: "BySite", fields: ["site", "status"], queryField: "itemsBySite")
{
id: ID!
name: String!
site: Site! @connection(name: "SiteProjects")
status: Status!
}

enum Status {
ACTIVE
DELETED
}

I changed my structure as recommended, but this results in a rollback:

type Group
  @model
 {
  id: ID!
  owner: String
  groupname: String!
  groupcode: String!
  swipes: [Swipe] @connection(name: "GroupSwipes")
}

type Schedule
  @model
  @key(name: "ByGroup", fields: ["groupId"], queryField: "schedulesByGroup")
{
  id: ID!
  recipe: Recipe! @connection
  groupId: ID!
  cookdate: AWSDateTime
  timestamp: AWSDateTime
}

The error in the log:

 One or more parameter values were invalid: Both ReadCapacityUnits and WriteCapacityUnits must be specified for index: ByGroup (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: *)

I created a new issue for this #1714

@sprucify The @key directive can only create indexes on top of scalar fields. If you want to index such that you can lookup resources by the logical then you can add a field groupId: ID! and then index on that.

Is there a specific reason for this? Why, for example, can't we use @key(name: "byProductByStatus", fields: ["id", "status"]) where the status attribute is an enum instead of a scalar type?

Especially because you CAN use enums as part of composite sort keys by doing something like @key(name: "byProductByStatus", fields: ["id", "status", "createdAt"]). They're all just strings to Dynamo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kaustavghosh06 picture kaustavghosh06  路  51Comments

davidbiller picture davidbiller  路  62Comments

DonPepone picture DonPepone  路  70Comments

ChrisWun picture ChrisWun  路  43Comments

undefobj picture undefobj  路  78Comments