What to Expect
GraphQL Schema Definition Language (SDL)
Why I Began to Use GraphQL
Schema First Design (SFD)
Define the Schema first and the rest will follow
SFD with GraphQL
Conference
, Contact
, Location
, Theme
)name
, description
)Types as Classes
Conference.ts
...
@ObjectType()
export class Conference {
@Field(_ => ID)
public id!: string;
@Field(_ => String)
public name!: string;
@Field(_ => Contact)
public organizer!: Contact;
...
}
Generated schema:
type Conference {
id: ID!
name: String!
organizer: Contact!
...
}
GraphQL/TypeScript definition at the same time!
Resolvers as Classes
ConferenceResolver.ts
...
@Resolver(_ => Conference)
class ConferenceResolver {
@Query(_ => Conference)
public conference(
@Arg("id", _ => ID) id: string
) {
return getConference(id);
}
...
}
Generated schema:
type Query {
conference(id: ID!): Conference
}
Resolve to data here (async possible too)
What else?
Why I use TypeGraphQL
Is it possible to use GraphQL and TypeScript type systems to generate queries?
Spoiler: Yes
React Demo
Schedule.tsx
interface ScheduleProps {
theme: Theme;
intervals: Array<{
begin: Interval["begin"];
end: Interval["end"];
sessions: Array<{
title: Session["title"];
type: Session["type"];
}>;
}>;
}
Schedule.tsx
function Schedule({
theme, intervals
}: ScheduleProps) {
...
}
The types point to our schema (source of truth for GraphQL)
Approach
connected(Schedule)
finds the component by name and constructs the related queriesTo work, the system expects to find matching root queries (i.e.
theme
andintervals
in this case)
Constraints
Pros and Cons
Pros:
Cons: