Apollo-ios: Error: Fields "Field" conflict because they return conflicting types Field and Field!

Created on 6 Nov 2019  Â·  14Comments  Â·  Source: apollographql/apollo-ios

I got this error when I try to compile my project:
error: Fields "commit" conflict because they return conflicting types Commit and Commit!. Use different aliases on the fields to fetch both if this was intentional.

Code below is my GraphQL fragments definition:

# Represents a 'merged' event on a given pull request.
# See https://developer.github.com/v4/object/mergedevent/
fragment MergedEventFragment on MergedEvent {
    # Identifies the actor who performed the event.
    actor {
        ... Actor
    }
    # Identifies the commit associated with the merge event.
    commit {
        ... PullRequestTimelineItemCommitFragment
    }
}

# Represents a Git commit.
# See https://developer.github.com/v4/object/commit/
fragment PullRequestTimelineItemCommitFragment on Commit {
    # Authorship details of the commit.
    author {
        ... GitActorFragment
    }
    # Committership details of the commit.
    committer {
        ... GitActorFragment
    }
    # The Git commit message
    message
    # The Git object ID
    oid
    # The HTTP URL for this commit
    url
}

MergedEvent definition from schema.json: https://gist.github.com/TonnyL/7bedf6f0ac7b0496cdc3a823cef71885 and the doc reference: https://developer.github.com/v4/object/mergedevent/
Commit definition from schema.json: https://gist.github.com/TonnyL/98f3a7941bb2d48cdfcafb212bd12ec1 and the doc reference: https://developer.github.com/v4/object/commit/

BTW, these GraphQL fragment definition could compile successfully when I use apollo-android.

codegen

All 14 comments

I know @davedelong ran into the same thing recently as well from this issue on our tooling repo. I'm gonna futz with it a bit and see if I can figure out what the hell is going on.

On Android are you using the incubating plugin or the main one? I ask mostly because there was a significant rewrite between the two.

I'm using the main one instead of incubating plugin on Android.

OK cool - I think I figured out Dave's issue, which was essentially that his query included two different ... on Type bits that wound up having the same name at the same level, with differing nullability options. This is definitely something to watch out for with nested fragments and/or trying to handle multiple different types in a single response.

If my answer to him helps out, awesome, otherwise if you can post the query you're having problems with I'll see what I can figure out.

Cool and thanks. Look forward to see any progress on it.

@TonnyL Were you able to figure out what was going on, or would you like to share the query you're making?

@designatednerd Sure, code below is my query:

# Items in a pull request timeline
query PullRequestTimelineItems($owner: String!, $name: String!, $number: Int!, $after: String, $before: String, $perPage: Int!) {
    repository(owner: $owner, name: $name) {
        pullRequest(number: $number) {
            timelineItems(first: $perPage, after: $after, before: $before) {
                nodes {
                    # Represents a 'merged' event on a given pull request.
                    ... MergedEventFragment
                    # ... other fragments
                }
                # Information to aid in pagination.
                pageInfo {
                    ... PageInfo
                }
            }
        }
    }
}

I would double check the fragments included with # ... other fragments to see if any of them contain commit at the same level MergedEventFragment does. It looks like you're having a similar problem, in that you've got two different things both named commit at the same level, but one is optional and the other isn't.

This "works" in terms of raw querying, but at least on the iOS side, it can't have code generated for it in a type safe fashion because of the conflict between optional and non-optional for the same name of a property in the same position.

Just thought of something: Are you having code generated in Java or Kotlin for Android? If it's Kotlin, is the generated property nullable?

You are right, # ... other fragments includes a fragment named PullRequestCommitFragment on PullRequestCommit which has a non-null field commit.

Yep, I've set the codegen generating Kotlin models. And yes, the generated property is nullable. Here are the generated codes.

data class Commit(
    val __typename: String,
    val fragments: Fragments
) 

data class Fragments(
    val pullRequestTimelineItemCommitFragment: PullRequestTimelineItemCommitFragment
)
data class MergedEventFragment(
  val __typename: String,

  val actor: Actor?,

  val commit: Commit?,

  val createdAt: Date,
  val id: String,

  val mergeRef: MergeRef?,

  val mergeRefName: String,

  val pullRequest: PullRequest

  val url: Uri
) : GraphqlFragment 
data class PullRequestCommitFragment(
  val __typename: String,

  val commit: Commit,
  val id: String,

  val pullRequest: PullRequest,

  val url: Uri
) : GraphqlFragment

Yeah it'd be nullable at the fragment level but it looks like there's some differences in terms of how it's set up between iOS and Android that make it difficult to have that work overall.

Same advice as I gave to Dave: Try aliasing the commit property on one of your fragments - then the names won't collide anymore.

Many thanks to you guys, I'll try it later 😉.

@TonnyL Did it wind up working?

@designatednerd Yes. After aliased all the conflicting fields, it finally worked. My project can build successfully now.

Hooray! Mind if we close this issue out then?

Close it plz. Thanks for your help 😀.

On Mon, Nov 18, 2019 at 02:20 Ellen Shapiro notifications@github.com
wrote:

Hooray! Mind if we close this issue out then?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/apollo-ios/issues/888?email_source=notifications&email_token=ADFWF7BXGBALUGJY6PV6YBDQUGDQNA5CNFSM4JJO35PKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEISMPA#issuecomment-554772028,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ADFWF7ASDTHOU3PXGMM6VRLQUGDQNANCNFSM4JJO35PA
.

Was this page helpful?
0 / 5 - 0 ratings