New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

graphql-directive-resolve-as

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-directive-resolve-as

Graphql directive to resolve fields as different prop names of the belonging object

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
50
increased by16.28%
Maintainers
1
Weekly downloads
 
Created
Source

graphql-directive-resolve-as

Version downloads PRs Welcome MIT License

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

Installation

yarn add graphql-directive-resolve-as

or

npm i graphql-directive-resolve-as

Usage

const { resolveAs } = require('graphql-directive-resolve-as');

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  schemaDirectives: {
    resolveAs,
    ...
  },
});

then you can use it in your fields like this:

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:

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 then you need to add this definition on top of the schema:

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:

Keywords

FAQs

Package last updated on 15 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc