graphql-directive-computed-property

GraphQL directive for create computed property

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
graphql-directive-computed-property
30190.1.15 years ago6 years agoMinified + gzip package size for graphql-directive-computed-property in KB

Readme

graphql-directive-computed-property
!Versionversion-badgepackage !downloadsdownloads-badgenpmtrends !PRs Welcomeprs-badgeprs !MIT Licenselicense-badgebuild
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 :tada:
Run yarn test (try --watch flag) for unit tests (we are using Jest)
LICENSE
The MIT License (MIT) 2018 - Luke Czyszczonik -