relay-tools / relay-compiler-language-typescript

⛔️ Obsolete - A language plugin for Relay that adds TypeScript support, including emitting type definitions.
MIT License
241 stars 70 forks source link

Add schema comments to type definitions. #35

Open alloy opened 6 years ago

alloy commented 6 years ago

As per @orta’s comment, we should add schema comments as documentation to the type definitions.

orta commented 6 years ago

Not quite figured this, looking like the details come though on the original createVisitor callbacks, but loses the description down the line. For now, I have a pretty solid test that should be green when it all works.

import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLID, GraphQLInterfaceType, GraphQLNonNull } from "graphql";
import {GraphQLCompilerContext, IRTransforms, transformASTSchema} from 'relay-compiler'
import * as parseGraphQLText from 'relay-test-utils/lib/parseGraphQLText'
import * as TypeScriptGenerator from '../src/TypeScriptGenerator'

const DogNameDescription = "Ideally short and sweet"

export const NodeInterface = new GraphQLInterfaceType({
  name: "Node",
  description: "An object with a Globally Unique ID",
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLID),
      description: "The ID of the object.",
    },
  }),
})

const DogType = new GraphQLObjectType({
  name: 'Dog',
  fields: {
    id: { type:  new GraphQLNonNull(GraphQLID) },
    name: { type: GraphQLString, description: DogNameDescription }
  },
  interfaces: [NodeInterface],
})

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      doggo: {
        type: DogType,
        args: {
          id: { type: new GraphQLNonNull(GraphQLID) }
        }
      }
    }
  })
})

test("handles adding the comment on a field", () => {
  const text = `
  fragment HandlesCommentsFragment on Node {
    id
    ... on Dog {
      name
    } 
  }
  `

  const {definitions} = parseGraphQLText(schema, text);
  const tsInterface = new GraphQLCompilerContext(schema, schema)
    .addAll(definitions)
    .applyTransforms(TypeScriptGenerator.transforms)
    .documents()
    .map(doc =>
      TypeScriptGenerator.generate(doc, {
        customScalars: {},
        enumsHasteModule: null,
        existingFragmentNames: new Set(['PhotoFragment']),
        inputFieldWhiteList: [],
        relayRuntimeModule: 'relay-runtime',
        useHaste: true,
      }),
    )
    .join('\n\n');

    expect(tsInterface).toContain(DogNameDescription)
})
kastermester commented 6 years ago

Without having looked too much into it. I think some changes to the relay code might be needed to make this work.

The way I see it, the IR representation should contain the comments. See the TS type definitions here (copy/pasted and adapted from flow code from the relay code): https://github.com/relay-tools/relay-compiler-language-typescript/blob/master/types/graphql-compiler/core/GraphQLIR.d.ts.

But perhaps some dialog with the Relay team is in order wrt. solving this.