andreas / ocaml-graphql-server

GraphQL servers in OCaml
MIT License
624 stars 60 forks source link

Allow for `__typename` to be selected for alongside one other root field in a subscription operation #197

Closed sgrove closed 3 years ago

sgrove commented 3 years ago

In JavaScript, it's common to use the gql tag, which (in Apollo's case) will walk the operation and insert __typename on each object selection (presumably for its caching mechanism).

So this query:

const NPM_PUBLISHED_SUBSCRIPTION_ = gql`
  subscription NpmPublishedSubscription {
    npm {
      allPublishActivity {
        package {
          description
          name
        }
      }
    }
  }
`;

becomes:

subscription NpmPublishedSubscription {
  npm {
    allPublishActivity {
      package {
        description
        name
        __typename
      }
      __typename
    }
    __typename
  }
}

Unfortunately, this doesn't play well with subscriptions, where we'll get this error: Subscription field must have exactly 1 selection, found 2.

The error makes sense, except (perhaps?) for the __typename restriction. Should that selection be special-cased when checking for the 1-selection restriction?