Giter Club home page Giter Club logo

apollo-dynamic-angular's Introduction

Apollo Dynamic Angular npm version

Apollo Dynamic allows to create dynamic selection sets on queries, mutations and subscriptions when using @apollo/client for consult GraphQL resolvers. It works by decorating entity classes with @SelectionType and @SelectionField which allows to fabric dynamics selections set with a similar syntax as TypeORM repositories (relations).

This library is a wrapper of apollo-dynamic for Angular, it offer the same @Injectable classes as apollo-angular: Query, Mutation and Subscription. But with support for dynamic selection set based on Apollo Dynamic library. Given the new classes: DynamicQuery, DynamicMutation and DynamicSubscription.

Installation

$ npm install apollo-dynamic-angular

# dependencies
$ npm install apollo-dynamic apollo-angular @apollo/client graphql

Usage

Decorators

With this library you have to use the @SelectionType and @SelectionField from apollo-dynamic for decorate your entities interfaces and allow the selection set generation:

import { SelectionType, SelectionField } from 'apollo-dynamic'

@SelectionType('Person')
export class Person {
    @SelectionField()
    id?: string;

    @SelectionField()
    firstname?: string;

    @SelectionField()
    lastname?: string;

    @SelectionField({ include: 'isSuperAgent' })
    secret?: string;

    @SelectionField(() => Profile)
    profile: Profile;

    @SelectionField(() => Article)
    articles: Article[];
}

@SelectionType('Profile')
export class Profile {
    @SelectionField()
    avatar: string;

    @SelectionField()
    nickname: string;
}

@SelectionType('Article',{
    default: { relations: { artType: true } }
})
export class Article {
    @SelectionField({ skip: (cond) => cond.noIDsPlease })
    id: string,

    @SelectionField()
    name: string;

    @SelectionField(() => Person)
    person: Person;

    @SelectionField(() => ArticleType)
    artType: ArticleType;
}

@SelectionType('ArticleType')
export class ArticleType {
    @SelectionField()
    category: string;

    @SelectionField()
    section: string;
}

DynamicQuery, DynamicMutation and DynamicSubscription


But you can use this with apollo-angular mechanisms. For example:

const GET_PERSONS = gql`
  query GetPersons {
    persons {
      Person
    }
  }
`;
import { select } from 'apollo-dynamic';
import { Apollo } from 'apollo-angular';

@Component({
  // ...
})
export class ListPersons implements OnInit {
  // inject angular-apollo
  constructor(public apollo: Apollo);

  ngOnInit() {
    // use it with the "select" function
    this.apollo
      .query({
        query: select(GET_PERSONS, { relations: { profile: true } })
      })
      .subscribe(/*...*/);
  }
}

Or better, with the new approach of apollo-angular:

import { gql } from 'apollo-angular';
import { DynamicQuery } from 'apollo-dynamic-angular';

// use "Dynamic" version of Query, Mutation or Subscription
@Injectable({ providedIn: 'root' })
export class FindPersons extends DynamicQuery<{ persons: Person[] }> {
  override document = gql`
    query Persons {
      persons {
        Person
      }
    }
  `;
}
import { Component, OnInit } from '@angular/core';
import { FindPersons, Person } from './person.entity';

@Component({
  // ...
})
export class ListPersons implements OnInit {
  persons: Person[];

  // inject it
  constructor(private findPersons: FindPersons) {}

  ngOnInit() {
    // use it
    this.persons = this.findPersons({ relations: { profile: true } })
      .fetch()
      .subscribe({
        next: ({ data }: any) => {
          persons = data.persons;
        },
        error: (error: any) => {
          console.log(error);
        }
      });
  }
}

Same apply for Mutations and Subscriptions.


Please consider reading the apollo-dynamic and apollo-angular usage guides for more information.

Stay in touch

License

This package is MIT licensed.

apollo-dynamic-angular's People

Contributors

giuliano-marinelli avatar

Watchers

 avatar

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.