The directive allows creating a computed property from fields where is defined.
yarn add graphql-directive-computed-property
This package requires graphql and graphql-tools as peer dependency
const { makeExecutableSchema } = require('graphql-tools');
const computedDirective = require('graphql-directive-computed-property');
const typeDefs = `
type User {
firstName: String
lastName: String
fullName: String @computed(value: "$firstName $lastName")
}
type Query {
me: User
}
`;
const resolvers = {
Query: {
me: () => ({
firstName: 'John',
lastName: 'Doe',
}),
},
};
module.exports = makeExecutableSchema({
typeDefs,
resolvers,
schemaDirectives: {
computed: computedDirective,
},
});
Query:
query {
me {
fullName
}
}
Result:
{
fullName: 'John Doe'
}
Computed property work well with other directives like @rest:
Example:
admin: String @rest(url: "${URL_TO_API}") @computed(value: "Are you admin? $admin")
Directive params:
The calculated value. It can contain other fields from the type in which it is defined.
Example:
@computed(value: "$firstName $lastName")
@computed(value: "$price $")
I would love to see your contribution. ❤️
For local development (and testing), all you have to do is to run yarn
and then yarn dev
. This will start the Apollo server and you are ready to contribute 🎉
Run yarn test (try --watch
flag) for unit tests (we are using Jest)
The MIT License (MIT) 2018 - Luke Czyszczonik - mailto:lukasz.czyszczonik@gmail.com