Created
March 13, 2016 09:55
-
-
Save bs1180/13d40a96c2f7c3c9848b to your computer and use it in GitHub Desktop.
How to get an array of requested field names from graphQL query
This file contains hidden or 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
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