Created
October 8, 2018 21:51
-
-
Save corzani/aa1b7a3d762237611fcefd2b520871d4 to your computer and use it in GitHub Desktop.
graphql directive
This file contains 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
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