Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/edujugon/graphql-directive-resolve-as
Graphql directive to resolve fields as different prop names of the belonging object
https://github.com/edujugon/graphql-directive-resolve-as
graphql graphql-directive
Last synced: 16 days ago
JSON representation
Graphql directive to resolve fields as different prop names of the belonging object
- Host: GitHub
- URL: https://github.com/edujugon/graphql-directive-resolve-as
- Owner: Edujugon
- License: mit
- Created: 2018-10-13T15:57:48.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T17:13:04.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T10:44:29.541Z (about 1 month ago)
- Topics: graphql, graphql-directive
- Language: JavaScript
- Size: 2.55 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# graphql-directive-resolve-as
[![Version][version-badge]][package]
[![downloads][downloads-badge]][npmtrends]
[![PRs Welcome][prs-badge]][prs]
![MIT License][license-badge]# Introduction
Graphql directive to resolve fields as different prop names of the belonging object.
This directive helps you not to declare resolvers over and over
just because the name you want to expose is different to the prop name.It allows resolution for nested objects, converting a string in dot notation into an object reference.
# Table of Contents
* [Introduction](#introduction)
* [Installation](#installation)
* [Usage](#Usage)
* [Parameters](#directive-parameters)# Installation
```
yarn add graphql-directive-resolve-as
```
or
```
npm i graphql-directive-resolve-as
```# Usage
```js
const { resolveAs } = require('graphql-directive-resolve-as');const schema = makeExecutableSchema({
typeDefs,
resolvers,
schemaDirectives: {
resolveAs,
...
},
});
```then you can use it in your fields like this:
```js
const typeDefs = `
type User {
name: String @resolve_as(name: "firstName")
lastName: String,
city: String @resolveAs(name: "country.city.name")
firstFriend: String @resolveAs(name: "friends.0.name")
}
type Query {
me: User
}
`;
````me` query resolver could be something like this:
```js
const resolvers = {
Query: {
me: () => ({
firstName: 'John',
lastName: 'Doe',
friends: [
{
name: 'Edu'
},
{
name: 'Natalia'
}
],
country: {
name:'Germany',
city: {
name:'Berlin',
neighborhood: {
name: "Kreuzberg"
}
}
}
}),
},
};
```> if you use [`graphql-import`](https://github.com/prismagraphql/graphql-import) then you need to add this definition on top of the schema:
```graphql
directive @resolveAs(name: String) on FIELD_DEFINITION
```# Directive Parameters
`name` = Path as string in dot notation to the object property to be resolved.
## Happy coding :tada:
[npm]: https://www.npmjs.com/
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/travis/edujugon/graphql-directive-resolve-as.svg?style=flat-square
[build]: https://travis-ci.org/edujugon/graphql-directive-resolve-as
[version-badge]: https://img.shields.io/npm/v/graphql-directive-resolve-as.svg?style=flat-square
[package]: https://www.npmjs.com/package/graphql-directive-resolve-as
[downloads-badge]: https://img.shields.io/npm/dm/graphql-directive-resolve-as.svg?style=flat-square
[npmtrends]: http://www.npmtrends.com/graphql-directive-resolve-as
[license-badge]: https://img.shields.io/npm/l/graphql-directive-resolve-as.svg?style=flat-square
[license]: https://github.com/edujugon/graphql-directive-resolve-as/blob/master/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com