Skip to content

ekampf/graphene-custom-directives

Repository files navigation

Graphene Custom Directives

https://coveralls.io/repos/ekampf/graphene-custom-directives/badge.svg?branch=master&service=github

A collection of custom GraphQL directives for (Graphene)[https://github.com/graphql-python/graphene]

Number Formatting

  • Floor
query {
    stringValue(value: "3.14") @floor
    floatValue(value: 3.14) @floor
}

// => { stringValue: "3.0", floatValue: 3.0 }
  • Ceil
query {
    stringValue(value: "3.14") @ceil
    floatValue(value: 3.14) @ceil
}

// => { stringValue: "4.0", floatValue: 4.0 }

String formatting directives

  • Default value:
query {
    input @default(to: "YEAH")
}

// => { input: "YEAH" }
  • Base64 encode
query {
    stringValue(value: "YES") @base64
}

// => { stringValue: "Tm9uZQ==" }
  • Uppercase:
query {
    input(value: "FOO BAR") @lowercase
}

// => { input: "foo bar" }
  • Lowercase:
query {
    input(value: "foo BAR") @uppercase
}

// => { input: "FOO BAR" }
  • Capitalize:
query {
    input(value: "foo BAR") @capitalize
}

// => { input: "Foo Bar" }
  • Number formatting
query {
    stringValue(value: "3.14") @number(as: "0.4f")
}

// => { stringValue: "3.1400" }
  • Currency formatting
query {
    stringValue(value: "150000") @currency
}

// => { stringValue: "$150,000" }

List Directives

  • Sample
query {
    listValue(value: [1, 2, 3]) @sample(k: 1)
}

// => { listValue: [2] }
  • Shuffle
query {
    listValue(value: [1, 2, 3]) @shuffle
}

// => { listValue: [2, 1, 3] }

Misc.

You can also use multiple directives, executed from left to right:

query {
    stringValue(value: "3.14") @ceil @number(as: "0.4f")
}

// => { stringValue: "4.0000" }

About

Custom GraphQL Directives for Graphene (Python)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published