Skip to content

Instantly share code, notes, and snippets.

@bs1180
Created March 13, 2016 09:55
Show Gist options
  • Save bs1180/13d40a96c2f7c3c9848b to your computer and use it in GitHub Desktop.
Save bs1180/13d40a96c2f7c3c9848b to your computer and use it in GitHub Desktop.
How to get an array of requested field names from graphQL query
import { simplifyAST } from 'graphql-sequelize'
const parseFields = (ast) => {
let s = simplifyAST(ast[0])
return Object.keys(s.fields)
}
// and then in use:
accounts: {
type: new GraphQLList(accountType),
description: 'Accounts belonging to this user',
resolve: (user, _, { rootValue, fieldASTs }) => rootValue.actions.getAccountsForUser(user.id, parseFields(fieldASTs))
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment