Custom graphQL directives for Apollo
npm i apollo-directives
# or
yarn add apollo-directives
renameField, camelCase, capitalize, lowerFirst, toLower, toUpper, trim, upperFirst
Add schemaDirectives
to your server
const { ApolloServer } = require('apollo-server-express')
const { renameField, upperFirst, toUpper, toLower, trim, } = require('apollo-directives')
const server = new ApolloServer({
schemaDirectives: {
renameField,
upperFirst,
toUpper,
toLower,
trim
}
...
context,
typeDefs,
resolvers,
dataSources,
});
Include and use directives
in your typeDefs
const { gql } = require('apollo-server-express');
const typeDefs = gql`
directive @renameField(name: String!) on FIELD_DEFINITION
directive @upperFirst on FIELD_DEFINITION
directive @toLower on FIELD_DEFINITION
directive @trim on FIELD_DEFINITION
directive @toUpper on FIELD_DEFINITION
type Person {
myCustomField: String! @renameField(name: "original_api_field_name")
firstName: String! @upperFirst
lastName: String! @upperFirst
title: String! @toUpper
address: String! @trim
state: String! @toLower
}
`;
Warning: This library is still a WIP. This is a beta version.
Feel free to reach out ~ ferdelamad