Version: 0.15.x

CRUD Overview

Graphback generates a CRUD API for each model in your GraphQL schema. This gives you queries, mutations and subscriptions that can be instantly be used perform queries and mutations on your data.

Graphback provides an implementation of GraphQLCRUD for a standardised CRUD API using common data access patterns.

Queries, mutations and subscriptions that you add to your model files will be included in the generated schema. These require manual implementation and merging with the resolvers generated by Graphback.

Your custom operations can utilize the input types and filters created by Graphback. This lets you control the schema as you would when building it from scratch, while significantly reducing the amount of boilerplate required to set up it from your data.

To tell Graphback to create a CRUD API for your type, add a @model annotation.

""" @model """
type Note {
id: ID!
title: String!
description: String
likes: Int
}

This will add queries, mutations and subscriptions to the schema for Note. It will also create corresponding CRUD resolvers which are configured to access the data from your data source.

""" @model """
type Note {
id: ID!
title: String!
description: String
likes: Int
}
type Query {
getNote(id: ID!): Note
findNotes(filter: NoteFilter, page: PageRequest, orderBy: OrderByInput): NoteResultList!
}
type Mutation {
createNote(input: CreateNoteInput!): Note
updateNote(input: MutateNoteInput!): Note
deleteNote(input: MutateNoteInput!): Note
}
type Subscription {
newNote(filter: NoteSubscriptionFilter): Note!
updatedNote(filter: NoteSubscriptionFilter): Note!
deletedNote(filter: NoteSubscriptionFilter): Note!
}
caution

Graphback will override custom queries, mutations and subscriptions from model files which have the same name as Graphback CRUD queries, mutations and subscriptions. Either change the name of your custom definitions or disable generation of the CRUD operation(s) for the model(s).

Configuration#

Graphback can be configured to enable/disable CRUD generation per GraphQL type, or globally across the entire application.

See the table below for the full list of CRUD configuration options that can be applied to the application or models.

OptionDescriptionDefault
findSpecifies whether a find<Type>s findBy query should be generated.true
findOneSpecifies whether a get<Type> findOne query should be generated.true
createSpecifies whether a create<Type> query should be generated.true
updateSpecifies whether a update<Type> update mutation should be generated.true
deleteSpecifies whether a delete<Type> delete mutation should be generated.true
subCreateSpecifies whether a new<Type> subscription should be generated.true
subUpdateSpecifies whether a updated<Type> subscription should be generated.true
subDeleteSpecifies whether a deleted<Type> subscription should be generated.true

Application wide#

You can configure different CRUD methods to be enabled or disabled across all data models in buildGraphbackAPI through the crud config option.

In this example, generation of the delete mutation for all models is disabled.

const { typeDefs, resolvers, contextCreator } = buildGraphbackAPI(modelDefs, {
...,
crud: {
delete: false
}
});
caution

CRUD configuration set on an individual type with the @model annotation will override the application-wide CRUD configuration.

Per type#

CRUD can be enabled/disabled individually for each model in your schema with the @model annotation.

"""
@model(delete: false)
"""
type Note {
...
}

This disables deleteNote generation for Note.

type Mutation {
createNote(input: CreateNoteInput!): Note
updateNote(input: MutateNoteInput!): Note
}
caution

CRUD configuration set with @model will override the application-wide CRUD configuration value for that type only.