Ga naar hoofdinhoud

GraphQL API

Ever Gauzy provides a GraphQL API alongside the REST API, offering a flexible, type-safe query language for frontend clients.

info

The GraphQL API is currently a Work-In-Progress (WIP). The REST API is the primary production API. GraphQL coverage is being expanded incrementally.

Endpointโ€‹

EnvironmentGraphQL EndpointGraphQL Playground
Localhttp://localhost:3000/graphqlhttp://localhost:3000/graphql
Demohttps://apidemo.gauzy.co/graphqlhttps://apidemo.gauzy.co/graphql

Architectureโ€‹

The GraphQL layer is built with @nestjs/graphql (Apollo Server):

Code-First Approachโ€‹

Gauzy uses the code-first approach where GraphQL schema is generated from TypeScript classes:

// Object Type
@ObjectType()
export class Employee {
@Field(() => ID)
id: string;

@Field()
firstName: string;

@Field()
lastName: string;

@Field({ nullable: true })
startedWorkOn?: Date;
}

// Resolver
@Resolver(() => Employee)
export class EmployeeResolver {
constructor(private readonly employeeService: EmployeeService) {}

@Query(() => [Employee])
async employees(): Promise<Employee[]> {
return this.employeeService.findAll();
}

@Mutation(() => Employee)
async createEmployee(
@Args("input") input: CreateEmployeeInput,
): Promise<Employee> {
return this.employeeService.create(input);
}
}

Example Queriesโ€‹

Query Employeesโ€‹

query {
employees {
id
firstName
lastName
startedWorkOn
user {
email
}
organization {
name
}
}
}

Query with Variablesโ€‹

query GetEmployee($id: ID!) {
employee(id: $id) {
id
firstName
lastName
skills {
name
}
}
}

Mutationโ€‹

mutation CreateEmployee($input: CreateEmployeeInput!) {
createEmployee(input: $input) {
id
firstName
lastName
}
}

Authenticationโ€‹

GraphQL requests require the same JWT authentication as REST:

POST /graphql
Authorization: Bearer {jwt_token}
Content-Type: application/json

{
"query": "query { employees { id firstName } }"
}

GraphQL Playgroundโ€‹

When running in development mode, the GraphQL Playground is available at the /graphql endpoint. It provides:

  • Interactive query editor
  • Schema exploration
  • Auto-completion
  • Documentation browser
  • Request history

Configurationโ€‹

// app.module.ts
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: true, // Code-first schema generation
playground: process.env.NODE_ENV !== "production",
introspection: true,
context: ({ req, res }) => ({ req, res }),
});

REST vs GraphQLโ€‹

FeatureRESTGraphQL
Maturityโœ… Production-readyโš ๏ธ Work-in-progress
Coverageโœ… All endpointsโš ๏ธ Partial coverage
Documentationโœ… Swagger UIโœ… Playground
Data fetchingFixed response shapeClient-defined response shape
Over-fetchingPossibleEliminated
Under-fetchingMultiple requests neededSingle request
CachingHTTP cachingApollo Client caching