在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):lirown/graphql-custom-directives开源软件地址(OpenSource Url):https://github.com/lirown/graphql-custom-directives开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):graphql-custom-directivesA collection of custom graphql directives created with Moment, Lodash and Numeral-js. Checkout graphql-custom-directive for creating your own graphql custom directives. Install
Usageimport {
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLTemplateDirective,
GraphQLPhoneDirective,
applySchemaCustomDirectives,
} from 'graphql-custom-directives';
const query = new GraphQLObjectType({
name: 'Query',
fields: {
input: {
type: GraphQLString,
args: {
value: {
type: GraphQLString
}
},
resolve: (source, {value}) => value
}
}
});
const schema = new GraphQLSchema({
directives: [
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLPhoneDirective,
GraphQLTemplateDirective
],
query
});
applySchemaCustomDirectives(schema);
graphql(schema, `{ input(value: "test") @upperCase }`)
.then(({ result, errors }) => {
console.log(result); // will print { input: "TEST }
}); Using with apollois also possible import { makeExecutableSchema } from 'graphql-tools';
import {
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLTemplateDirective,
GraphQLPhoneDirective
applySchemaCustomDirectives
} from 'graphql-custom-directives';
let directives = [
GraphQLDateDirective,
GraphQLNumberDirective,
GraphQLCurrencyDirective,
GraphQLLowerCaseDirective,
GraphQLUpperCaseDirective,
GraphQLCamelCaseDirective,
GraphQLStartCaseDirective,
GraphQLCapitalizeDirective,
GraphQLKebabCaseDirective,
GraphQLTrimDirective,
GraphQLDefaultToDirective,
GraphQLToLowerDirective,
GraphQLToUpperDirective,
GraphQLTemplateDirective,
GraphQLPhoneDirective
]
let schema = makeExecutableSchema(...);
schema._directives.push.apply(schema._directives, directives);
applySchemaCustomDirectives(schema); Date formatting directivesAdding date directive to graphql query for formatting the result using Moment.
query {
input(value: "2016-01-01T00:00:00") @date
}
// => { input: "01 Jan 2016 00:00" }
query {
input(value: "2016-01-01T00:00:00") @date(as:"YYYY")
}
// => { input: "2016" }
query {
input(value: "${(new Date).toISOString()}") @date(as:"days ago")
}
// => { input: "0 days ago" } Number formatting directivesAdding number directive to graphql query for formatting the result using Numeral-js.
query {
input(value: "1500.404") @number
}
// => { input: "1,500" }
query {
input(value: "-1500.404") @number(as:"(0,0.00)")
}
// => { input: "(1,500.40)" }
query {
input(value: "1500") @currency
}
// => { input: "$1,500)" } String formatting directivesAdding string directive to graphql query for formatting the result using Lodash.
query {
input(value: "FOO BAR") @lowerCase
}
// => { input: "foo bar" }
query {
input(value: "foo bar") @upperCase
}
// => { input: "FOO BAR" }
query {
input(value: "foo bar") @camelCase
}
// => { input: "fooBar" }
query {
input(value: "foo bar") @startCase
}
// => { input: "Foo Bar" }
query {
input(value: "foo bar") @capitalize
}
// => { input: "Foo var" }
query {
input(value: "foo bar") @kebabCase
}
// => { input: "foo-bar" }
query {
input(value: " foo bar ") @trim
}
// => { input: "foo bar" }
query {
input @default(to:"N/A")
}
// => { input: "N/A" }
query {
input(value: "FOO BAR") @toLower
}
// => { input: "foo bar" }
query {
input(value: "foo bar") @toUpper
}
// => { input: "FOO BAR" }
query {
input(value: "foo bar") @template(as:"${input} ${toUpper(input)}")
}
// => { input: "foo bar FOO BAR" }
query {
input(value: " foo bar ") @trim @template(as:"${input} ${input}") @toUpper
}
// => { input: "FOO BAR FOO BAR" } Phone formatting directivesAdding phone directive to graphql query for formatting the result using libphonenumber-js.
query {
input(value: "+12133734253") @phone
}
// => { input: "+1 213 373 4253" }
query {
input(value: "+12133734253") @phone(as: "national")
}
// => { input: "(213) 373-4253" }
query {
input(value: "+12133734253") @phone(as: "RFC3966")
}
// => { input: "tel:+12133734253" } // URI format License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论