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

Unexpected '__typename not found on object' #509

Closed
AlistairB opened this issue Aug 25, 2020 · 7 comments · Fixed by #773 or #777
Closed

Unexpected '__typename not found on object' #509

AlistairB opened this issue Aug 25, 2020 · 7 comments · Fixed by #773 or #777
Labels
bug Something isn't working scope/client morpheus-graphql-client

Comments

@AlistairB
Copy link
Contributor

AlistairB commented Aug 25, 2020

Hi, possibly related to #493

I got around to using the changes for this feature. They are compiling as expected, however now I am getting a runtime error.

My query looks like:

defineByDocumentFile
  "../resource/minimal-github.graphql"
  [gql|
    query FetchRepoFile ($repoOwner: String!, $repoName: String!, $expression: String!) {
      repository(name: $repoName, owner: $repoOwner) {
        object(expression: $expression) {
          ... on Blob {
            isTruncated
            text
          }
        }
      }
    }
  |]

You can see the schema I am using here https://github.com/AlistairB/morpheus-repro/blob/master/minimal-github.graphql .

The error I am getting: Error in $.data.repository.object: key \"__typename\" not found on object

Running this in the github graphql explorer, eg. this query:

query { 
  repository(name: "react", owner: "facebook") {
    object(expression: "af219cc6e6c514099a667ffab4e2d80c5c0c1bcc:.nvmrc") {
      ... on Blob {
        isTruncated
        text
      }
    }
  }
}

produces this response:

{
  "data": {
    "repository": {
      "object": {
        "isTruncated": false,
        "text": "v12.16.2\n"
      }
    }
  }
}

I can include __typename in the query and will get back in the response "__typename": "Blob" (via the github graphql explorer).

Adding __typename to the query that morpheus is using causes compile time errors eg. Error: \"cant find field DataInterface.

Is this enough info? If not I can set up a repro in https://github.com/AlistairB/morpheus-repro

@nalchevanidze
Copy link
Member

hi @AlistairB. please checkout #511

@AlistairB
Copy link
Contributor Author

Thanks! This works if I change the query to:

defineByDocumentFile
  "../resource/minimal-github.graphql"
  [gql|
    query FetchRepoFile ($repoOwner: String!, $repoName: String!, $expression: String!) {
      repository(name: $repoName, owner: $repoOwner) {
        object(expression: $expression) {
          __typename
          ... on Blob {
            isTruncated
            text
          }
        }
      }
    }
  |]

This works for me as a solution.

However, if I don't add __typename to the query it still fails with the same error. This is a bit unintuitive since I don't need __typename for my purposes, but morpheus seems to require it. I think ideally morpheus would request __typename itself if it needs it even if it isn't in the query.

I could be wrong though. Happy to close if you think this is the right solution.

@nalchevanidze
Copy link
Member

@AlistairB you are right, i fixed that too. please checkout #512

@AlistairB
Copy link
Contributor Author

Unfortunately this is still producing the same error unless I manually include __typename.

I have updated master on https://github.com/AlistairB/morpheus-repro to reproduce the issue.

As per https://github.com/AlistairB/morpheus-repro/blob/master/src/Lib.hs#L50-L52 , you will need to create a github personal access token. Then you can reproduce the issue with stack run.

@AlistairB
Copy link
Contributor Author

AlistairB commented Sep 7, 2020

Hi, I've been playing with this a little bit more and noticed another minor issue. In the morpheus-repro example, the master version of morpheus will generate the following type:

data RepositoryObjectGitObject
  = RepositoryObjectGitObject {__typename :: Text} |
    RepositoryObjectBlob {__typename :: Text,
                          isTruncated :: Bool,
                          text :: (Maybe Text)}
  deriving Generic
  deriving Show
  deriving Eq

A sum type with record accessors is considered a bit of an anti-pattern. This is because isTruncated is now a partial function. ie. isTruncated (RepositoryObjectGitObject "blah") will compile, but will error at runtime. The data type will cause a ghc warning if compiled with -Wpartial-fields (which is not enabled by -Wall).

I pushed a new commit to https://github.com/AlistairB/morpheus-repro with the warning enabled to demonstrate the issue.

One solution is just to exclude the accessors, but this would get ugly if you had a lot of fields returned. I think the ideal solution would be something like:

data RepositoryObjectGitObject
  = RepositoryObjectGitObject RepositoryObjectGitObjectValues
  | RepositoryObjectBlob RepositoryObjectBlobValues
  deriving (Generic)
  deriving (Show)
  deriving (Eq)

data RepositoryObjectGitObjectValues = RepositoryObjectGitObjectValues {repositoryObjectGitObjectValues__typename :: Text}

data RepositoryObjectBlobValues = RepositoryObjectBlobValues
  { repositoryObjectBlobValues__typename :: Text, -- actually we need to prefix this now as there can't be overlapping `__typename`. Unless DuplicateRecordFields is enabled.
    isTruncated :: Bool,
    text :: (Maybe Text)
  }

Anyway, as mentioned I think this is minor. It could be a separate issue (I can create if you want), or not worried about. Just thought I'd mention it.

@nalchevanidze
Copy link
Member

hi @AlistairB ,

Hi, I've been playing with this a little bit more and noticed another minor issue. In the morpheus-repro example, the master version of morpheus will generate the following type:

please address it in different issue :)

Unfortunately this is still producing the same error unless I manually include __typename.

i checked this out. i will address this problem later, as long as it is not as trivial as i thought

@nalchevanidze
Copy link
Member

finally fixed it. see #773

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working scope/client morpheus-graphql-client
Projects
None yet
2 participants