How to extract a typescript interfaces from a schema.graphql with apollo client:codegen

1 answer

it seems like i need some help with this, because i was looking for a solution for a while now.

I created an angular project using graphql and configured the apollo client for that. In the backend i'm using laravel with the rebing/graphql extension. Now i'd like to extract some typescript interfaces from my schema to use them in my logic. A while ago i started to develop my own little system in which i dynamically generate gql queries from json definition during runtime. I guess, that's something which could get challenged too - i guess.. Thats why there are no real graphql query definitions in my code itself. I do not use any .gql/.graphql files.

My graphql query are create with something like:

UserModel.with('posts', { pagination: 10, offset: 0 }).get() <-- QueryRef  

What i try to say is that, this way, i can not easily start the apollo typescript generator to search for any graphql queries in my project to genereate types.

I already managed it to download my schema from the endpoint like so:

apollo client:download-schema ./libs/graphql/src/lib/schema.graphql --endpoint https://api.ellingtons-palace.com/graphql 

It works quite nice. That way i can get either grapqhl or json schemas.

What i'm not able to, is generate typescript files of interaces to use in my code. I tried it like that:

apollo codegen:generate --localSchemaFile=libs/graphql/src/lib/schema.json --target=typescript --includes=libs/graphql/src/lib/schema.graphql --tagName=graphql --addTypename --globalTypesFile=libs/graphql/src/lib/graphql-global-types.ts 

Unfortunately, everytime i get errors like:

The type of your project could not be derived from your config.

or Field "UserType.Locations" already exists in the schema. It cannot also be defined in this type extension.

Is there someone who can tell me what i'm doing wrong, or what options there could be to convert my schema to valid typescript?

Thanks in advance!!

Greets Jules ;)

Received answer to this question:
The best answer according to the author of the question:

I think your problem is that for apollo codegen:generate to work you don't just need a schema, you also need to provide a query that the tool can parse.

The query is used to figure out what types the tools should generate. It does not generate all the potential types for the whole schema, just the ones that you actually need, based on the properties you query for.

This UserModel.with is not graphql query syntax (looks like JavaScript), so I don't think apollo codegen:generate can parse that. It can parse graphql embedded in JavaScript (or TypeScript), by extracting the graphql parts.