Metanoia
A set of TypeScript decorators for defining a GraphQL schema directly out of your TypeScript class definitions.
Installation
npm install metanoia
Usage
First, let's define a GraphQL interface:
; // Create a new Schema builder, this is the centrepiece of this library.;
Now we that we've defined our interface, we can define a type that implements it!
// Define an enum for user roles. // We have to use this method to decorate it, since TypeScript does// not allow decorators on enums yet.builder.decorateEnumUserRoleEnum, ;
Next, let's try our hand at defining a subscription!
; ; // This type will hold all the information we have about a chat message.
We're almost done! We have everything properly defined.
Now let's do the last step and build the schema!
; returns an instance of GraphQLSchema console.logprintSchemaschema;
At this junction our schema will be printed to the console, and it will look like this:
schema { query: Query mutation: Mutation subscription: Subscriptions} """A chat message. """type ChatMessage implements Node { """Base type of this Node.""" kind: String! """A unique ID for this object.""" id: ID! text: String! user: User room: String!} """The root mutation object."""type Mutation { """Ends the current session.""" logout: Boolean} interface Node { """Base type of this Node.""" kind: String! """A unique ID for this object.""" id: ID!} """The root query object."""type Query { """Get the currently logged in user.""" currentUser: User} """The root subscriptions object."""type Subscriptions { """Fired when a user sends a message.""" chatMessage(room: String = "lobby"): ChatMessage} """A user."""type User implements Node { """Base type of this Node.""" kind: String! """A unique ID for this object.""" id: ID! firstName: String! lastName: String! """The role of this user.""" role: UserRole!} """A user's role."""enum UserRole { """An administrator.""" administrator """A moderator.""" moderator """A subscriber.""" subscriber}
At this point you can use the schema as normal.
graphqlschema, ` query testQuery { currentUser { id firstName lastName role } }`.thenconsole.log, console.error;
What about GraphQL Unions?
GraphQL unions are not currently supported in version 1.0.0
.
I might decide to add this feature later if there's enough demand for it, or if somebody else submits a pull request! That's always welcome!
Contributions
All contributions and pull requests are welcome.
Please make sure that test coverage does not drop below the set limits in package.json
.
License (MIT)
Copyright (c) 2018 Abdullah A. Hassan
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.