Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Add schema comments to type definitions. #35

Open
alloy opened this issue Feb 7, 2018 · 2 comments
Open

Add schema comments to type definitions. #35

alloy opened this issue Feb 7, 2018 · 2 comments

Comments

@alloy
Copy link
Member

alloy commented Feb 7, 2018

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

@orta
Copy link
Member

orta commented Feb 11, 2018

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
Copy link
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants