0

The intention is to query a list of users using an array of User IDs passed into a contains filter. The schema below is my attempt at solving this, but the query does not return a list of users. Even passing only a single User ID results in an empty query result. This schema is being published to AWS AppSync.

Is it possible to query a list of users using an array of User IDs in AppSync?

schema.graphql

type User @model {
  id: ID!
  username: String!
  friends: [String]
}

type Query {
    getAllFriends(filter: ModelAllFriendsFilterInput): ModelGetAllFriends
}

type ModelGetAllFriends {
  items: [User]
}

input ModelAllFriendsFilterInput {
  id: ModelAllFriendsIDInput
}

input ModelAllFriendsIDInput {
  contains: [ID]
}

GraphQL Query:

query MyQuery {
  getAllFriends(filter: {id: {contains: "VALID-USER-ID-HERE"}}) {
    items {
      id
      username
    }
  }
}

Query result:

{
  "data": {
    "getAllFriends": null
  }
}
4
  • Your problem seems to be with the *resolver*, not with the schema. What are your data source and resolvers? That is, where's the data coming from? Also, are you using the CDK, CloudFormation, or the console to configure Appsync? Commented Nov 3, 2021 at 16:59
  • I used amplify add api, so the data source is DynamoDB. Editing schema.graphql locally, and then publishing using amplify push. The simple auto-generated queries and mutations work, but not this custom query. Even though it is available in the interactive AppSync GraphQL user interface after the schema is compiled and published. Commented Nov 3, 2021 at 17:16
  • Right, amplify. Your actual question may not be "Is it possible to query a list of users using an array of User IDs in AppSync?", which is clearly yes and answered below. Rather, your question seems to be "why is my amplify auto-generated GraphQl resolver returning null?". I do not use the Amplify tool, but a start would be to debug your resolvers. Good luck! Commented Nov 3, 2021 at 19:37
  • Build an identical schema using the editor in the AppSync console. Same results. Commented Nov 4, 2021 at 0:09

1 Answer 1

1

Yes, lists are valid inputs in GraphQl fields.

The "null" response indicates that (a) Appsync passed your query to the resolver, (b) your resolver returned a result without error and (c) Appsync accepted the result. If any of these were not true, Appsync would have given you an error message. In other words, I believe the problem to be your resolver returning a null result, not the schema.

By the way, in the case of a list field like contains: [ID], Appsync will accept a list or a scalar value (like your {contains: "VALID-USER-ID-HERE"} above) as valid input. If you pass a scalar value to a list field, Appsync will helpfully pass it as a list/array value ["VALID-USER-ID-HERE"] in the lambda resolver's handler function arguments.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried defining contains in ModelAllFriendsIDInput as both a single ID and the array field in the example. Neither worked. What is also strange is if I pass the same User ID field through the auto-generated listUsers query using a contains argument, AppSync successfully returns that user. So just not sure why the custom query does not return the user.
Can confirm now that the resolver was missing. Was not aware that this is required (I'm new to AppSync and GraphQL). The resolver has been added. Still needing to write the request mapping template and response mapping template but im completely new to these concepts. Tested with some of the sample templates in AppSync, but they unfortunately did not work with this particular query out of the box.
The fact that it doesn't support querying in an array of IDs without custom resolver voodoo feels really short sighted...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.