Last active
March 18, 2019 11:18
-
-
Save timjbray/5710646b3fcf26967e07a2c429605f9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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