Skip to content

graphql-community/graphql-directive-computed-property

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-directive-computed-property

Version downloads PRs Welcome MIT License

Introduction

The directive allows creating a computed property from fields where is defined.

Table of Contents

Installation

yarn add graphql-directive-computed-property

This package requires graphql and graphql-tools as peer dependency

Usage

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 Parameters

Directive params:

value: String

The calculated value. It can contain other fields from the type in which it is defined.

Example:

@computed(value: "$firstName $lastName")

@computed(value: "$price $")

Contributing

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)

LICENSE

The MIT License (MIT) 2018 - Luke Czyszczonik - mailto:lukasz.czyszczonik@gmail.com