Skip to content

Instantly share code, notes, and snippets.

@corzani
Created October 8, 2018 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corzani/aa1b7a3d762237611fcefd2b520871d4 to your computer and use it in GitHub Desktop.
Save corzani/aa1b7a3d762237611fcefd2b520871d4 to your computer and use it in GitHub Desktop.
graphql directive
const {makeExecutableSchema} = require('graphql-tools');
const {graphql} = require('graphql');
// Construct a schema, using GraphQL schema language
const typeDefs = `
directive @upper on FIELD_DEFINITION
type Query {
hello: String @upper
}
`;
// Implement resolvers for out custom Directive
const directiveResolvers = {
upper(
next,
src,
args,
context,
) {
return next().then((str) => {
if (typeof(str) === 'string') {
return str.toUpperCase();
}
return str;
});
},
}
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: (root, args, context) => {
return 'Hello world!';
},
},
};
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
directiveResolvers,
});
const query = `
query UPPER_HELLO {
hello
}
`;
graphql(schema, query).then((result) => console.log('Got result', result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment