0

I have this query:

query ListFreightDriverTrucks($state: String! $tons: Float!) {
  listFreightDrivers(filter: {
    state: {
      contains: $state
    }
  }) {
    items {
      name
      city
      state
      trucks (filter: {
        tons: {
          eq: $tons
        }
      }) {
        items {
          id
          brand
          model
          fuelType
          fuelEfficiency
          utilityPercentage
          tons
          axes
          frontPhoto
          truckBox {
            type
            width
            height
            depth
          }
        }
      }
    }
  }
}

And I get as a response the data that match with the $state which is Jalisco.

{
  "data": {
    "listFreightDrivers": {
      "items": [
        {
          "name": "Jaen Carlos",
          "city": "Zapopan",
          "state": "Jalisco",
          "trucks": {
            "items": []
          }
        },
        {
          "name": "Diey",
          "city": "Zapopan",
          "state": "Jalisco",
          "trucks": {
            "items": []
          }
        },
        {
          "name": "Roberto mendez",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": {
            "items": []
          }
        },
        {
          "name": "Engineering",
          "city": "Zapopan",
          "state": "Jalisco",
          "trucks": {
            "items": []
          }
        },
        {
          "name": "Roberto mendez",
          "city": "Guadalajara",
          "state": "Jalisco",
          "trucks": {
            "items": []
          }
        },
        {
          "name": "Andrés",
          "city": "Zapopan",
          "state": "Jalisco",
          "trucks": {
            "items": [
              {
                "id": "2b0cb78e-49c4-4229-8a71-60b350a5fc47",
                "brand": "chevrolet",
                "model": "xx",
                "fuelType": "magna",
                "fuelEfficiency": 12,
                "utilityPercentage": 10,
                "tons": 15,
                "axes": 12,
                "frontPhoto": "freight-driver/e9adf7fb-09c2-477e-9152-56fe4a71a96b/trucks/dlb0275xqna51.png",
                "truckBox": {
                  "type": "Plataforma",
                  "width": 4,
                  "height": 4,
                  "depth": 4
                }
              }
            ]
          }
        }
      ]
    }
  }
}

If you check the response, there are some with this:

"trucks": {
   "items": []
}

But I'm not interested in those because do not match with the $tons just the last one did. How can I remove them?

In case I need to make a lambda how the DynamoDB queries will look?

Andrés Montoya
  • 4,079
  • 4
  • 18
  • 33

1 Answers1

1

I see this question a lot which makes me a bit insecure but GraphQL isn't really supposed to work that way. You are supposed to get what you ask for and not to "SQL query yourself to victory".

Anyhoot,

You could fix this in your resolvers (the req.vtl file) by filtering out all trucks.items.length < 1 or other things. Please see this link Appsync & GraphQL: how to filter a list by nested value

Be aware that this is a DynamoDB scan operation (all list operations are) which is quite slow.

AWS DynamoDB has the same design philosophy that you most of the time know the unique keys you are looking for and only filter over a small amount of items. Adding lots of indexes or combining keys.

Recommended reading if you want to update your data model: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices.html


Maybe rethink your GraphQL design? I don't know anything about trucks but maybe

  • "Location has Truck has Driver" instead? or
  • "Location has Driver has Truck"?

or even both! Since GraphQL gives you what you want a Driver can contain a Truck and a Truck a Driver.

Location {
id: ID!
truck: [Truck]
driver: [Driver]
}

Truck {
 id: ID!
 driver: Driver!
}

Driver {
 id: ID!
 Truck: Truck!
}

Amplify auto generates with depth 2 so that your lists don't circle forever and you can just don't ask for what you don't need. There are tons of options here.

https://docs.amplify.aws/cli/graphql-transformer/dataaccess


If you want to make it a Lambda (@function) the dynamo syntax is quite easy (and pretty much the same).

Either you scan the whole table https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#scan-property or you create an index which you query and then filter https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#query-property


Last but not least

Zanndorin
  • 360
  • 3
  • 15