Many of the upgrade steps below can be completed using the new @redwoodjs/codemods
package. Amazing! You'll run the codemods using the npm
subcommand npx
, which allows you to run a package without needing to install either locally or globally. Here's an example codemod you'll run from your project root directory:
<aside> 🚀 Codemod Available
npx @redwoodjs/codemods update-graphql-function
</aside>
Here are the only other things you should know in order to get the most out of this preview feature:
npx
is a subcommand of the npm
command.) If you encounter errors, you are likely using npm v7 (Node.js v16). Confirm by running npm --version
.Need to switch versions? Try nvm for Mac or Windows.
<aside> 💡 Helpful Typescript Commands These tips only apply after you have upgraded to the latest release.
You might notice type errors that don't seem right or up-to-date. Try:
yarn rw g types
and thenIf you see Prisma type errors, you might need to re-generate the Prisma client:
yarn rw prisma generate
</aside>This is the upgrade guide the v0.37 Release Notes <link here>
graphql.js
handler functionWe've done some house keeping, and now your API-side packages are split across two packages:
@redwoodjs/api
for general api-side utilities: logger, webhook, and dbAuth@redwoodjs/graphql-server
includes Context and GraphQL specific imports. This means the utilities to set up your GraphQL Server and GraphQL-specific errors are included in this package.<aside>
💡 Have you already upgraded your project to use Envelop+Helix? If you are already using @redwoodjs/graphql
as an experimental feature via these instructions, you will not need to complete steps 1 through 3 below. However, verify your implementation is correct — some aspects may have changed.
</aside>
@redwoodjs/graphql-server
as a dependency to your api sideyarn workspace api add @redwoodjs/graphql-server
Your ./api/package.json should look like this:
{
"name": "api",
"version": "0.0.0",
"private": true,
"dependencies": {
"@redwoodjs/api": "0.36.4" // your current version may be different
+ "@redwoodjs/graphql-server": "0.37.0"
}
}
./api/src/functions/graphql.{js/ts}
- and how you call the function<aside> 🚀 Codemod Available To implement this step via automated codemod, run:
npx @redwoodjs/codemods update-graphql-function
</aside>
You only need createGraphQLHandler
now.
<aside> 💡 Reference template file: api/src/functions/graphql.ts
</aside>
-import {
- createGraphQLHandler,
- makeMergedSchema,
- makeServices,
- } from '@redwoodjs/api'
// We only need this import 👇
+ import { createGraphQLHandler } from '@redwoodjs/graphql-server'
// We now use glob imports for services, directives and sdls 👇
import services from 'src/services/**/*.{js,ts}'
- import schemas from 'src/graphql/**/*.{js,ts}'
+ import directives from 'src/directives/**/*.{js,ts}'
+ import sdls from 'src/graphql/**/*.sdl.{js,ts}'
import { getCurrentUser } from 'src/lib/auth.js' // may not exist in your application
import { db } from 'src/lib/db'
import { logger } from 'src/lib/logger'
export const handler = createGraphQLHandler({
loggerConfig: { logger, options: {} },
getCurrentUser, // may not exist in your application
- schema: makeMergedSchema({
- schemas,
- services: makeServices({ services }),
- }),
// Just pass them in 👇
+ directives,
+ sdls,
+ services,
onException: () => {
// Disconnect from your database with an unhandled exception.
db.$disconnect()
},
})
<aside> 🚀 Codemod Available To implement this step via automated codemod, run:
npx @redwoodjs/codemods update-api-imports
</aside>
Errors
In your services, or in ./api/src/lib/auth
if you are using AuthenticationError
or ForbiddenError
- change where these are imported from.
<aside>
💡 An easy way to check this is to search for the string "from '@redwoodjs/api'", then check the imports for imports - like AuthenticationError
, ForbiddenError
- and move them across as necessary.
</aside>
- import { AuthenticationError } from '@redwoodjs/api'
+ import { AuthenticationError } from '@redwoodjs/graphql-server'
Context
Search and replace imports of context
, so it's imported from graphql-server instead
- import { context } from '@redwoodjs/api'
+ import { context } from '@redwoodjs/graphql-server'
If you have a typescript project, you can validate your project using a typescript check
yarn rw type-check api
# or
yarn rw tsc api