@imqueue/graphql-dependency

Cross service GraphQL dependency loading during query calls for @imqueue
ecosystem.
Install
npm i --save @imqueue/graphql-dependency
Usage
This module allows to describe cross-service dependencies and fetch user
requested data in an optimal manner. Let's imagine we have 2 micro-services
serving User
and Company
data respectively. Let's assume User can be
a team member of the Company. As well as Company can have User as an owner.
On GraphQL API level it may be represented by a following schema:
type User {
id: ID!
name: String!
email: String!
phone: String!
ownerOf: [Company]
memberOf: [Company]
}
type Company {
id: ID!
name: String!
description: String!
ownerId: Int!
owner: User
members: [User]
}
Now we have a query which can fetch a user with all related data, like this:
query user(id: "VXNlcjox") {
id
name
email
phone
ownerOf {
id
name
members {
id
name
phone
}
}
memberOf {
id
name
owner {
id
name
email
phone
}
}
}
As seen from such query we would need to implement resolver recursively loading
companies data for user and user data for companies.
With this module it's possible to resolve such dependencies automatically and
fetch data in a most efficient way using caching and minimizing number of
request by defining the dependencies between entities and their loaders and
initializers.
import { Dependency } from '@imqueue/graphql-dependency';
import {
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
} from 'graphql';
import { globalIdField } from 'graphql-relay';
import { userClient, companyClient } from '../clients';
export const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: globalIdField(
User.name,
(user: userClient.User) => user.id + '',
),
name: {
type: new GraphQLNonNull(GraphQLString),
resolve: (user: userClient.User) => user.name,
},
phone: {
type: new GraphQLNonNull(GraphQLString),
resolve: (user: userClient.User) => user.name,
},
email: {
type: new GraphQLNonNull(GraphQLString),
resolve: (user: userClient.User) => user.name,
},
ownerOf: {
type: new GraphQLList(Company),
resolve: (user: userClient.user) => user.name
}
}),
});
Dependency(User).defineLoader(async (
context: any,
filter: any,
fields: any,
): Promise<Array<Partial<userClient.User>>> =>
await context.user.list(filter, fields),
);
export const Company = new GraphQLObjectType({
name: 'Company',
fields: () => ({
id: globalIdField(
Company.name,
(company: companyClient.Company) => company.id + '',
),
name: {
type: new GraphQLNonNull(GraphQLString),
resolve: (company: companyClient.Company) => company.name,
},
description: {
type: new GraphQLNonNull(GraphQLString),
resolve: (company: companyClient.Company) => company.description,
},
ownerId: {
type: new GraphQLNonNull(GraphQLID),
resolve: (company: companyClient.Company) => company.ownerId,
},
owner: {
type: User,
resolve: (company: companyClient.Company) => company.owner,
},
members: {
type: new GraphQLList(User),
resolve: (company: companyClient.Company) => company.members,
},
}),
});
Dependency(Company).defineLoader(async (
context: any,
filter: any,
fields: any,
): Promise<Array<Partial<companyClient.Company>>> =>
await context.company.list(filter, fields),
);
Dependency(Company).require(User, () => ({
as: Company.getFields().owner,
filter: {
[User.getFields().id.name]: Company.getFields().ownerId,
},
}), () => ({
as: Company.getFields().members,
filter: {
'relatedCompanyIds': Company.getFields().id,
},
}));
Dependency(User).require(Company, () => ({
as: User.getFields().memberOf,
filter: {
'relatedMemberIds': User.getFields().id,
},
}), () => ({
as: User.getFields().ownerOf,
filter: {
[Company.getFields().ownerId]: User.getFields().id,
},
}));
With this setup we assume that user and company loaders implements data fetching
by a defined dependency requirement filters. @imqueue ecosystem provides a
straightforward way dealing with filters/fields fetched from a user request,
or you can implement it any suitable way, for example having loaders
implementation which directly makes database or key-value storage calls without
the need to create service layer.
Then on query implementation resolver we would act as this:
async function resolve(
source: any,
args: any,
context: any,
info: GraphQLResolveInfo,
) {
const fields = fieldsMap(info);
const user = context.user.find(fromGlobalId(args.id).id);
if (user) {
await Dependency(User).load([user], context, fields);
}
return user;
}
Dependency loader will do all the job calling the minimum amount of requests
required to fill the result data. If end user created a request containing
recursive nesting which falls into fetching process of the same data recursively
it will end up in a data mapping without an additional calls for each nesting
levels. Most of the data are not copied and is mapped by references, so the
memory footprint will be kept on minimal level as well.
License
ISC