The document discusses best practices and challenges for building GraphQL applications. It recommends using tools like TypeScript, TypeORM, and TypeGraphQL to generate schemas from database models to avoid duplication. It also suggests using DataLoader to optimize requests, implementing pagination on the data layer, and using info and context parameters to only select needed fields. The document covers other topics like authorization, versioning, testing, and avoiding overfetching with info.
2. Nikita
Galkin
Love and Know:
â° How to make developers and business happy
â° Technical and process debt elimination
Believe that:
â° Any problem must be solved at the right level
â° Software is easy. People are hard
â° A problem should be highlighted, an idea should
be "sold", a solution should be demonstrated
Links:
Site GitHub Twitter Facebook
2
3. GraphQL is
an open source
query language
created by
Facebook
The specification
published in June 2018
20. GraphQL Schema Definition Language:
â A type has a name and can implement
one or more interfaces
â A field has a name and a type
â The built-in scalar types are
Int/Float/String/Boolean/ID
â Enum is a scalar value that has a
specified set of possible values
21.
22. I love to look in
the mirrors
Worse than D.R.Y.,
DUPLICATION at:
â Schema
â DB tables
â Endpoints
Common solution:
â Schema generation
30. I have Big
Latency, perhaps
Reasons:
â Poor Performance
â Too many DB
queries
â Client cache only
Common solution:
â Dataloader
31. Precomputed data
You store data for resolvers
in NoSQL DB with structure
similar GraphQL schema.
Limitations:
â You have data ownership
32. We need data, but there
are not decorators for
such source type. For
example:
â Elastic Search
â Data from external API
â etc
I miss
something...
33. // class
class User {
constructor(name, surname) {
this.name = name;
this.surname = surname;
}
}
// class instance
new User('Bart', 'Simpson');
// plain (literal) object ==============>
const hero = {
name: 'Gomer',
surname: 'Simpson'
}; POM
34. import { plainToClass } from 'class-transformer';
fetch("users.json").then((users: Object[]) => {
const realUsers = plainToClass(User, users);
// now each user in realUsers is instance of User class
});
const hero = {
name: 'Gomer',
surname: 'Simpson'
};
hero instanceof User; // false
36. Reasons:
â SELECT all, because
pagination and
sorting implemented
in the resolver
â SELECT * FROM ...
â JOIN is used
I have Big
Requests, Dear!
39. GraphQL Resolver function signature:
fieldName(obj, args, context, info)
â obj: The object that contains the result
returned from the resolver on the parent field
â args: An object with the arguments passed
into the field in the query
â context: This is an object shared by all
resolvers
â info: AST, state of resolving. Example
40. import { GraphQLResolveInfo } from 'graphql';
import graphqlFields from 'graphql-fields';
import { Ctx, Info, Query, Resolver } from 'type-graphql';
import { GraphQLContext } from '~/boundaries/graphql';
import { UsersRepository } from '~/repositories';
import { User } from '~/entities';
@Resolver(type => User)
export class UserResolver {
@Query(type => User)
async currentUser (
@Ctx() context: GraphQLContext,
@Info() info: GraphQLResolveInfo
): Promise<User> {
const fields = graphqlFields(info);
return UsersRepository.find(
{ where: { id: context.user.id } },
{ select: Object.keys(fields) }
);
}
}