Coder Social home page Coder Social logo

Comments (3)

hayes avatar hayes commented on August 15, 2024 1

The helpers definitely need some API improvement. I need to think carefully about what to add, how to expose it, and keep backwards compatibility. I have not actually spent a lot of time working on relay APIs myself, and need to experiment more before I can commit to how to build out additional helpers.

In the mean time, You could add something like this to your app:

(Note that I have not tested this at all, and it might be slightly wrong, but the general idea should work)

export async function resolveOffsetConnectionWithExtra<T, U>(
  options: {
    args: DefaultConnectionArguments;
    defaultSize?: number;
    maxSize?: number;
  },
  resolveResult: (params: {
    offset: number;
    limit: number;
  }) => Promise<{ nodes: T[]; connection: U }> | { nodes: T[]; connection: U },
): Promise<(ConnectionShape<SchemaTypes, T, false> & U) | null | undefined> {
  let resolveConnection: (con: U) => void;

  const connectionPromise = new Promise<U>((resolve) => {
    resolveConnection = resolve;
  });

  const result = await resolveOffsetConnection(options, async (params) => {
    const { nodes, connection } = await resolveResult(params);

    resolveConnection(connection);

    return nodes;
  });

  if (!result) {
    return result;
  }

  return {
    ...result,
    ...(await connectionPromise),
  };
}

from pothos.

litewarp avatar litewarp commented on August 15, 2024 1

The above is a little verbose but eliminates the duplication of my limit / offset problem.

For someone that doesn't have that much relay experience you've created some super useful tools! I can't thank you enough. If I get some time soon, I'll be sure and help you with the docs and anything else. Happy to take on chores when not busy.

from pothos.

hayes avatar hayes commented on August 15, 2024

this would be used something like:

resolve: async (parent, args) =>
        resolveOffsetConnectionWithExtra({ args }, ({ limit, offset }) => {
          const nodes = await getThings(offset, limit);

          return { nodes, connection: { totalCount: 500 } };
        }),
    },

from pothos.

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.