1
votes

I am new to both graphql & AWS Amplify, so please forgive any ignorance :)

I have a graphql schema like this:

type Location @model @auth(rules: [{allow: owner}]){
  street: String
  city: String
  state: String
  zip: String
}

type Trip @model @auth(rules: [{allow: owner}]){
  id: String!
  ...
  location: Location
}

I'm trying to create both the location and the trip at the same time with a mutation request like this:

mutation {
  createTrip(input: {
      id: "someIdentifier",
      location: {
        street: "somewhere"
      }
  }) {
      id
      location {
        street
      }
  }
}

But I'm getting an error like this:

{
  "data": null,
  "errors": [
    {
      "path": null,
      "locations": [
        {
          "line": 2,
          "column": 21,
          "sourceName": null
        }
      ],
      "message": "Validation error of type WrongType: argument 'input' with value '...' contains a field not in 'CreateTripInput': 'location' @ 'createTrip'"
    }
  ]
}

Checking the generated schema.graphql file, I see that there is indeed no location object on the input model:

input CreateTripInput {
  id: String!
  ...
}

How can I have amplify generate the proper input schema so that I can create both the Trip and the location objects at the same time?

1

1 Answers

0
votes

I was able to get an answer from the aws-amplify team here. To summarize:

Both Trip and Location have model directive. There isn't a @connection directive connecting the Trip with Location. The two options to "resolving" this is:

Update the schema connecting the models if you want them to be in 2 separate tables and want the ability to query Trip based on Location. Using 2 separate table you won't be able to create both Trip and Location in a single mutation, though. For example:

type Location @model @auth(rules: [{allow: owner}]){
  street: String
  city: String
  state: String
  zip: String
  trips: Trip @connection(name:"TripLocation")
}

type Trip @model @auth(rules: [{allow: owner}]){
  id: String!
  location: Location @connection(name:"TripLocation")
}

The second option, if the Location data is very specific to a trip and you don't want to create a separate table, then get rid of @model directive from your Location type. Doing so would allow you to create Location as a part of same mutation.

type Location {
  street: String
  city: String
  state: String
  zip: String

}

type Trip @model @auth(rules: [{allow: owner}]){
  id: String!
  location: Location
}

The later was the solution that I moved forward with.