Skip to content

Instantly share code, notes, and snippets.

@ryanclark
Created May 20, 2020 06:05
Show Gist options
  • Save ryanclark/4accd07af178ef8dbe69aca11510da3d to your computer and use it in GitHub Desktop.
Save ryanclark/4accd07af178ef8dbe69aca11510da3d to your computer and use it in GitHub Desktop.
@Controller()
export class BuildController {
constructor(
private entityManager: EntityManager,
private permissions: Permissions,
private user: User
) {}
@Query()
async build(args: BuildArgs, info: GraphQLResolveInfo) {
this.permissions.check(BuildScope.Read);
// recursively get the requested fields (allows for fragments)
const nodes = graphqlFields(info) as FieldsObject;
// match the fields requested to the fields available on the Build entity
const fields = getMatchingFields(Build, nodes);
let query = this.entityManager
.getRepository(Build)
.createQueryBuilder('build')
.select(fields.map((field) => `build.${field}`));
// builds can be queried by their database ID or pipeline name and build ID
if (args.id) {
const { id } = fromGlobalId(args.id);
query = query.where('build.id = :id', { id: parseInt(id, 10) });
} else {
query = query.where(
'build.jobName = :jobName AND build.buildNumber = :buildNumber',
{ jobName: args.jobName, buildNumber: args.buildNumber }
);
}
// we get the raw data back from the DB, so convert into an entity object
return getNormalObject(await query.getRawOne());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment