aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

How does the @connection work under the hood? #7272

Closed yaquawa closed 3 years ago

yaquawa commented 3 years ago

Hi, I'm having an error when referring a specific relationship field, so I decide take a look at the generated vtl resolvers. But I can't understand how it works with the @connection..

For example:

type Project @model {
  id: ID!
  name: String
  teamID: ID!
  team: Team @connection(fields: ["teamID"])
}

type Team @model {
  id: ID!
  name: String!
}

it gets something like this:

type Query {
    getProject(id: String!): Project
}

the source of the Project is a DynamoDB table, but it doesn't contain a team attribute, so how could the vtl resolver return such an object with the team field set? I can't see any related code in the vtl template for getProject. anyone tell me why? and how does it work?

attilah commented 3 years ago

It works as any other GraphQL implementation with it comes to related data, your example is the 'Has one' scenario from the docs.

I suggest to take a look at the build folder of your API where you can see the generated resolvers, also the AppSync console is a great help to better understand what is being generated/deployed for you.

The Project.team resolver is responsible to return the team data for a given project (as you query it through the Project type). In the AppSync console you can see the attached resolver to the team field.

I hope this answers your question, if you are just getting started with GraphQL and Amplify I would suggest to go through the various QuickStarts we have on the docs site.

yaquawa commented 3 years ago

Hi @attilah , thanks for the information! Do you mean the resolver of getProject don't have to return a Project object? It is allowed to return only partial fields of the Project object, and the lucked fields (team in this example) could be resolved from another resolver(Project.team in this example)?

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.