Unique id
directive for GraphQL schema.
Generating unique id
for each graphql schema type may be a daunting task.
The @uid
is a GraphQL directive allowing users to generate unique IDs for their queries. All benefits of GraphQL cache with minimum effort.
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
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
}
}
You'll find examples of how to use it in the test directory.
Feel free to contribute more!
MIT