Schema Appliances
Intro
Schema Appliances or 'schema extras' are parts of the GraphQL schema that is not a Node. This includes;
Enums
You can use IdioEnum to apply Enumeration types to your GraphQL schema.
const StatusEnum = new IdioEnum({
name: "StatusEnum",
typeDefs: `
enum StatusEnum {
ONLINE
OFFLINE
INACTIVE
}
`,
resolver: {
ONLINE: "online",
OFFLINE: "offline",
INACTIVE: "inactive"
}
});
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
status: StatusEnum
}
...
`,
...
});
const { typeDefs, resolvers } = combineNodes(
[ User ],
{ enums: [ StatusEnum ] }
)
You can encapsulate IdioEnums in a GraphQLNode.
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
status: StatusEnum
}
...
`,
enums: [ StatusEnum ],
...
});
Scalars
You can use IdioScalar to apply Scalar types to your GraphQL schema. A scalar does not require typeDefs
.
You can only specify scalars at combineNodes.
example uses graphql-type-json.
const JSONScalar = new IdioScalar({
name: "JSON",
resolver: GraphQLJSON
});
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
metadata: JSON
}
...
`,
...
});
const { typeDefs, resolvers } = combineNodes(
[ User ],
{ scalars: [ JSONScalar ] }
);
Directives
You can use IdioDirective to apply Directives to your GraphQL schema.
You can only specify directives at combineNodes.
example uses graphql-auth-directives.
const hasScopeDirective = new IdioDirective({
name: "hasScope",
typeDefs: `
directive @hasScope(
scopes: [ String ]!
) on FIELD_DEFINITION
`,
resolver: HasScopeDirective
});
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User ...
type Query {
getUser: User @hasScope(scopes: [ "admin" ])
}
`,
...
});
const { typeDefs, resolvers } = combineNodes(
[ User ],
{ directives: [ hasScopeDirective ] }
);
Interfaces
You can use IdioInterface to apply Interface types to your GraphQL schema.
const PersonInterface = new IdioInterface({
name: "PersonInterface",
typeDefs: `
interface PersonInterface {
eyeColor: String
hairColor: String
}`,
resolver: {
__resolveType(obj) {
if (obj.name) {
return "User";
}
}
}
});
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User implements PersonInterface {
eyeColor: String
hairColor: String
name: String
age: Int
}
type Query {
getUser: PersonInterface
}
`,
...
});
const { typeDefs, resolvers } = combineNodes(
[ User ],
{ interfaces: [ PersonInterface ] }
);
You can encapsulate IdioInterfaces in a GraphQLNode.
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
status: StatusEnum
}
...
`,
interfaces: [ PersonInterface ],
...
});
Unions
You can use IdioUnion to apply Union types to your GraphQL schema.
const UserUnion = new IdioUnion({
name: "UserUnion",
typeDefs: `union UserUnion = User | Admin`,
resolver: {
__resolveType(obj) {
if (obj.admin) {
return "Admin";
}
}
}
});
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User ...
type Query {
getUser: UserUnion
}
`,
...
});
const { typeDefs, resolvers } = combineNodes(
[ User ],
{
unions: [ UserUnion ],
schemaGlobals: `
type Admin {
name: String
age: Int
roles: [ String ]
}
`
}
);
You can encapsulate IdioUnions in a GraphQLNode.
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
status: StatusEnum
}
...
`,
unions: [ UserUnion ],
...
});
Types
You can use GraphQLType to apply Object Types to your GraphQL schema.
const Metadata = new GraphQLType({
name: "Metadata",
typeDefs: `
type Metadata {
lastLogin: String
}
`,
resolvers: {
lastLogin: () => { ... }
}
});
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
metadata: Metadata
}
type Query ...
`,
...
});
const { typeDefs, resolvers } = combineNodes(
[ User ],
{
types: [ Metadata ]
}
);
You can encapsulate GraphQLTypes in a GraphQLNode.
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
metadata: Metadata
}
...
`,
types: [ Metadata ],
...
});
Schema Globals
If you have type definition's that are generic to multiple Node's, you can provide a string or an array of strings to combineNodes where they will be injected into the resulting typeDefs
.
If your Schema Global requires a resolver, you should prefer creating a GraphQLNode.
const User = new GraphQLNode({
name: "User",
typeDefs: `
type User {
name: String
age: Int
timeStamp: TimeStamp
}
...
`,
...
});
const { typeDefs, resolvers } = await combineNodes(
[ User ],
{
schemaGlobals: [
`
type TimeStamp {
updatedAt: String
createdAt: String
}
`
]
}
);