Creating Nodes
Intro
A "Node" GraphQLNode is designed to encapsulate a ObjectTypeDefinition
together with its related resolvers & properties. Lets create a User
Node.
const User = new GraphQLNode({
name: "User"
});
TypeDefs
Here is the Schema Definition Language (SDL)
for our Node.
type User {
name: String
age: Int
}
type Query {
getUser: User
}
SDL can be referred to as typeDefs
.
You can use filePaths, strings & even an AST when specifying
typeDefs
.
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
}
type Query {
getUser: User
}
`
});
Resolvers
A Node allows you to specify the following resolvers;
Query
Mutation
Subscription
Fields
Let's implement the getUser
resolver...
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User ...
type Query {
getUser: User
}
`,
resolvers: {
Query: {
getUser: (root, args, ctx) => { ... }
}
}
});
{
typeDefs: `
type User ...
type Mutation {
likeUser: User
}
`,
resolvers: {
Mutation: {
likeUser: (root, args, ctx) => { ... }
}
}
}
{
typeDefs: `
type User {
name: String
posts: [Post]
}
...
`,
resolvers: {
Fields: {
posts: (root, args, ctx) => { ... }
}
}
}
Subscriptions must return a 'subscribe' property that resolves to an async iterator!
{
typeDefs: `
type User ...
type Subscription {
userUpdate: User
}
`,
resolvers: {
Subscription: {
userUpdate: {
subscribe: (root, args, ctx) => asyncIterator()
}
}
}
}
Injections
It's recommended to inject dependencies into your Node, this helps with testing. For each resolver injections will be a property on the context
parameter.
new GraphQLNode({
injections: { ... }
})
const User = new GraphQLNode({
name: "User",
typeDefs: "./User.gql",
injections: {
UserModel
},
resolvers: {
Query: {
getUser: (root, args, context) => {
return context.injections.UserModel.findOne()
}
}
}
});
Nodes
You can recursively nest nodes to reflect domains & work with architectural constraints.
const Comment = new GraphQLNode({
name: "Comment",
...
});
const Post = new GraphQLNode({
name: "Post",
nodes: [ Comment ],
...
});
const User = new GraphQLNode({
name: "User",
nodes: [ Post ]
...
});
Enums
You can encapsulate enums within a Node. Checkout the Schema Appliances guide and the API for IdioEnum.
const StatusEnum = new IdioEnum({
name: "StatusEnum",
...
});
const User = new GraphQLNode({
name: "User",
enums: [ StatusEnum ],
...
});
Interfaces
You can encapsulate interfaces within a Node. Checkout the Schema Appliances guide and the API for IdioInterface.
const PersonInterface = new IdioInterface({
name: "PersonInterface",
...
});
const User = new GraphQLNode({
name: "User",
interfaces: [ PersonInterface ],
...
});
Unions
You can encapsulate unions within a Node. Checkout the Schema Appliances guide and the API for IdioUnion.
const UserUnion = new IdioUnion({
name: "UserUnion",
...
});
const User = new GraphQLNode({
name: "User",
unions: [ UserUnion ],
...
});
Types
You can encapsulate types within a Node. Checkout the Schema Appliances guide and the API for GraphQLType.
const Metadata = new GraphQLType({
name: "Metadata",
typeDefs: `
type Metadata {
lastLogin: String
}
`,
resolvers: {
lastLogin: () => { ... }
}
});
const User = new GraphQLNode({
name: "User",
types: [ Metadata ],
...
});