Graphql-info-parser helps you to take advantage of the info argument. It transforms the info object into an understandable data structure, with all the necessary information of the query. (Directives, Args). This way, you can for example generate an SQL request from the root resolver.
- Directives ok
- Args ok
- fragments ok
- interface TODO
- union TODO
- extends
npm install --save graphql-info-parser
yarn add graphql-info-parser
import { infoParser } from 'graphql-info-parser';
const Query = {
users: (parents, args, ctx, info) => {
const obj = infoParser(info);
},
};
- schema.graphql
type User {
firstName: String
lastName: String
last_name: String @deprecated
online: Boolean
friends(limit: Int!): [User]
}
type Query {
users: [User]
}
- query
query {
users {
firstName
last_name
friends(limit: 5) {
firstName
}
}
}
- resolver
import { infoParser } from 'graphql-info-parser';
const Query = {
users: (parents, args, ctx, info) => {
const obj = infoParser(info);
},
};
- Result:
obj = {
name: 'users',
type: 'User',
isList: true,
args: {},
directivesObject: {},
fields: [{
name: 'firstName',
type: 'String',
isList: false,
args: {},
directivesObject: {},
directivesField: {},
},
last_name: {
name: 'last_name',
type: 'String',
isList: false,
args: {},
directivesObject: {},
directivesField: { deprecated: {} },
},
friends: {
name: 'friends',
type: 'User',
isList: true,
args: { limit: 5 },
directivesObject: {},
directivesField: {},
fields: [{
name: 'firstName',
type: 'String',
isList: false,
args: {},
directivesObject: {},
directivesField: {},
}]
},
]
};