ngrx-graphql
A helper library for convertion of selectors made by ngrx-entity-relationship to a GraphQL query.
Supports
- Angular 6 and
ngrx/store@6
- Angular 7 and
ngrx/store@7
- Angular 8 and
ngrx/store@8
- Angular 9 and
ngrx/store@9
- Angular 10 and
ngrx/store@10
Installation
To install the package execute the next command npm install --save ngrx-graphql
.
The next step is to add needed augments for types. It will help with integration of ngrx-graphql
into ngrx-entity-relationship
.
For that add the next line in src/main.ts
.
;
Usage
Note: you should be familiar with ngrx-entity-relationship.
Imagine we have a selector:
;
And we want to get a GraphQL query like that:
{ user(id: "1") { id firstName lastName companyId company { id name addressId address { id street city country } } }}
The only issue is that the selector doesn't know fields of the entities. To solve this we need to define them manually as meta of every selector.
Don't forget to include fields for the id detection.
;
Profit, now we can use this selector to generate a GraphQL query via toGraphQL
helper function.
; // works for arrays of entities; // works with parameters; // converts parameters
The result will be
{ users { id firstName lastName companyId company { id name addressId address { id street city country } } }}
{ user(id: "1") { id # and all other fields with relationships. }}
{ user(id: "2") { id # and all other fields with relationships. }}
If we want we can combine them together to a single query
;
will generate
{ all:users { id # ... } u1:user(id: "1") { id # ... } u2:user(id: "2") { id # ... }}
Usage with HttpClient
An example of a ngrx effect and HttpClient
.
Usage with Apollo Service
An example of a ngrx effect and Apollo
service.
Subscriptions
Usage of toGraphQL
isn't enough to generate a subscription query.
Here toSubscription
solves the issue.
For example
;
will generate
subscription { users { id # ... }}
With Apollo
service it can be used like thath
apollo.subscribe.subscribe;
Mutations
Usage of toGraphQL
isn't enough to generate a mutation query.
Here toMutation
solves the issue.
For example
;
will generate
mutation { updateUser( id:"id1", data: { firstName:"updatedFirstName", lastName:"lastFirstName" } ) { id # and all other fields with relationships. }}
With Apollo
service it can be used like that
apollo.mutate.subscribe;
Query
Usage of toGraphQL
isn't enough to generate a query with variables.
Here toQuery
solves the issue, but about that in the Variables section.
Variables
All toQuery
, toSubscription
and toMutation
support variables.
They can be passed as the first parameter.
toGraphQL
supports $
to define variables instead of values.
apollo.mutate.subscribe;
will generate
mutation($data: UpdateUserInput!) { updateUser( id:"id1", data: $data ) { id # and all other fields with relationships. }}