Back to Graphql Code Generator

flow-resolvers

website/src/pages/plugins/flow/flow-resolvers.mdx

1.17.72.4 KB
Original Source

import { Callout } from '@theguild/components' import { PluginApiDocs, PluginHeader } from '@/components/plugin' import { pluginGetStaticProps } from '@/lib/plugin-get-static-props' export const getStaticProps = pluginGetStaticProps(__filename)

<PluginHeader /> <Callout> **Quick Start with `flow-resolvers`**

You can find a blog post we wrote about using and customizing this plugin here, it refers to typescript-resolvers but everything there is relevant to flow-resolvers as well.

</Callout>

Enum Resolvers

Apollo-Server and schemas built with graphql-tools supports creating resolvers for GraphQL enums.

This is helpful because you can have internal values that are different from the public enum values, and you can use the internal values in your resolvers.

Codegen allows you to specify either mappers or enumValues to map enums in your resolvers, and if you are using it for enums, you'll get a resolver signature for the enum resolvers as well.

Usage Example

With the following schema:

graphql
type Query {
  favoriteColor: Color!
}

enum Color {
  RED
  BLUE
}
yaml
schema: schema.graphql
generates:
  ./resolvers-types.js:
    config:
      enumValues:
        Color: ./enums#ColorsCode
    plugins:
      - flow
      - flow-resolvers
ts
export enum ColorsCode {
  MY_RED = '#FF0000',
  MY_BLUE = '#0000FF'
}
ts
import type { Resolvers } from './resolvers-types'
import { ColorsCode } from './enums'

const resolvers: Resolvers = {
  Color: {
    RED: ColorsCode.MY_RED,
    BLUE: ColorsCode.MY_BLUE
  },
  Query: {
    favoriteColor: () => ColorsCode.MY_RED // Now you cn return this, and it will be mapped to your actual GraphQL enum
  }
}

You can also define the same with explicit enum values:

yaml
schema: schema.graphql
generates:
  ./resolvers-types.js:
    config:
      enumValues:
        Color:
          RED: '#FF0000'
          BLUE: '#0000FF'
    plugins:
      - flow
      - flow-resolvers

Or, with mappers:

yaml
schema: schema.graphql
generates:
  ./resolvers-types.js:
    config:
      mappers:
        Color: ./enums#ColorsCode
    plugins:
      - flow
      - flow-resolvers
<PluginApiDocs />