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

Am I right to assume the language flow package is an incomplete work in progress. #78

Open
epicallan opened this issue Jul 9, 2017 · 13 comments

Comments

@epicallan
Copy link
Contributor

epicallan commented Jul 9, 2017

I wanted to have flow as a target language but saw not tests or examples hence my assumption that its a work in progress. If its incomplete it would be good to update the readme to that effect.

@epicallan epicallan changed the title Am I right to assume the language flow package as an incomplete work in progress. Am I right to assume the language flow package is an incomplete work in progress. Jul 9, 2017
@brettjurgens
Copy link
Contributor

It should work fine. It was more of an example of how to extend this to generate outputs for other languages.

Does it not work? I believe there are only minor differences between flow and ts, which I think were accounted for

@jwickens
Copy link

jwickens commented Oct 28, 2017

I'm using flow and having some issues. First of all, there is no CLI options for using flow (from what I could tell). So I used the from-schema package. generateNamespace is the documented way to do it, and flow does not support namespaces.

Another issue I'm having are duplicated type names, which is because I want a graphql type to exported as a type, not as an interface. More on that in #103

Here's how im using the lib:

const { generateNamespace } = require('@gql2ts/from-schema')
const schemaDoc = require('./src/graphql/typeDefs')
const { DEFAULT_OPTIONS: flowDefaults } = require('@gql2ts/language-flow')
const fs = require('fs')

const myNamespace = generateNamespace('Humanise', schemaDoc, { ingnoreTypeNameDeclaration: true }, {
  ...flowDefaults,
  interfaceBuilder: (name, body) => `export type ${name} = ${body}`,
  typeBuilder: (name, body) => `export type ${name} = ${body}`,
  generateNamespace: (namespaceName, interfaces) => interfaces,
  generateInterfaceName: (name) => `${name}`
})

fs.writeFileSync('flowtypes.js', myNamespace)

@brettjurgens
Copy link
Contributor

@jwickens I'm not using flow but if you could set me up with a starter repo, I'd be glad to help through the issues you've encountered

@brettjurgens
Copy link
Contributor

@jwickens you should be able use the -e flag with @gql2ts/language-flow to load flow.

@brettjurgens
Copy link
Contributor

added to #163

@linonetwo
Copy link

linonetwo commented Jun 25, 2018

@gql2ts/language-flow is still generating declare namespace GQL, while it's an error, flow just don't have namespace:

flow
Error ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ server/generated/types.flow.js:6:9

Unexpected identifier

     3│ // tslint:disable
     4│ // graphql typescript definitions
     5│
     6│ declare namespace GQL {
     7│ interface IGraphQLResponseRoot {
     8│ data?: IQuery;
     9│ errors?: Array<IGraphQLResponseError>;



Found 1 error

namespace above is colored as red.

@brettjurgens
Copy link
Contributor

@linonetwo I'm not super familiar with how flow declares this. for what it's worth you can overwrite the generateNamespace to be: (_, interfaces) => interfaces and it will print out all of the contents without the namespace. It seems like a reasonable thing to default to, I'd certainly welcome a PR

@linonetwo
Copy link

linonetwo commented Jun 25, 2018

Seems @arahansen is making a working version in his PR, I'm using his version locally waiting his PR to be merged https://github.com/arahansen/gql2ts/blob/fa648647c642b2bac1ee8c7e14f62288807101ff/packages/language-flow/src/index.ts

#108

@linonetwo
Copy link

linonetwo commented Jun 25, 2018

But @arahansen 's version still ends each line with ;, while it should be ,. And it lacks indentation.
So I pass generated content to prettier-eslint to fix it:

gql2ts ./server/generated/schema.json -e $PWD/scripts/@gql2ts-language-flow.js -o ./server/generated/types.flow.js && prettier-eslint \"./server/generated/*.js\" --write

@brettjurgens
Copy link
Contributor

@linonetwo if prettier is installed, it should run the output through it by default. i'm not sure what you mean about the semicolons, can you give an example? is that in an type/interface declaration?

@linonetwo
Copy link

linonetwo commented Jun 27, 2018

It looks like this without prettier-eslint:

export type Block = {
blockNum: number;
blockID: string;
prevBlockID: ?string;
timestamp: string;
transactionMerkleRoot: string;
producerAccountID: string;
pending: boolean;
transactions: Array<Transaction>;
}

After formatting:

export type Block = {
  blockNum: number,
  blockID: string,
  prevBlockID: ?string,
  timestamp: string,
  transactionMerkleRoot: string,
  producerAccountID: string,
  pending: boolean,
  transactions: Array<Transaction>,
};

So my scripts includes prettier-eslint-cli:

{
    "gql2flow": "yarn build-schema && yarn gql-gen && yarn gqlJson2flow",
    "build-schema": "mkdir -p ./server/generated &&  node ./scripts/buildSchema.js > ./server/generated/schema.graphql",
    "gql-gen": "apollo-codegen introspect-schema ./server/generated/schema.graphql --output ./server/generated/schema.json",
    "gqlJson2flow": "gql2ts ./server/generated/schema.json -e $PWD/scripts/@gql2ts-language-flow.js -o ./server/generated/types.flow.js --ignore-type-name-declaration && prettier-eslint \"./server/generated/*.js\" --write"
}

and

// buildSchema.js
const { importSchema } = require('graphql-import');

console.log(importSchema('./server/graphql/schema.graphql'));

@vjpr
Copy link

vjpr commented Jul 19, 2018

@linonetwo Enums are included in the output with your script.

Here is a fix for enums:

import {
  IFromQueryOptions,
  WrapType,
  InterfaceAndTypeBuilder,
  TypePrinter,
  NamespaceGenerator,
} from '@gql2ts/types'
import {DEFAULT_OPTIONS as TS_OPTIONS} from '@gql2ts/language-typescript'
import {pascalize} from 'humps'
import * as util from '@gql2ts/util'

export const FLOW_WRAP_PARTIAL: WrapType = partial => `$SHAPE<${partial}>`
export const FLOW_INTERFACE_NAMER: WrapType = name => `${pascalize(name)}`
export const FLOW_INTERFACE_BUILDER: InterfaceAndTypeBuilder = (name, body) =>
  `export type ${name} = ${body}`
export const FLOW_ENUM_NAME_GENERATOR: WrapType = name => `${pascalize(name)}`
export const FLOW_TYPE_PRINTER: TypePrinter = (type, isNonNull) =>
  isNonNull ? type : `?${type}`
export const FLOW_POST_PROCESSOR: WrapType = str => `/* @flow */
${str}
`
export const FLOW_NAMESPACE_GENERATOR: NamespaceGenerator = (_, interfaces) => `
// graphql flow definitions
${interfaces}
`

export const DEFAULT_OPTIONS: IFromQueryOptions = {
  ...TS_OPTIONS,
  printType: FLOW_TYPE_PRINTER,
  generateInterfaceName: FLOW_INTERFACE_NAMER,
  generateEnumName: FLOW_ENUM_NAME_GENERATOR,
  enumTypeBuilder: (name, values) => `type ${name} = ${values}`,
  formatEnum: (values, documentationGenerator) => `
    ${util.filterAndJoinArray(
      values.map(value =>
        util.filterAndJoinArray([
          documentationGenerator(util.buildDocumentation(value)),
          `'${value.name}'`,
        ]),
      ),
      ' |\n',
    )}
  `,
  interfaceBuilder: FLOW_INTERFACE_BUILDER,
  generateNamespace: FLOW_NAMESPACE_GENERATOR,
  wrapPartial: FLOW_WRAP_PARTIAL,
  postProcessor: FLOW_POST_PROCESSOR,
}

export default DEFAULT_OPTIONS

@vjpr
Copy link

vjpr commented Jul 19, 2018

Also @linonetwo, you don't need to do this:

    "gql-gen": "apollo-codegen introspect-schema ./server/generated/schema.graphql --output ./server/generated/schema.json",

gql2ts can work with .graphql. Or is there another reason you need json?

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

No branches or pull requests

5 participants