End to End React with Prisma 2
Start at the beginning and work your way through this project. The code for each step as well as the finished project can be found in the Github repository.
7. Add Bundle queries and mutations
Objective: Create all of the bundle queries and the create mutation.
Let's add the createBundle
mutation as well as the bundle
and bundles
queries. These will seem pretty similar to the feeds queries and mutations that we've already made.
utils/api/typeDefs.ts
ts
import { gql } from 'apollo-server-micro'export const typeDefs = gql`type Feed {id: Stringname: Stringurl: String}type Bundle {id: Stringname: Stringdescription: String}input FeedInput {id: String}input BundleInput {id: String}input FeedCreateInput {id: Stringurl: Stringname: String}input BundleCreateInput {id: Stringname: Stringdescription: String}type Query {hello: Stringfeed(data: FeedInput): Feedbundle(data: BundleInput): Bundlefeeds: [Feed]bundles: [Bundle]}type Mutation {createFeed(data: FeedCreateInput): FeedcreateBundle(data: BundleCreateInput): Bundle}`
utils/api/resolvers.ts
ts
export const resolvers = {Query: {hello: (parent, args, context) => `hi!`,feed: (parent, { data: { id } }, { prisma }) =>prisma.feed.findOne({ where: { id } }),bundle: (parent, { data: { id } }, { prisma }) =>prisma.bundle.findOne({ where: { id } }),feeds: (parent, args, { prisma }) => prisma.feed.findMany(),bundles: (parent, args, { prisma }) => prisma.bundle.findMany(),},Mutation: {createFeed: async (parent, { data }, { prisma, user }) => {const result = await prisma.feed.create({ data: { ...data } })return result},createBundle: async (parent, { data }, { prisma, user }) => {const result = await prisma.bundle.create({data: { ...data },})return result},},}
Let's try the createBundle
mutation:
graphql
mutation createBundleMutation($data: BundleCreateInput) {createBundle(data: $data) {...BundleFragment}}fragment BundleFragment on Bundle {idnamedescription}
Variables
json
{"data": {"name": "My bundle","description": "This bundle is about fun stuff","id": "1"}}
Next, we can run a bundles
and bundle
query to make sure it works:
graphql
{bundles {idnamedescription}}
graphql
query bundleQuery($data: BundleInput) {bundle(data: $data) {...BundleFragment}}fragment BundleFragment on Bundle {idnamedescription}
json
{"data": { "id": "1" }}
Ensure that they return the bundle that we just created either as an array or a single object.