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

Fragments of concrete types when querying interface fields throwing an error #739

Closed
davidyaha opened this issue Oct 3, 2016 · 1 comment
Assignees
Labels
Milestone

Comments

@davidyaha
Copy link
Contributor

I am getting an error while querying with a fragment of certain type in a field that is defined as interface:

  fragment message on Message {
      _id
      text
      createdAt
      user {
        _id
        avatar
      }
      reply {
        __typename
        expiration
        ... on OptionsReply {
          options
        }
      }
    }

The query being:

query chat($offset: Int!, $limit: Int!) {
    user: me {
      _id
      avatar
    }
    messages(offset: $offset, limit: $limit) {
      ...message
    }
  }

The error I am getting is the following "Network error: Can't find field options on result object $ROOT_QUERY.messages({"offset":0,"limit":50}).0.reply."

After poking the client code I found this error is being thrown here: https://github.com/apollostack/apollo-client/blob/master/src/data/writeToStore.ts#L184

It is supposed to be thrown when you don’t get a field that is in the selection fields. I think that the graphql spec defines fragments as a way to get fields of interfaces when they are implemented on a concrete type.

It seems that the client does check for the fragment type here: https://github.com/apollostack/apollo-client/blob/master/src/data/writeToStore.ts#L246
but it does not compare it to anything AFAIK.

Is this a bug? if so is there a fix on the way? if not, do you think a simple check on the line I pointed out could get rid of this error?

Thanks ahead!

@davidyaha
Copy link
Contributor Author

Wrote a failing test:

    it('should resolve fields it can on interface with non matching inline fragments', () => {
      storeRoundtrip(gql`
        query {
          dark_forces {
            __typename
            name
            ... on Droid {
              model
            }
          }
        }`, {
        dark_forces: [
          {
            __typename: 'Droid',
            name: '8t88',
            model: '88',
          },
          {
            __typename: 'Darth',
            name: 'Anakin Skywalker',
          },
        ],
      });
    });

Will try to fix it later today but it made me realise a workaround for it.
For anyone that comes across this issue before a fix is deployed, just make sure all possible concrete types have their own fragment in the query.

for the test above, the following query will not throw an error and give the same results:

query {
  dark_forces {
    __typename
    ... on Droid {
      name
      model
    }
    ... on Darth {
      name
    }
  }
}`

davidyaha added a commit to davidyaha/apollo-client that referenced this issue Oct 7, 2016
@stubailo stubailo self-assigned this Oct 12, 2016
@stubailo stubailo modified the milestones: New API/Refactor, Release 0.5 Oct 12, 2016
stubailo pushed a commit that referenced this issue Oct 13, 2016
stubailo pushed a commit that referenced this issue Oct 13, 2016
stubailo pushed a commit that referenced this issue Oct 15, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants