I have a GraphQL query that returns an array of items. I can run the query in GraphiQL and get the result I expected.
I have a react application that uses react-apollo and the #graphql decorator to resolve the very same query to component props. When running the query via react-apollo, every item in the returned array is the same - each item has been 'overwritten' by the last item.
I can see in the dev-tools network tab that the correct array items were sent over the network, so the issue seems to relate to react-apollo. What could be causing react-apollo to overwrite array elements?
The issue was that my qgl fragment for the query did not include the id property for the items in the Array.
If you have a similar issue, ensure your schema includes an id or _id property on each item; that each id value is unique, and that you are requesting the id property in your query. Alternatively you can supply a dataIdFromObject function in your client to dynamically generate IDs for values.
You can read more in the Apollo docs for Normalization with dataIdFromObject
Related
I have a product and I have a property that brings an array with a provider id and its price. The idea is that every product has an array with multiples providers, each one with its own price.
However, when I use populate() looking for ProviderID, it brings me populated as string instead of a JSON. Below is GraphQL query return from a query that bring products.
ProviderPrice class, inside ProductModel
Optional parameter in ProductModel
This is how I am executing populate
Has anyone gone through this?
Found the problem.
I was using the class ProviderPrice instead of the model ProviderPriceModel.
I have a large array that comes in from an API that I'd like to store straight into MongoDB.
Model.create(largeArray) ... // many documents created
The problem is, I have one additional key:value pair that I need to set for all documents in that array. It's a user id, and many documents are created for a given user once per API call. So for a given Model.create call, the user id is the same for every doc in the array.
Without mapping over the array, is there an efficient way of adding a field with a consistent value? Something like Model.create(myLargeArray, {userId: someUserId}) would be ideal, but I know this isn't the case with the Mongoose API.
function addDocsForUser(largeArray, someUserId) {
// each element of largeArray needs to have `userId: someUserId` added to it
return Model.create(largeArray)
}
Trying to do a where filter in findOne and in Node api returns empty array when filtering by ids in loopback
https://url/api/Model1/findOne?filter={"where":{"attrs":"id"}}
where id is exactly 24 digits long
It is possible that you simple don't have an object with that ID in that particular collection in the database. In such a case you should get an empty array.
Or you may need to find by id and not by attr - it depends on how the relevant field in your database is named.
See the docs, there are good examples there: https://loopback.io/doc/en/lb2/Where-filter.html
E.g. this:
http://localhost:3000/api/Books?filter={"where":{"or":[{"id":1},{"id":2}]}}
Note that the id is used and not attr. See what is your field in your case.
I am developping the messaging part of an app.
I have a schema that represents a conversation. It contains an array of ObjectId, and each ObjectId refers to a message.
I would like the user to sent to my API a message _id, so I can retrieve all the messages _ids on the array, after the one he sent.
So he can update with all the messages he doesn't have yet.
I can I get the position of the _id he sends me, within the messages: [ObjectId] array, into the query ?
Mongo doesn't support this type of query on the server, so if you take this approach you will have to do it within your application.
You might want to consider other possible architectures. First, after you have the position of the last ObjectId, how will you make your subsequent query? You could instead query the messages collection directly, using the timestamp that is encoded in the _id to return only newer messages:
db.messages.find({_id: {$gt: lastViewedMessage._id}})
So far I haven't found any samples of HOW the elastic.js client api (https://github.com/fullscale/elastic.js) can be used for indexing documents. There are some clues here & there but nothing concrete yet.
http://docs.fullscale.co/elasticjs/ejs.Document.html
Document ( index, type, id ): Object used to create, replace, update, and delete documents
Document > doIndex(fnCallBack): Stores a document in the given index and type. If no id is set, one is created during indexing.
Document > source (doc): Sets the source document.
Can anyone provide a sample snippet of code to show how an document object can be instantiated and used to index data?
Thanks!
Update # 1 (Sun Apr 21st, 2013 on 12:58pm CDT)
https://gist.github.com/pulkitsinghal/5430444
Your gist is correct.
You create ejs.Document objects specifying the index, type, and optionally the id of the document you want indexed. If you don't specify an id, elasticsearch will generate one for you.
You set the source to the json object you want indexed then call the doIndex method specifying a callback if needed. The node example does not index docs, but the angular and jquery examples show a basic example and can easily be used with the node client.
https://github.com/fullscale/elastic.js/blob/master/examples/angular/js/controllers.js#L30
Also have a peek at the tests:
https://github.com/fullscale/elastic.js/blob/master/tests/index_test.js#L265
elastic.js nowadays only implements the Query DSL, so it can't be used for this scenario anymore. See this commit.