cult-of-coders / apollo-live-client

Handles reactive events to easily work with live queries
MIT License
12 stars 2 forks source link

Field "doc" must not have a selection since type "JSON" has no subfields. #4

Closed Twisterking closed 4 years ago

Twisterking commented 4 years ago

So I am very new to Apollo/GraphQL in general, so please bear with me:

const GET_USERS = gql`
  query($limit: Int, $skip: Int) {
    lists(limit: $limit, skip: $skip) {
      _id
      list_id
      row_id
    }
  }
`

const SUBSCRIBE_USERS = gql`
  subscription($limit: Int, $skip: Int) {
    lists(limit: $limit, skip: $skip) {
      event
      doc {
        _id
        list_id
        row_id
      }
    }
  }
`

My :

<ReactiveQuery
  query={GET_USERS}
  variables={{limit: 20, skip: (page-1)*20}}
  subscription={SUBSCRIBE_USERS}
>
  {
    ({ loading, error, data }) => {
      if(loading) return <h2>loading</h2>
      if(error) {
        console.log(error)
        return <h2>Error</h2>
      }                
      let { lists } = data;
      console.log(lists);                
      return 'loaded'
    }
  }
</ReactiveQuery>

my typeDefs on the server look something like this:

type Query {
  lists(limit: Int, skip: Int): [List]
  list(id: Int): List
}

type List @mongo(name: "listsbody") {
  _id: ID
  list_id: ID
  row_id: Int
}

type Item @mongo(name: "items") {
  _id: ID
  created_at: String
  supplier_id: ID
}

type Subscription {
  lists(limit: Int, skip: Int): SubscriptionEvent
}

... any my Subscription resolver:

Subscription: {
  lists: {
    resolve: payload => payload,
    subscribe(_, {limit, skip}, { db }, ast) {
      const observer = db.listsbody.astToQuery(ast, {
        $options: { limit, skip }
      });
      // const observer = db.listsbody.find({}, { limit: 100 });
      return asyncIterator(observer);
    }
  }
}

After some time always the same Error pops up:

Field "doc" must not have a selection since type "JSON" has no subfields.

I guess I am missing something here I just can't see right now with my not existing GraphQL experience. 😄 Hope someone can help, thanks!

theodorDiaconu commented 4 years ago
const SUBSCRIBE_USERS = gql`
  subscription($limit: Int, $skip: Int) {
    lists(limit: $limit, skip: $skip) {
      event
      doc
    }
  }
`
theodorDiaconu commented 4 years ago

Use it without nested fields, just doc

Twisterking commented 4 years ago

Thank you @theodorDiaconu!

Now I have a new error:

TypeError: observable.observeChanges is not a function in this line:

Subscription: {
  lists: {
    resolve: payload => payload,
    subscribe(_, {limit, skip}, { db }, ast) {
      const observer = db.listsbody.astToQuery(ast, {
        $options: { limit, skip }
      });
      return asyncIterator(observer); // THIS LINE //
    }
  }
}

Maybe I can't use astToQuery() in a subscription?!

theodorDiaconu commented 4 years ago

@Twisterking because it's done on a mongo cursor, not a query. db.listsbody.find() is a cursor.

Twisterking commented 4 years ago

Okay it kinda works now. Unfortunately the subscriptions don't update the data on the client. I already tried this:

<ReactiveQuery
  query={GET_USERS}
  variables={{listId: "pshLYD4optX3yj9Pw", limit: 20, skip: (page-1)*20}}
  subscription={SUBSCRIBE_USERS}
  fetchPolicy='no-cache'
>

no change! 😢

One more thing: Since you said that I don't need to define any subfields in the doc in my Subscription, why do you have it like this in your Readme?

theodorDiaconu commented 4 years ago

@Twisterking you'll have to create repro for me.