Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): incorrect selection set for optional associated field #509

Merged
merged 1 commit into from Jun 5, 2020

Conversation

lawmicha
Copy link
Member

@lawmicha lawmicha commented Jun 2, 2020

Issue #, if available:
#483

Description of changes:
This PR fixes a problem with the selection set that is generated for a Model containing a connection to another Model. Take this for example

type Post @model {
   user: User @connection
}

translates to "A Post has an optional association to the User"
.belongsTo(post.user, is: .optional, ofType: User.self, targetName: "postUserId"),

The original code checked for required associations

let isRequiredAssociation = field.isRequired && field.isAssociationOwner
if isRequiredAssociation, let associatedModel = field.associatedModel {

Consider a case where we retrieve the Post and the User does not exist on the Post. The selection set's intent is to retrieve that data in the response, and no User data is returned, thus nothing gets deserialized into the Post's User field.

This PR adds the following schema to test building a GraphQLRequest

type User @model {
    id: ID!
    name: String!
    following: [UserFollowing] @connection (name: "following")
    followers: [UserFollowers] @connection (name: "followers")
}

type UserFollowing @model {
  id: ID!
  user: User @connection(name: "following")
  followingUser: User @connection
}

type UserFollowers @model {
  id: ID!
  user: User @connection(name: "followers")
  followersUser: User @connection
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@lawmicha lawmicha self-assigned this Jun 4, 2020
@lawmicha lawmicha added the api Issues related to the API category label Jun 4, 2020
@lawmicha lawmicha changed the base branch from fix-api-temporaldatetime to master June 4, 2020 16:55
@lawmicha lawmicha force-pushed the fix-optional-associations branch 2 times, most recently from 746ff9a to 4112598 Compare June 5, 2020 19:22
@lawmicha lawmicha merged commit 9f9d658 into master Jun 5, 2020
@lawmicha lawmicha deleted the fix-optional-associations branch June 5, 2020 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues related to the API category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants