Comments (8)
Ok. I will explain what I was trying to do: Suppose I have list of items. I want to make a RANGE_ADD mutation. In mutateAndGetPayload function I receive inputs and save new Item to db, then I return this item:
mutateAndGetPayload: async (input, { rootValue }) => {
const item = new Item({
text: input.text,
});
await item.save();
return { item };
},
In outputFields resolve function I fetch previously fetched items
outputFields: {
itemEdge: {
type: ItemEdgeType,
resolve: async ({ item }) => {
const array = await ItemModel.orderBy(...);
....
Here I want to return
return {
cursor: cursorForObjectInConnection(array, item),
node: item,
};
however cursorForObjectInConnection has indexOf in place, so it returns null, as item saved in mutateAndGetPayload is not the same object as the item in array, although they have exactly same properties and values.
So I have to manually find this Item in array to pass it to cursorForObjectInConnection, i.e.
outputFields: {
itemEdge: {
type: ItemEdgeType,
resolve: async ({ item }) => {
const array = await ItemModel.orderBy(...);
let itemToPass;
for (const i of array) {
if (i.id === item.id) {
itemToPass = item;
}
}
return {
cursor: cursorForObjectInConnection(array, itemToPass),
node: itemToPass,
};
SO I need to iterate and find an item in order for indexOf method to iterate correctly. It's not a big deal, I just don't like how the code looks
from graphql-relay-js.
I don't think we'd want to add a dependency on lodash, as we've intentionally kept the dependency footprint small (note that we only depend on babel-runtime
).
If you can provide a bit more detail about what you're trying to achieve, and maybe some sample code, we could suggest ways to make it easier to do what you want.
from graphql-relay-js.
I have the same problem. Perhaps an optional callback to provide our own equality conditions would suffice, or simply a third argument to specify the index of the matching node. I think the main problem is moreso silent failure when the nodeList
and node
come from two separate sources.
from graphql-relay-js.
i've had this problem myself... indeed it mostly arrises when nodeList and node come from seperate sources therefore the objects are equal in value but not in reference.. so indexOf
fails.
plus it's a real pity to have to pass trough the list twice (once to find our node in the nodeList and once again in cursorForObjecttInConnection
with indexOf
to find the index of that node in the same nodeList...)
@bmcmahen's callback solution seems reasonable but seeing all cursorForObjecttInConnection
does is return offsetToCursor(index) wouldn't it be simpler to just expose offsetToCursor
and let the user deal with finding the index ?
from graphql-relay-js.
wouldn't it be simpler to just expose
offsetToCursor
and let the user deal with finding the index
I'd be ok with this, especially seeing as its inverse, cursorToOffset
, is already exposed.
@bmcmahen, @almasakchabayev: Would that address your use case?
from graphql-relay-js.
Yup, that sounds good to me.
from graphql-relay-js.
@wincent 👍
from graphql-relay-js.
@wincent that solves the problem
from graphql-relay-js.
Related Issues (20)
- Update master to main HOT 1
- Build error in ./resources/prepublish.sh
- New release? HOT 6
- Return type check problem of mutationWithClientMutationId after 0.7.0 update HOT 3
- Is the peer dependency on graphql exactly 15.5.1 on purpose? HOT 3
- RangeError: Invalid code point HOT 2
- Are filtering operators supported? HOT 1
- isPromise is not a function HOT 3
- Add support for `graphql@v16`
- Can I return a union type on `outputFields`?
- Does anyone use this package with their Nest.js server?
- Merge the `graphql-relay-subcription` into `graphql-relay-js`? HOT 1
- Migrate from `chai` to `jest`?
- 0.10.0 connections causing compile error HOT 3
- To use this plugin is it necessary to get all records from the database with one request?
- filters connectionFromArray HOT 1
- Export pageInfoType
- 0.10.0 has no Flow type support HOT 4
- Make fields on PageInfo type shareable
- Compile error: 'NotSupportedTSVersion.d.ts' is not a module. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-relay-js.