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

generated .d.ts lacking exports #217

Open
cherihung opened this issue Aug 13, 2018 · 3 comments
Open

generated .d.ts lacking exports #217

cherihung opened this issue Aug 13, 2018 · 3 comments

Comments

@cherihung
Copy link

Hi, I used the cli tool to generate a .d.ts file.
Everything works except the generated file doesn't have any export. I'm looking at the examples and expecting that it should have a export default [namespace] ?

I am using a command like this:
gql2ts build/schemas.json -o ../common/models/gql_schemas.d.ts

thank you!

@felixfbecker
Copy link

I would like a flag to switch to export types instead of a global namespace too. Currently do this through the API:

const schemaStr = await readFile(GRAPHQL_SCHEMA_PATH, 'utf8')
    const schema = buildSchema(schemaStr)
    const result = (await graphql(schema, introspectionQuery)) as { data: IntrospectionQuery }

    const formatOptions = (await resolveConfig(__dirname, { config: __dirname + '/../prettier.config.js' }))!
    const typings =
        'export type ID = string\n\n' +
        generateNamespace(
            '',
            result,
            {
                typeMap: {
                    ...DEFAULT_TYPE_MAP,
                    ID: 'ID',
                },
            },
            {
                generateNamespace: (name: string, interfaces: string) => interfaces,
                interfaceBuilder: (name: string, body: string) =>
                    'export ' + DEFAULT_OPTIONS.interfaceBuilder(name, body),
                enumTypeBuilder: (name: string, values: string) =>
                    'export ' + DEFAULT_OPTIONS.enumTypeBuilder(name, values),
                typeBuilder: (name: string, body: string) => 'export ' + DEFAULT_OPTIONS.typeBuilder(name, body),
                wrapList: (type: string) => `${type}[]`,
                postProcessor: (code: string) => format(code, { ...formatOptions, parser: 'typescript' }),
            }
        )
    await writeFile(__dirname + '/src/backend/graphqlschema.ts', typings)
}

@brettjurgens
Copy link
Contributor

brettjurgens commented Sep 4, 2018

i think a flag is reasonable - because we actually do the same thing as @felixfbecker 😄

FWIW there's also DEFAULT_OPTIONS.exportFunction so you instead of concatenating 'export' you can do

interfaceBuilder: (name: string, body: string) => DEFAULT_OPTIONS.exportFunction(DEFAULT_OPTIONS.interfaceBuilder(name, body))

@rlancer
Copy link

rlancer commented Sep 4, 2018

Love the module!

I currently use a shell script to perform the transformation. Looking forward to a first class solution.

#!/usr/bin/env sh

sed -i -e 's/interface /export interface /g' graphqlTypes.d.ts
sed -i -e 's/const /export const /g' graphqlTypes.d.ts
sed -i -e 's/declare namespace GQL {//g' graphqlTypes.d.ts
sed -i -e '/__typename/c\\t\t__typename: string | null' graphqlTypes.d.ts
sed -i -e 's/^}//g' graphqlTypes.d.ts

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

4 participants