Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL Codegen for generating gql types #2485

Merged
merged 24 commits into from
May 24, 2021

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented May 11, 2021

What does it do?

  1. Adds command yarn rw generate types which will generate all autogenerated types (currently CurrentUser and graphql types)
  2. Modifies yarn rw dev to also call above generator
  3. Makes QueryOperationResult and MutationOperationResult overridable types so that when you bring your gql client, you can add your own types. (See packages/web/src/apollo/typeOverride.ts)

The generated types can be used like this:

// import generate type ->
import { PetQuery } from 'types/gql-types'
import type { CellSuccessProps, CellFailureProps } from '@redwoodjs/web'

export const QUERY = gql`
  query PetQuery {
    pets {
      id
    }
  }
`
// -> See type declaration here
export const Success = ({ pets }: CellSuccessProps<PetsQuery>) => {

You also get types from the Apollo client! (Overridable for other clients)

I realise that its unusal that rw generate types --watch has a watch flag, open to suggestions for a better way. I just thought generate was the best place to put this.

Uses graphql-codegen underneath.

Outstanding tasks:

  • Investigate using makeMergeSchema instead of the actual api server for generating types
    Update: Peter's cell mirrored types may generate the schema to a file, which we can reuse.
  • Remove generating types from build process elsewhere
  • Modify typescript generators for Cells to import types "optimistically"
  • Modify naming of scaffold queries to prevent conflicts in names (e.g. EditTaskQuery vs GetTaskQuery)
    was already done in another PR
  • Also pass other props passed to the Cell components (...queryRest)
  • Handle plural cells and their types
  • Handle typescript templates for .stories.tsx, .tests.tsx and .mocks.ts

Closes #418

@github-actions
Copy link

github-actions bot commented May 11, 2021

📦 PR Packages

Click to Show Package Download Links

https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/create-redwood-app-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-api-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-api-server-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-auth-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-cli-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-core-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-dev-server-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-eslint-config-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-eslint-plugin-redwood-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-forms-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-internal-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-prerender-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-router-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-structure-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-testing-0.31.2-4b44d71.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2485/redwoodjs-web-0.31.2-4b44d71.tgz

Install this PR by running yarn rw upgrade --pr 2485:0.31.2-4b44d71

@dac09 dac09 changed the title GraphQL Codegen GraphQL Codegen for generating gql types May 11, 2021
dac09 added 2 commits May 11, 2021 15:58
All type generation now under rw g types
Move watcher one level up, so it can be reused more easily for other actions
…-codegen

* 'main' of github.com:redwoodjs/redwood: (54 commits)
  Add private methods loose explicitly (redwoodjs#2554)
  Custom `useAuth` pass through for `RedwoodApolloProvider` (redwoodjs#2455)
  Prerender all routes nested in Set with prerender prop (redwoodjs#2542)
  Upgrade eslint and prettier packages including formatting fixes (redwoodjs#2540)
  contributors updates (redwoodjs#2544)
  Rename default datasource (redwoodjs#1941)
  Add default config for Component generation (redwoodjs#1814)
  build(deps): bump core-js from 3.10.1 to 3.12.1 (redwoodjs#2481)
  upgrade babel 7.14.2 with misc babel packages (redwoodjs#2541)
  build(deps): bump http-proxy-middleware from 1.1.0 to 2.0.0 (redwoodjs#2536)
  build(deps): bump pino-pretty from 4.7.1 to 4.8.0 (redwoodjs#2534)
  build(deps): bump concurrently from 6.0.2 to 6.1.0 (redwoodjs#2533)
  build(deps-dev): bump firebase-admin from 9.7.0 to 9.8.0 (redwoodjs#2522)
  build(deps): bump esbuild-loader from 2.10.0 to 2.13.0 (redwoodjs#2518)
  build(deps): bump @graphql-tools/merge from 6.2.13 to 6.2.14 (redwoodjs#2516)
  updating minor dependency versions across packages (redwoodjs#2532)
  Add JSON headers to Function generator template (redwoodjs#2457)
  fixed firebase promises so that they can be caught (redwoodjs#2503)
  Prevent re-initialization of a Firebase app that is already initialized (redwoodjs#2504)
  build(deps-dev): bump magic-sdk from 2.7.0 to 4.3.0 (redwoodjs#2463)
  ...
@jtoar jtoar added this to the next-release-candidate milestone May 19, 2021
@jtoar jtoar mentioned this pull request May 19, 2021
export { createCell }

/** @deprecated `withCell` will be removed in v0.32.0, please use `createCell` instead. */
export const withCell = createCell
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I've removed withCell

dac09 added 3 commits May 21, 2021 14:32
…-codegen

* 'main' of github.com:redwoodjs/redwood:
  Adds option to cell generation for lists of a given model (redwoodjs#2569)
  Router: More helpful typings for Route (redwoodjs#2592)
  upgrade Prisma v2.23.0 (redwoodjs#2566)
  add crowdin contribs (redwoodjs#2560)
  `rw exec` to run arbitary scripts in Redwood context (redwoodjs#2548)
@dac09 dac09 requested a review from peterp May 21, 2021 17:21
@dac09 dac09 marked this pull request as ready for review May 21, 2021 17:21
@@ -169,7 +169,7 @@ export const Success = ({ userProfile }) => {

exports[`creates a cell mock with a camelCase word name 1`] = `
"// Define your own mock data here:
export const standard = (/* vars, { ctx, req } */) => ({
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno why the snapshots do this weird thing. Its not actually doing this when we actually generate 🤷‍♂️

@@ -414,7 +416,7 @@ describe('UserProfileCell', () => {
`;

exports[`creates a cell test with a multi word name 1`] = `
"import { render, screen } from '@redwoodjs/testing'
"import { render } from '@redwoodjs/testing'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one of the weird snapshot things. Something to do with tsToJs maybe

@@ -101,10 +100,16 @@ export const handler = async ({
command: `cd "${path.join(
BASE_DIR,
'web'
)}" && yarn webpack-dev-server --config ../node_modules/@redwoodjs/core/config/webpack.development.js ${forward}`,
)}" && yarn cross-env NODE_ENV=development webpack-dev-server --config ../node_modules/@redwoodjs/core/config/webpack.development.js ${forward}`,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will also allow us to use tailwind's JIT without more hassle

Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small things that are problems, but overall looks good to me!

@@ -1,39 +1,43 @@
import { useQuery, OperationResult } from './GraphQLHooksProvider'
import { A } from 'ts-toolbelt'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a devDependency

Suggested change
import { A } from 'ts-toolbelt'
import { A } from 'ts-toolbelt'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha not sure what the change here is peter! But yes its a devDep, did you mean import type only?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, I don't know why I used the suggestion tool... top-kek!

I mean that because it's a devDependency it won't be installed for users, and it looks like a "design time requirement," basically I'm not sure what the type looks like when it's generated? Does it reference A or does it interpolate that value and expand it in the generated type def?

packages/web/src/global.web-auto-imports.ts Outdated Show resolved Hide resolved
@peterp
Copy link
Contributor

peterp commented May 24, 2021

Let's combine the efforts for creating docs once I've merged my two TS generation PRs.

dac09 added 4 commits May 24, 2021 11:12
…-codegen

* 'main' of github.com:redwoodjs/redwood:
  Removes console warnings in webhook verifiers (redwoodjs#2563)
  build(deps): bump eslint from 7.26.0 to 7.27.0 (redwoodjs#2610)
  CONTRIBUTING.md: styling tweaks for improved readability and consistency (redwoodjs#2608)
  build(deps): bump esbuild from 0.11.21 to 0.12.1 (redwoodjs#2579)
  Cache magic.link's token  (redwoodjs#2555)
  build(deps): bump @babel/plugin-transform-runtime from 7.14.2 to 7.14.3 (redwoodjs#2603)
  build(deps): bump @types/node from 15.3.1 to 15.6.0 (redwoodjs#2601)
  eslint-plugin-import 2.23.2 with lint fixes (redwoodjs#2604)
  build(deps): bump @apollo/client from 3.3.18 to 3.3.19 (redwoodjs#2576)
  build(deps): bump apollo-server-lambda from 2.22.2 to 2.24.1 (redwoodjs#2580)
  build(deps): bump babel-plugin-graphql-tag from 3.2.0 to 3.3.0 (redwoodjs#2600)
  build(deps): bump @babel/cli from 7.13.16 to 7.14.3 (redwoodjs#2578)
  build(deps-dev): bump @types/lodash from 4.14.169 to 4.14.170 (redwoodjs#2597)
  build(deps-dev): bump @types/findup-sync from 2.0.2 to 4.0.0 (redwoodjs#2574)
  Aggregate Dependabot misc patch minor upgrades (redwoodjs#2596)
  CONTRIBUTING.md: add missing char (redwoodjs#2594)
  changes test command instructions to reference run-e2e instead of test-tutorial (redwoodjs#2593)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RFC] Typed GraphQL
4 participants