Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate typescript interfaces for graphql types using a graphql schema.json without operations?

How can i generate Typescript files from a schema.json. Neither graphql-codegenerator nor apollo-codegen seems to provide a method to generate typescript interfaces from a schema.json without requiring a specific usage of a type in an operation.

Im struggling with this for a while now. Is there a way to achieve this?

like image 761
DevJules Avatar asked Sep 05 '25 03:09

DevJules


1 Answers

My schema.json has a standard introspection output. I was struggling to find a proper way to generate my typescript interfaces.

I actually found a way to do so:

codegen.yml

schema:
    - http://my-graphql-api.com/graphql
    - ./local/schema.graphql
documents:
    - ./src/**/*.graphql
overwrite: true
clientSchema: ./local/schema.graphql
generates:
    src/graphql-types.ts:
        plugins:
            - typescript
            - typescript-operations
        config:
            declarationKind:
                union: type
                type: interface
                input: interface
                scalar: interface
                arguments: interface
                interface: interface
            enumsAsTypes: true
            includeDirectives: true
            commentDescriptions: true
            flattenGeneratedTypes: true
            avoidOptionals:
                field: false
                object: false
                inputValue: false

And then run: graphql-code-generator --config ./codegen.yml

https://graphql-code-generator.com/docs/plugins/typescript

is there a way to do this with apollo-codegen cli too?

like image 165
DevJules Avatar answered Sep 09 '25 20:09

DevJules