This API lets you define your own schema types to have persisted fields.
Gridsome generates the GraphQL schema for metadata and collections based on the data which is discovered on startup. That's great for simple projects but will often lead to errors with for example missing fields because content has been removed in an external source. This API lets you define your own schema types to have persisted fields.
The Schema API can be used in the loadSource
and createSchema
hooks.
string | array
Required.Schema types can be added as an SDL string or by using the factory methods. Types for collections must implement the Node
interface. And Gridsome will not infer field types for custom fields unless the @infer
directive is used.
api.loadSource(({ addSchemaTypes }) => {
addSchemaTypes(`
type Post implements Node {
title: String
}
`)
})
api.loadSource(({ addSchemaTypes, schema }) => {
addSchemaTypes([
schema.createObjectType({
name: 'Post',
interfaces: ['Node'],
fields: {
title: 'String'
}
})
])
})
Read more about Schemas and Types
object
Required.Resolvers are methods that are executed on each field in the query. The default resolvers for types like String
or Int
simply return the value without any modifications. Resolvers for fields that are referencing another node are interacting with the internal store to return data from the requested node.
Use the addSchemaResolvers()
action to add new fields or override existing fields. The resolver method will receive four arguments that can be used:
addSchemaResolvers({
TypeName: {
fieldName(obj, args, context, info) { ... }
}
})
obj
The results from the resolver on the parent field.args
An object with arguments from the query.context
An object with references to the internal store etc.info
Information about the execution state of the query.Read more about GraphQL resolvers
This example adds a new fullName
field on the User
type which merges two fields:
api.loadSource(({ addSchemaResolvers }) => {
addSchemaResolvers({
User: {
fullName(obj) {
return `${obj.firstName} ${obj.lastName}`
}
}
})
})
This example adds a title
field on the Post
type with an argument to uppercase the returned value:
api.loadSource(({ addSchemaResolvers }) => {
addSchemaResolvers({
Post: {
title: {
args: {
uppercased: 'Boolean'
},
resolve(obj, args) {
if (args.uppercased) {
return obj.title.toUpperCase()
}
return obj.title
}
}
}
})
})
Then in a query, use the argument to get an uppercased title:
query {
post {
title(uppercased: true)
}
}
GraphQLSchema
Required.Add a custom GraphQL schema that will be merged with the internal schema.
const { GraphQLSchema } = require('gridsome/graphql')
api.loadSource(({ addSchema }) => {
addSchema(new GraphQLSchema({
// ...
}))
})
Read more about GraphQL schemas
Custom schemas for meta data and collections will not infer any more field types from the source by default. Add the @infer
extension to add discovered fields that aren't defined in the custom schema.
type Post implements Node @infer {
title: String
}
type Metadata @infer {
siteName: String
}
Field extensions extends the default resolvers.
The @proxy
extension can be used to return a value from another field.
type Post implements Node {
userName: String @proxy(from: "user_name")
}
type Metadata {
siteName: String @proxy(from: "other.field")
}
Fields of another node type will lookup the node by default, but no arguments will be inherited from the internal resolvers. Use
type Post implements Node {
author: Author @reference(by: "slug")
}
object
Required.string
Required.object
Required.object
string[]
object
Required.string
Required.string[]
Required.object
Required.string
Required.object
Required.object
Required.string
Required.object
Required.object
Required.string
Required.object
Required.type Post implements Node {
title: String
publishedAt: Date
author: Author
content: String
}
type Author implements Node {
name: String
picture: Image
description: String
}
Custom schema types for a collection must implement the Node
interface.
type Metadata {
siteName: String
}