GraphQL queries — examples for begginers
1 min readJun 1, 2020
No words, only code from dummy NestJS app.
@Query(returns => ClientDto, { nullable: true, name: ‘getClient’ })
async getClient(@Args(‘id’) id: string) {
return this.clientService.findById(id);
}@Mutation(returns => ClientDto, { name: 'createClient' })
async createClient(@Args('client') client: ClientInputDto) {
return this.clientService.create(client as ClientEntity);
}@Mutation(returns => ClientDto, { name: 'updateClient' })
async updateClient(
@Args('id') id: string,
@Args('client') client: ClientInputDto,
) {
return this.clientService.update(id, client);
}
schema.gql (code first)
type ClientDto {
name: String!
signUpDate: DateTime!
}"""
A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format.
"""scalar DateTimetype Query {
client(id: String!): ClientDto
}type Mutation {
createClient(client: ClientInputDto!): ClientDto!
updateClient(client: ClientInputDto!, id: String!): ClientDto!
}input ClientInputDto {
name: String!
}
mutation {
createClient(client: {name: "Olena"}) {name, signUpDate}
}mutation {
updateClient(
client: {name: "Olena Yedyharova"},
id: "5ed551240592ba0569aa2751") {name, signUpDate}
}query {
client(id: "5ed551240592ba0569aa2751") {name, signUpDate}
}
just a pic