GithubHelp home page GithubHelp logo

Comments (3)

pie6k avatar pie6k commented on May 21, 2024 2

I've also created some example as I think it's quite common use-case:

from typegql.

pie6k avatar pie6k commented on May 21, 2024 1

Hey @lorefnon

There is great library https://github.com/jakepusateri/graphql-list-fields that is able to give you string[] of needed fields from info variable.

It can be used inside Inject function:

@Schema()
class SuperSchema {
  @Query({ type: [Issue] })
  issues(
    @NeededFields(['creator'])
    relations: string[], 
  ) {
    console.log({ relations });
    // ...
    return repo.find({ relations });
  }
}

I've created function named NeededFields that returns @Inject decorator. I could use @Inject inline, but it'd make schema code less readable. Here is this function definition:

import * as getFieldNames from 'graphql-list-fields';
import { Inject } from 'typegql';

function NeededFields(filterNames: string[] = []) {
  return Inject(({ info }) => {
    const selectionFieldNames: string[] = getFieldNames(info);

    return selectionFieldNames.filter(fieldName => {
      return allowed.includes(fieldName);
    });
  });
}

It takes argument filterNames: string[] so you can filter fields list to only those that are interesting for you (relations in your case).

Let me know if that is helpful for you.

Also, make sure to upgrade typegql to latest version (0.3.0+) as Inject api has changed slightly.

from typegql.

lorefnon avatar lorefnon commented on May 21, 2024 1

This is great! Thanks a lot for your work on this library.

from typegql.

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.