Skip to content

graphql-community/graphql-directive-uid

Repository files navigation

graphql-directive-uid

Unique id directive for GraphQL schema.

Version downloads

PRs Welcome MIT License

The problem

Generating unique id for each graphql schema type may be a daunting task.

This solution

The @uid is a GraphQL directive allowing users to generate unique IDs for their queries. All benefits of GraphQL cache with minimum effort.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

use npm:

npm install --save graphql-directive-uid

or yarn :

yarn add graphql-directive-uid

This library has a peerDependencies listing for:

  • graphql
  • graphql-tools

Usage

import { makeExecutableSchema } from 'graphql-tools';
import graphqlDirectiveUid from 'graphql-directive-uid';
import typeDefs from './schema.graphql';
import resolvers from './resolvers';

export default makeExecutableSchema({
  typeDefs,
  resolvers,
  schemaDirectives: {
    uid: graphqlDirectiveUid, // the name of directive can be as you like
  },
});

GraphQL schema:

type Person @uid(from: ["personId", "name"]) {
  personId: Int
  name: String
}

type Query {
  persons: [Person]
}

GraphQL query:

query getPersons {
  persons {
    uid
    personId
    name
  }
}

Examples

You'll find examples of how to use it in the test directory.

Feel free to contribute more!

LICENSE

MIT