Skip to content

Instantly share code, notes, and snippets.

@timjbray
Last active March 18, 2019 11:18
Show Gist options
  • Save timjbray/5710646b3fcf26967e07a2c429605f9a to your computer and use it in GitHub Desktop.
Save timjbray/5710646b3fcf26967e07a2c429605f9a to your computer and use it in GitHub Desktop.
// define the graphql query
const UserQueryDefs = gql`
extend type Query {
user(id: ID): User
}
`;
// graphql resolver
async user(_, args, context): Promise<any> {
// return the requested user
return getRepository(User).findOne(args.id);
}
// graphql resolver which only selects
async user(_, args, context): Promise<any> {
// get an array of selected fields
const selectedFields = info.fieldNodes[0].selectionSet.selections.map((field: any) => field.name.value);
// return the requested user - with only the selected fields
return getRepository(User).findOne({
select: selectedFields,
where: { id: args.id }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment