Coder Social home page Coder Social logo

Comments (8)

almeynman avatar almeynman commented on May 6, 2024 2

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.

wincent avatar wincent commented on May 6, 2024

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.

bmcmahen avatar bmcmahen commented on May 6, 2024

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.

athibaud avatar athibaud commented on May 6, 2024

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 indexOffails.

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.

wincent avatar wincent commented on May 6, 2024

@athibaud:

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.

bmcmahen avatar bmcmahen commented on May 6, 2024

Yup, that sounds good to me.

from graphql-relay-js.

athibaud avatar athibaud commented on May 6, 2024

@wincent 👍

from graphql-relay-js.

almeynman avatar almeynman commented on May 6, 2024

@wincent that solves the problem

from graphql-relay-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.