cult-of-coders / apollo-live-client Goto Github PK
View Code? Open in Web Editor NEWHandles reactive events to easily work with live queries
License: MIT License
Handles reactive events to easily work with live queries
License: MIT License
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!
I find that if I have fetched the query already and then close the subscription (navigate away from the page) and anything is changed while I am gone and then I come back the initial load will pull from the cache and be out of sync. I think that would be fixed just by changing the fetchPolicy to “cache-and-network” but I looked at your component and it looks like you’re only passing the query and variables to the react-apollo component.
Hi there,
I (think I) made quite some progress in understanding/using Apollo/GraphQL/Subscriptions but I now ran into the following problem:
Say I have a React component using your <ReactiveQuery/>
which has some variables
passed as prop. Problem is, that due to you only using componentDidMount()
(https://github.com/cult-of-coders/apollo-live-client/blob/master/src/ReactiveQuery.js#L37), (maybe instead of componentDidUpdate()
), the subscription is not updated when the variables change.
I do not understand how this should work like that ...
best regards, Patrick
if (found) {
return [...store.slice(0, idx), found, ...store.slice(idx + 1)];
}
Detect the query's name and simply replace with:
subscription {
users {
doc
event
}
}
Original Issue: Swydo/ddp-apollo#354
Following setup:
I have a parent React component (<TestPage/>
) which uses the standard withTracker()
HOC to load some stuff from normal Meteor subscriptions.
The wrapper <TestPage/>
then uses a standard logic in its render function like so: if(this.props.loading) return <Loading/>; // ... return <ReactiveQuery .../>
I wrote myself a little DDP message debugger based on this: https://stackoverflow.com/questions/25373648/how-to-view-meteor-ddp-traffic
Using this I can monitor subs and unsubs.
Following problem:
If the <Parent/>
displays <Loading/>
at least once before displaying the actual <ReactiveQuery .../>
the subscription is immediately canceled again after subbing!
When I remove the if(this.props.loading) return <Loading/>;
the subscription works just fine, although I have more (re-) renders obviously.
The problem is, that I can NOT simply remove if(this.props.loading) return <Loading/>;
in my actual production app, since there are many more child components depending on the data loaded inside the withTracker()
HOC. These are broken after remove the if(loading).
Anyone ever experienced this? I just don't understand what exactly is causing this behavior, but this is completely breaking my app because I really need this subscriptions to work!
<TestPage/>
class TestPage extends React.Component {
render() {
const { currentUser } = this.props.store.current;
const { loading } = this.props;
if(loading) return <Loading/> // this line breaks the Subscription!
return (
<div className="container">
<TestItemlist type="list" listId="YBxgJgkSNLHBmdNpp" openOrderId="jFZJm79tSQYuhqpna" />
</div>
)
}
}
export default withTracker((props) => {
const groupId = "7gLtWKifiRtEAeTvq";
const sub1 = Meteor.subscribe('buyer:getGroups');
const sub2 = Meteor.subscribe('group:getOpenOrderHead', groupId);
const sub3 = Meteor.subscribe('group:getLists', groupId);
const loading = !sub1.ready() || !sub2.ready() || !sub3.ready();
const group = Groups.findOne({ _id: groupId });
if(!group || loading) return { loading: true }
const supplier = Meteor.users.findOne({ _id: group.supplierId });
return {
loading,
group,
supplier,
lists: ListsHead.find({ groupIds: groupId, 'special': { $nin: ['blacklist'] } }, { sort: { datetime: -1 } }).fetch(),
currentOpenOrder: OpenOrdersHead.findOne({ group_id: groupId })
}
})(withMainStore(TestPage))
<TestItemlist/>
export default class TestItemlist extends React.PureComponent {
constructor() {
super();
this.state = {
page: 1
}
}
prevPage = () => {
this.setState(state => {
if(state.page == 1) return;
state.page--;
return state;
});
}
nextPage = () => {
this.setState(state => {
state.page++;
return state;
});
}
render() {
let query = GET_LIST_DATA;
let subscription = SUB_LIST_DATA;
let variables = { listId: this.props.listId, limit: 20, skip: (this.state.page-1)*20 };
const { type } = this.props;
if(type == 'cart') {
query = GET_CART_DATA;
subscription = SUB_CART_DATA;
variables = { openOrderId: this.props.openOrderId };
}
return (
<ReactiveQuery
query={query}
subscription={subscription}
variables={variables}
>
{(props) => {
const { data, error, loading, refetch } = props;
return ...
)
}}
</ReactiveQuery>
)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.